Recent

Author Topic: Using TObject.Dispatch  (Read 11563 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Using TObject.Dispatch
« Reply #15 on: October 21, 2016, 02:19:25 pm »
You should try to override the TForm.DefaultHandler and do some logging in order to figure out which forms causes the SIGSEGV and under what conditions. This could maybe disclose a bug in your code you have not spottet yet :)

True, perhaps I have a lurking bug that I should squish. I'll try that.

“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Using TObject.Dispatch
« Reply #16 on: October 21, 2016, 02:22:35 pm »
You may have inadvertently checked "Use Heaptrc unit (check for mem-leaks) (-gh)" in the Project option dialog. (See attached image)

Remove the check and see if the message remains.

Ah, that is possible! I will try that. Thanks!
« Last Edit: October 21, 2016, 02:24:54 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Using TObject.Dispatch
« Reply #17 on: October 24, 2016, 04:22:38 pm »
I'm trying to make some progress. I added a TForm.DefaultHandler to all the Forms, and am checking the messages coming in. One of my Forms is getting message ids like:

$B02C
$B015

even when I don't use TObject.Dispatch to send any. I thought user messages were free to use any id greater than:

$400 (LM_USER)

:( Does anyone have an idea where these might come from? If I do send messages using Dispatch they show up along with these.

Cheers,
VTwin
« Last Edit: October 24, 2016, 04:25:24 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Using TObject.Dispatch
« Reply #18 on: October 24, 2016, 04:35:23 pm »
Maybe I answered that myself, a word > 32767 is a negative smallint. But message id is 4 bytes...
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: Using TObject.Dispatch
« Reply #19 on: October 24, 2016, 05:47:07 pm »
I'm getting confused.. The TObject.Dispatch is described as a "integer message handler" in FPC, but in Delphi it is 2-byte integers. The message structure in this subject:

Code: Pascal  [Select][+][-]
  1. TVMessage = packed record
  2.   Id : dword; //This might be a wrong data type..
  3.   Param : word;
  4.   Flag  : word;
  5.   Text  : string;
  6. end;

May be wrong all along since the message's id should be a word (or smallint if signed - both 2 bytes) and not a dword (4 bytes). You might want to experiment with this to check if that is the problem.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Using TObject.Dispatch
« Reply #20 on: October 25, 2016, 09:05:19 pm »
I'm fairly sure that a 64 bit unsigned integer (dword, longword, or cardinal) is correct:

http://freepascal.org/docs-html/ref/refsu5.html

http://www.freepascal.org/docs-html/ref/refsu32.html

"The TObject.Dispatch method can be used to call a message handler. It is declared in the system unit and will accept a var parameter which must have at the first position a cardinal with the message ID that should be called."

I was however confused as the message id seemed to be a 2 bit signed integer. I thought I read somewhere that negative values are reserved by the system, and that seems to be what I was seeing.

I did change the string to a shortstring just in case, but did not see a difference.

Cheers,
VTwin
« Last Edit: October 25, 2016, 10:36:36 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Using TObject.Dispatch
« Reply #21 on: October 25, 2016, 10:18:18 pm »
At this point I am no longer able to reproduce the error. The messages are all getting through, and the program does not crash. I am using the Dispatch method I described.

I added a DefaultHandler to each form that responded to a message and monitored the messages. Note the peculiar (w <= 32767) to screen system messages.

Code: Pascal  [Select][+][-]
  1. procedure TGraphForm.DefaultHandler(var msg);
  2. begin
  3.   UnhandledMessageDlg('GraphForm', msg); // debug
  4. end;      
  5.  


Code: Pascal  [Select][+][-]
  1. procedure UnhandledMessageDlg(form: string; var msg);
  2. var
  3.   w: longword;
  4.   s: string;
  5. begin
  6.   w := TVMessage(msg).Id;
  7.   if (w > LM_User) and (w <= 32767) then begin
  8.     s := 'Unknown';
  9.     case w of
  10.       OM_FileChange        : s := 'FileChange';
  11.       OM_DataChange        : s := 'DataChange';
  12.  
  13.       ...
  14.  
  15.     end;
  16.     s := 'Unhandled Message in ' + form + ': ' + s + ' ($' + IntToHex(w, 4) + ')';
  17.     ShowMessage(s);
  18.   end;
  19. end;        
  20.  


Then I commented out all the message directives, and turned them back on one by one. No error, and all is working, but I am not clear why.

So... I guess this is closed unless the behavior reappears.  :-\

I appreciate the input!

Cheers,
VTwin
« Last Edit: October 25, 2016, 10:41:16 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Using TObject.Dispatch
« Reply #22 on: October 27, 2016, 02:53:05 pm »
By the way, howardpc and rvk, yes I had turned on the Heaptrc unit! :-[

Thanks!

Cheers,
VTwin
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: Using TObject.Dispatch
« Reply #23 on: October 28, 2016, 01:31:53 pm »
Fair enough! But then there may be a difference in Delphi and FPC which should be considered when designing code that may be compiled by different compilers:

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/System__TObject__Dispatch.html

 

TinyPortal © 2005-2018