Recent

Author Topic: Strange bug in QT Trayicon's PopupMenu  (Read 5742 times)

El Salvador

  • Full Member
  • ***
  • Posts: 134
Strange bug in QT Trayicon's PopupMenu
« on: April 30, 2021, 08:06:55 pm »
Hello everybody,

in my project I use Trayicon and in widgetset QT (os KDE Neon) I have a strange bug about its popupMenu. Basically, I use both the left click (OnClick event) and the right click (PopupMenu property) on the trayicon to show the popupMenu. The popup menu has some menu items, including the one to close the program.

If I RIGHT click on trayicon and then on the menu item Exit, the software closes correctly.

If I LEFT click on trayicon, close the popup menu (without clicking on Exit) and then I open the popup menu again with the RIGHT click and then click on Exit, then nothing will happen or it will go to SigSegv.

I have attached a demo project which reproduces the problem.

Furthermore, I also tried to download and compile the git version of Tomboy-ng and it has the same problem. But the official version from the ubuntu repositories works great. Could it be a regression? @dbannon, what version of Lazarus do you use to distribute tomboy-ng on Ubuntu?

Thank you everybody

Edit: Tried with Lazarus 2.0.10 and Trunk.
« Last Edit: April 30, 2021, 08:12:54 pm by El Salvador »

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Strange bug in QT Trayicon's PopupMenu
« Reply #1 on: May 01, 2021, 10:09:00 am »
Hm, interesting.

I compile my tomboy-ng releases with FPC320 and Lazarus Trunk but the debian autobuilds uses Lazarus 2.0.12 (from memory) and I have not had any reports of that problem.

Are you capturing the right click and using that event to trigger the menu ?  I don't think you should, the KNSI (KSNI ?) spec allows only one event back and that is internally directed to the popup menu.  Some OSs will use the right click to manage thinks like systray location, size etc.  And KDE is home to that spec ...

So, if there are any, try removing any events coming back to your app.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

El Salvador

  • Full Member
  • ***
  • Posts: 134
Re: Strange bug in QT Trayicon's PopupMenu
« Reply #2 on: May 01, 2021, 11:15:15 am »
Are you capturing the right click and using that event to trigger the menu ?
 
Nope. This is another issue (QSystemTrayIcon::ActivationReason doesn't fire with right click), but it is KDE & QT fault and I gave up.

I don't think you should, the KNSI (KSNI ?) spec allows only one event back and that is internally directed to the popup menu.  Some OSs will use the right click to manage thinks like systray location, size etc.  And KDE is home to that spec ...
 
I don't know this spec (free desktop spec?), but I did other tests with my simple demo. If I open the popup menu several times with the left click, on the third click the program will go to SIGSEGV (in KstatusNotifier internals).

Basically:
1 Left click on the icon -> show popup menu
- Click on taskbar / form to close the popup menu
2 Left click on the icon -> show popup menu
- Click on taskbar / form to close the popup menu
3 Left click on the icon -> SigSegv

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Strange bug in QT Trayicon's PopupMenu
« Reply #3 on: May 01, 2021, 12:29:58 pm »
.....
doesn't fire with right click), but it is KDE & QT fault and I gave up.
No, I am afraid thats intentional.   AppIndicator / KNSI thingo - all you can do is assign a menu and respond to those menu events, no right click, no double click.  A lot depends on your desktop, some still support the older SystemTray standard but its becoming fewer.


Basically:
1 Left click on the icon -> show popup menu
- Click on taskbar / form to close the popup menu
2 Left click on the icon -> show popup menu
- Click on taskbar / form to close the popup menu
3 Left click on the icon -> SigSegv

Well, I cannot replicate that, tried in two Linux env, you are using Linux I guess ?  I don't have a KDE VM at the moment, can you test for this problem on some other desktop ?

So, its just a demo, one you can post here ?

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

El Salvador

  • Full Member
  • ***
  • Posts: 134
Re: Strange bug in QT Trayicon's PopupMenu
« Reply #4 on: May 01, 2021, 10:21:10 pm »
Quote
No, I am afraid thats intentional.   AppIndicator / KNSI thingo - all you can do is assign a menu and respond to those menu events, no right click, no double click.  A lot depends on your desktop, some still support the older SystemTray standard but its becoming fewer.
Yes, it is intentional in Linux. I checked QT/KDE sources.


Quote
Well, I cannot replicate that, tried in two Linux env, you are using Linux I guess ?  I don't have a KDE VM at the moment, can you test for this problem on some other desktop ?
KDE Neon. Next week I will try on Kubuntu 21.04. Anyway I just tried github release of tomboy-ng in KDE Neon. Same bug. Probably I will make a QT project in C++, to test this behaviour. Maybe is it a QT/KDE bug?

Quote
So, its just a demo, one you can post here ?
See my first post.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Strange bug in QT Trayicon's PopupMenu
« Reply #5 on: May 02, 2021, 01:24:01 am »
Yep, I can replicate it on MXLinux KDE.
Interesting !

Will need to see what happens in a non FPC.Lazarus application to be sure.

Davo

Edit :  It does not seem to happen with pidgin and qlipper but both seem to use the systray in different modes.   it does not happen using a GTK2 version of tomboy-ng.  A strange difference between the GTK2 version and the Qt5 version seems to be that the GTK2 icon pops up in the SysTray, immediately visible, while the Qt5 one comes up, hidden. That indicates a different mechanism is in play here ....

In the GTK2 version, applying LAZUSEAPPIND I find I that the app is using the quite old original System Tray model, and that works fine. The newer KNSI thingo is not quite so happy.

(Nice of KDE to still do the old System Tray, life was so much easier back then ....  ::)  )

