Recent

Author Topic: [SOLVED]error when modifying main form from indy thread  (Read 14532 times)

Michael Collier

  • Sr. Member
  • ****
  • Posts: 253
[SOLVED]error when modifying main form from indy thread
« on: October 16, 2015, 04:50:01 pm »
I've posted this here (linux forum) because this error only seems to happen on my linux machine and not on windows, but I'm not sure if it is related to linux per se. I'm using lazarus v1.0.10 wth fpc2.6.2 compililng with dwarf2 , not using cmem.

I have an indy server object that uses sendmessage() to update a memo on the main form. Very simple but on linux I get SIG type errors, the call stack shows functions relating to gtk e.g. gtk_text_layout_xxxxxxx

My understanding is that calls from threads to the main form are ok using sendmessage, there are no other updates to the main form (I don't even move the mouse over the application).

I even tried using a criticalsection, just to see if it made any difference (it didn't).

Could the problem be with gtk? Can I do anything about that?
Is the problem with lazarus 1.1.10?

« Last Edit: October 16, 2015, 06:25:20 pm by Michael Collier »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3646
  • I like bugs.
Re: error when modifying main form from indy thread
« Reply #1 on: October 16, 2015, 04:55:46 pm »
Use TThread.Synchronize.

GetMem

  • Hero Member
  • *****
  • Posts: 3508
Re: error when modifying main form from indy thread
« Reply #2 on: October 16, 2015, 05:46:26 pm »
Always update visual controls with TIdSync or TIdMessage. The first one is synchronous the later on the other hand is asynchronous.
Something like this:
Code: Pascal  [Select]
  1. uses IdSync;
  2. type
  3.   TSync = class(TIdSync)
  4.   protected
  5.     procedure DoSynchronize; override;
  6.   public
  7.     FMessage: string;
  8.   end;
  9.  
  10. procedure TSync.DoSynchronize;
  11. begin
  12.   Form1.Memo1.Lines.Add(FMessage)
  13. end;
  14.  
  15. procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
  16. var
  17.   Sync: TSync;
  18. begin
  19.   //...
  20.   if (SomeCondition) then
  21.   begin
  22.     Sync := TSync.Create;
  23.     try
  24.       Sync.FMessage := 'This is a test';
  25.       Sync.Synchronize;
  26.     finally
  27.       Sync.Free;
  28.     end;
  29.   end;
  30. end;                            
  31.  

Michael Collier

  • Sr. Member
  • ****
  • Posts: 253
[SOLVED] Re: error when modifying main form from indy thread
« Reply #3 on: October 16, 2015, 06:24:59 pm »
Thank you, it is much better now

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: [SOLVED]error when modifying main form from indy thread
« Reply #4 on: October 16, 2015, 07:18:47 pm »
what happens if the postmessage is used instead? Is this behavior a bug (probably) or by design on GTK? Just to clarify a bit of what I'm asking here I'm quoting the help file
The difference between SendMessage and PostMessage is the way that they return control to the calling thread. With SendMessage control is not returned until the window that the message was sent to has completed processing the sent message, however with PostMessage control is returned immediately.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Michael Collier

  • Sr. Member
  • ****
  • Posts: 253
Re: [SOLVED]error when modifying main form from indy thread
« Reply #5 on: October 16, 2015, 08:04:08 pm »
Just tried postmessage() to find out what would happen, same errors as sendmessage().

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: [SOLVED]error when modifying main form from indy thread
« Reply #6 on: October 16, 2015, 11:25:44 pm »
Thank you for testing. At this point I think that this issue worth a bug report but since I haven't looked close enough on the message implementation in GTK I'll probably stay back on this one and let others decide. One last think can you create a simple test application that shows the error something along the lines of a thread sending a message every 10 seconds up to a number of times eg 10 messages. I would like to run it on QT as well to see how it fairs.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Michael Collier

  • Sr. Member
  • ****
  • Posts: 253
Re: [SOLVED]error when modifying main form from indy thread
« Reply #7 on: October 17, 2015, 12:39:31 pm »
Ok I'll get something done today, I can test on apple mac too if you like. Not sure what is meant by QT though, is it this http://wiki.freepascal.org/Qt_Interface

zeljko

  • Hero Member
  • *****
  • Posts: 1081
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: [SOLVED]error when modifying main form from indy thread
« Reply #8 on: October 17, 2015, 01:45:35 pm »
Postmessage works fine with Qt (multithreading app).

Michael Collier

  • Sr. Member
  • ****
  • Posts: 253
Re: [SOLVED]error when modifying main form from indy thread
« Reply #9 on: October 17, 2015, 02:50:26 pm »
I have attached test project..

thread_sendmessage.zip

You can choose the number of messages, on linux I can do 3 messages without crashing, but higher numbers cause the error to happen.

You can also select a sleep period in milliseconds between messages, not sure if this has any affect on bug.

Michael Collier

  • Sr. Member
  • ****
  • Posts: 253
Re: [SOLVED]error when modifying main form from indy thread
« Reply #10 on: October 17, 2015, 04:44:56 pm »
I extended the test project (attached thread_sendmessage2.zip )to offer the option of performing update(),refresh() & processmessages() procedures and retested linux/windows and this time apple mac too.

The mac crashes if update(),refresh() & processmessages()  is called within sendmessage() but ok for postmessage() and  synchronise(). The mac is Version 10.5.8. Lazarus 1.0.12 fpc 2.6.2

Windows is fine, under all circumstances, so I guess an application tested on windows could fail on apple.

thread_sendmessage2.zip



taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: [SOLVED]error when modifying main form from indy thread
« Reply #11 on: October 17, 2015, 04:49:03 pm »
I'll take a closer look on the demo later tonight. Thank you for your time, I appreciate it.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Michael Collier

  • Sr. Member
  • ****
  • Posts: 253
Re: [SOLVED]error when modifying main form from indy thread
« Reply #12 on: October 18, 2015, 02:22:40 pm »
I'm happy to help, any improvements to lazarus benefits us all  :)

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: [SOLVED]error when modifying main form from indy thread
« Reply #13 on: October 19, 2015, 09:05:40 am »
I'm still having problems installing lazarus 1.4.4 on my linux. Testing is impossible at this time for me I have the test application though so at some point in the future I will run my tests.
Sorry for any and all delays on this .
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Michael Collier

  • Sr. Member
  • ****
  • Posts: 253
Re: [SOLVED]error when modifying main form from indy thread
« Reply #14 on: October 19, 2015, 12:14:15 pm »
No worries, thanks for trying