Recent

Author Topic: PeekMessage in GTK2  (Read 973 times)

CanineQwer

  • New Member
  • *
  • Posts: 18
PeekMessage in GTK2
« on: July 17, 2020, 04:03:48 am »
Hello.
When using PeekMessage under Linux(GTK2), the program occasionally crashes and exits.
Try-Except does not solve the problem(SIGABRT).

Code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.   while true do
  4.     PMessages;
  5. end;
  6.  
  7. procedure PMessages;
  8. var
  9.   Msg: TMsg;
  10. begin
  11.   while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do begin
  12.     if Msg.Message = WM_QUIT then exit;
  13.     {$IFNDEF Linux} //TODO?
  14.     TranslateMessage(Msg);
  15.     DispatchMessage(Msg);
  16.     {$ENDIF}
  17.   end;
  18.   Sleep(1);
  19. end;
  20.  

On average, an error occurs one minute after launch, but sometimes the program runs without errors for about half an hour.
If you do PeekMessage in a Thread, the error occurs a little faster (regardless of critical sections).

Code: Pascal  [Select][+][-]
  1. function TGtk2WidgetSet.PeekMessage(var lpMsg: TMsg; Handle : HWND;
  2.   wMsgFilterMin, wMsgFilterMax, wRemoveMsg: UINT): Boolean;
  3. var
  4.   vlItem : TGtkMessageQueueItem;
  5. begin
  6.   //TODO Filtering
  7.   DebugLn('Peek !!!' );
  8.   fMessageQueue.Lock;   //!!!!!!!!Error is here
  9.   try
  10.     vlItem := fMessageQueue.FirstMessageItem;
  11.     Result := vlItem <> nil;
  12.  
  13.     if Result  then begin
  14.       lpMsg := vlItem.Msg^;
  15.       if (wRemoveMsg and PM_REMOVE) = PM_REMOVE then
  16.         fMessageQueue.RemoveMessage(vlItem,FPMF_Internal,true);
  17.     end;
  18.   finally
  19.     fMessageQueue.UnLock;
  20.   end;
  21. end;  
  22.  
  23. procedure TGtkMessageQueue.Lock;
  24. begin
  25.   if InterlockedIncrement(fLock)=1 then
  26.     {$IFDEF USE_GTK_MAIN_OLD_ITERATION}
  27.     EnterCriticalsection(FCritSec);
  28.     {$ELSE}
  29.     g_main_context_acquire(FMainContext);       //then the path goes to the libraries. The error is here.
  30.     {$ENDIF}
  31. end;
  32.  

What can you advise or when will this error be fixed?
*
Checked for Lazarus 2.0.8, 2.0.10.
On different graphical shells(just in case).

zeljko

  • Hero Member
  • *****
  • Posts: 1147
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: PeekMessage in GTK2
« Reply #1 on: July 17, 2020, 10:08:51 am »
1.glib2 version ?
2.gtk2 version ?
3.linux distribution ?
4.example project ?

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #2 on: July 17, 2020, 11:52:08 am »
Latest Ubuntu with all updates.
And the last Lazarus.

zeljko

  • Hero Member
  • *****
  • Posts: 1147
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: PeekMessage in GTK2
« Reply #3 on: July 17, 2020, 06:45:23 pm »
Currently it runs more than 30 minutes and everything looks ok. Fedora 30 64bit gtk2-2.24.32, glib2-2.60.7, test project compiled with fpc-3.0.4.
EDIT: Please, try to use writeln() instead of DebugLn() for 'Peek !!!' message in gtk2winapi.inc
« Last Edit: July 17, 2020, 06:49:07 pm by zeljko »

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #4 on: July 17, 2020, 09:45:20 pm »
It got a little better, but the error still appears.
Here's a project where you catch an error in 10-20 seconds.

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #5 on: July 17, 2020, 09:53:14 pm »
I will answer in advance, even if you delete that line altogether, nothing changes.
Please download MomError.rar and see it please.

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #6 on: July 17, 2020, 10:26:32 pm »
On WindowsLazarus, the project works fine.
The error is in GTK2 functions.

zeljko

  • Hero Member
  • *****
  • Posts: 1147
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: PeekMessage in GTK2
« Reply #7 on: July 18, 2020, 09:43:25 am »
It got a little better, but the error still appears.
Here's a project where you catch an error in 10-20 seconds.

Still works here w/o problems (since yesterday afternoon), so my conclusion is that it's something about glib2.

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #8 on: July 18, 2020, 11:18:34 am »
I'll download Fedora, take a look and unsubscribe.

dbannon

  • Hero Member
  • *****
  • Posts: 1148
    • tomboy-ng, a rewrite of the classic Tomboy
Re: PeekMessage in GTK2
« Reply #9 on: July 18, 2020, 11:49:27 am »
EDIT: Please, try to use writeln() instead of DebugLn() for 'Peek !!!' message in gtk2winapi.inc

Maybe that not a good idea. GTK2 is used on windows, it runs like a three legged dog and is a nightmare to install but it is used there.  I cannot give you an Lazarus example but GTK2 apps linked to Mono are not uncommon.  And windows gets quite funny when it hits a writeln and no console.

But related, be brilliant for that debug statement to be removed, it drives my Linux users (with Mate desktop) crazy.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #10 on: July 18, 2020, 12:59:20 pm »
I also said that it did not help fix the error.
So it won't go into release, don't worry.

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #11 on: July 20, 2020, 06:53:12 am »
I installed Fedora 32 with all updates.
The error still appears.
I ask you very much, please take a look at this project.
* Attached *
DebugLn -> WriteLn doesn't solve the problem.

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #12 on: July 20, 2020, 07:19:12 am »
Screenshots on Fedora.
Error after 3 seconds.

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #13 on: July 20, 2020, 07:53:07 am »
You're right, the call stack leads to the g_main_context_acquire function from the libglib-2.0.so library.
I asked them to test this function, but I ask you to check if you are using it correctly.
Maybe you need to lock it in some other way, or you don't need to lock it at all, or do something there before the lock.
« Last Edit: July 20, 2020, 08:22:16 am by CanineQwer »

CanineQwer

  • New Member
  • *
  • Posts: 18
Re: PeekMessage in GTK2
« Reply #14 on: July 20, 2020, 08:30:23 am »
In confirmation:
If you search in Google for "USE_GTK_MAIN_OLD_ITERATION", you will see that there are a lot of descriptions of this bug, from Lazarus users with the PeekMessage function...

the first is the same result:
https://bugs.freepascal.org/view.php?id=35395

 

TinyPortal © 2005-2018