« Last Edit: May 02, 2021, 02:05:59 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

El Salvador

  • Full Member
  • ***
  • Posts: 134
Re: Strange bug in QT Trayicon's PopupMenu
« Reply #6 on: May 03, 2021, 10:57:05 am »
Some news. After some testing, in my opinion the problem lies when we destroy and recreate popupmenu handles in the Popup() method:
Code: [Select]
procedure TPopupMenu.PopUp(X, Y: Integer);
begin
  if ActivePopupMenu <> nil then ActivePopupMenu.Close;
  FPopupPoint := Point(X, Y);
  ReleaseCapture;
  DoPopup(Self);
  if Items.Count = 0 then exit;
  ActivePopupMenu := Self;
  Items.InitiateActions;
  DestroyHandle;  //<---- here
  CreateHandle;  //<---- here
  if Assigned(OnMenuPopupHandler) then OnMenuPopupHandler(Self);
  TWSPopupMenuClass(WidgetSetClass).Popup(Self, X, Y);
end;   
And the right click -> QT's context menu doesn't have much to do with it. I was able to simulate the problem even just using the left click (and therefore with the code managed by LCL and QT). Indeed, on the third attempt to open and close the popup menu with the left click, the application goes to SigSegv.

If I comment out the previous two lines of code, I have no problem. So the problem lies there. Any ideas for fixing the bug?

Edit: Same bug when assign the popupmenu to Form1.PopupMenu.
« Last Edit: May 03, 2021, 11:18:26 am by El Salvador »

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Strange bug in QT Trayicon's PopupMenu
« Reply #7 on: May 03, 2021, 01:47:56 pm »
Some news. After some testing, in my opinion the problem lies when we destroy and recreate popupmenu handles in the Popup() method:
Hmm, rather sadly, thats not exactly limited to Qt5 and SysTray, that method is in ./lcl/include/popupmenu.inc and might have quite wide reaching implications.
Have you checked for, eg, memory leaks ?  My guess is we'd need to work out why those two lines were put in there in the first place to be sure they are not needed on some other platform, some other widget set...


> And the right click -> QT's context menu doesn't have much to do with it. I was able to simulate the problem even just using the left click

Yes, so was I, I did not try right click, doing left click three times and cancelling on the SysTray will crash on KDE/Qt5

> Edit: Same bug when assign the popupmenu to Form1.PopupMenu.

I cannot replicate that. You mean assigning a popupmenu to the form's popupmenu property and doing the same repeated clicks thing ?

Davo

EDIT:  I guess the real issue is can we be sure this is a Lazarus bug ?  Sure looks like it but those two lines of code you chopped out don't do any harm with the other widgets ......
« Last Edit: May 03, 2021, 01:57:05 pm by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

El Salvador

  • Full Member
  • ***
  • Posts: 134
Re: Strange bug in QT Trayicon's PopupMenu
« Reply #8 on: May 03, 2021, 03:32:02 pm »
Quote
Hmm, rather sadly, thats not exactly limited to Qt5 and SysTray, that method is in ./lcl/include/popupmenu.inc and might have quite wide reaching implications.
Nope. I don't have enough knowledge about LCL and my approach is Try and Error. So I might as well say something wrong in my analysis.

Quote
Have you checked for, eg, memory leaks ?  My guess is we'd need to work out why those two lines were put in there in the first place to be sure they are not needed on some other platform, some other widget set...
Probably with re-creating the handles, QTTrayicon's contextMenu has the old handle. By exiting the application (and thus starting to free up memory) QT tries to free the memory of the old handle, effectively going into SigSegV (it also sometimes does this during the life of the application, imho). This also causes the onClick event issue, with the popup menu being handled by QT.

New demo attached. The PopupMenu1 is assigned to Form1.PopupMenu and Trayicon1.Popup.

Step:
1 Right click in form to open the PopupMenu1
2 Close the PopupMenu1

Ok, now the Trayicon1.Popup is broken (it has the old handle of PopupMenu1). If you:
- Click on the button of the form to reload the trayicon's icon -> crash SigBUS or SigSegV
- Right click on the trayicon -> onClick event of the menuitems doesn't work (try to click menu item "ShowMessage")

Edit: Bugreport https://bugs.freepascal.org/view.php?id=38849
« Last Edit: May 03, 2021, 10:46:56 pm by El Salvador »

 

TinyPortal © 2005-2018