When program quit, QSystemTrayIcon crashed.
-
It is nessary to add "instance_=NULL;" into destructor?
class Tray : public QSystemTrayIcon
{
Q_OBJECT
public:
~Tray() { instance_=NULL; }
static Tray* instance()
{
if(NULL==instance_)
{
instance_=new PTray();
}
return instance_;
}
private:
Tray(){}
static Tray *instance_;
}; -
It is nessary to add "instance_=NULL;" into destructor?
Well, no. This does nothing, as
delete NULL
is valid and does nothing. The object will be still in memory anyway. Beside refactoring your code and not using globally available objects (which the singleton is - basically a global variable) you can hook up the destruction to be tied to Qt's application object's life cycle. To do that you just parent theQObject
you're creating to theQApplication
object like this:class Tray : public QSystemTrayIcon { Q_OBJECT public: Tray(QObject *); ~Tray(); static Tray * instance(); }; Tray::Tray(QObject * parent = NULL) : QSystemTrayIcon(parent) { } Tray::~Tray() { // Don't delete the object. Qt will delete it when the parent goes out of scope. } Tray * Tray::instance() { QCoreApplication * app = QCoreApplication::instance(); Q_ASSERT(app); //< Make sure this is not called before the application was constructed. static Tray * trayInstance = new Tray(app); return trayInstance; }
Again, it's better to not have singletons at all, C++ is not Java and not everything that's written in books about Java (or is common there) is applicable to C++. But the above snippet should work, although I haven't tested it.
-
@kshegunov
I have tested that. It crashed.
If I add tray->hide() after app.exec(). It normal exit.(gdb) bt
#0 0x000000000045821a in QWidget::~QWidget() ()
#1 0x00000000008b79a9 in QBalloonTip::~QBalloonTip() ()
#2 0x00000000008b6cca in QBalloonTip::hideBalloon() ()
#3 0x00000000008c6e33 in QSystemTrayIconPrivate::remove_sys() ()
#4 0x00000000008b7bef in QSystemTrayIcon::~QSystemTrayIcon() ()
#5 0x00000000004124b9 in Tray::~Tray (this=0x2878cc0, __in_chrg=<value optimized out>)
at tray.cpp:26
#6 0x00000000004124f0 in Tray::~Tray (this=0x2878cc0, __in_chrg=<value optimized out>)
at tray.cpp:26
#7 0x0000000000ba1f67 in QObjectPrivate::deleteChildren() ()
#8 0x0000000000ba84b6 in QObject::~QObject() ()
#9 0x000000000042568c in QApplication::~QApplication() ()
#10 0x00000000004121af in QtSingleApplication::~QtSingleApplication (this=0x7fff44a4b6f0,
__in_chrg=<value optimized out>) at ../qtsingleapplication/src/qtsingleapplication.h:65
#11 0x0000000000411c8f in main (argc=1, argv=0x7fff44a4b898) at main.cpp:103 -
@ugiwgh
Can you provide a minimal project that reproduces the crash (e.g. in a git repository somewhere)?That's strange, but what's stranger is this:
#10 0x00000000004121af in QtSingleApplication::~QtSingleApplication (this=0x7fff44a4b6f0, __in_chrg=<value optimized out>) at ../qtsingleapplication/src/qtsingleapplication.h:65
What is this and how it got there?
-
@kshegunov
I have upload it there.
https://github.com/ugiwgh/qsystemtray/
https://github.com/ugiwgh/qsystemtray.git -
@ugiwgh
Well works okay on my machine - no crashes. I even see an empty space in the tray where when I click I get a context menu and I can choose to quit. When I do, everything works as expected and the program exits.However, you shouldn't use
QtSingleApplication
as it seems to be ancient, you'd be better off looking for newer alternatives. -
@kshegunov
Yes. I have disabled QtSingleApplication. And I have update the code on github.
It also crashed, when I choose to quit.#0 0x00007f094a93564a in QWidget::~QWidget() ()
from /opt/qt-5.4.1/5.4/gcc_64/lib/libQt5Widgets.so.5
#1 0x00007f094ac92f0e in ?? () from /opt/qt-5.4.1/5.4/gcc_64/lib/libQt5Widgets.so.5
#2 0x00007f094ac92b25 in ?? () from /opt/qt-5.4.1/5.4/gcc_64/lib/libQt5Widgets.so.5
#3 0x00007f094ac735eb in QSystemTrayIcon::~QSystemTrayIcon() ()
from /opt/qt-5.4.1/5.4/gcc_64/lib/libQt5Widgets.so.5
#4 0x0000000000403625 in PTray::~PTray (this=0x22a1e60,
__in_chrg=<value optimized out>) at para_tray.cpp:26
#5 0x000000000040365c in PTray::~PTray (this=0x22a1e60,
__in_chrg=<value optimized out>) at para_tray.cpp:26
#6 0x00007f0949ba4b1a in QObjectPrivate::deleteChildren() ()
from /opt/qt-5.4.1/5.4/gcc_64/lib/libQt5Core.so.5
#7 0x00007f0949baa259 in QObject::~QObject() ()
from /opt/qt-5.4.1/5.4/gcc_64/lib/libQt5Core.so.5
#8 0x00007f094a8edda8 in QApplication::~QApplication() ()
from /opt/qt-5.4.1/5.4/gcc_64/lib/libQt5Widgets.so.5
#9 0x0000000000403129 in main (argc=1, argv=0x7ffff084acf8) at main.cpp:23 -
@ugiwgh
Well, I don't know man. As I said, I had run your code (the link you provided in your previous post) and it worked okay on my machine, no crashes whatsoever.
If it helps I'm running Debian stretch (4.4.0 kernel) + KDE plasma desktop and I used Qt 5.5.1 for the test run. You may have stumbled on a bug or possibly some platform dependent code is not executing properly, I can't tell really.EDIT:
I only just now saw something:#4 0x0000000000403625 in PTray::~PTray (this=0x22a1e60, __in_chrg=<value optimized out>) at para_tray.cpp:26 #5 0x000000000040365c in PTray::~PTray (this=0x22a1e60, __in_chrg=<value optimized out>) at para_tray.cpp:26
Why do you have 2 calls to your destructor??!
-
Like @kshegunov already wrote using singleton should be avoided when possible and your use case really does't need any singleton.
It sounds that you are rather creating tight coupling between your different classes rather that have a lean path to use system tray.
So what are all these classes doing with the system tray icon and that menu ?