I’m trying to show a simple dialog like this:
if (!aboutDialog)
aboutDialog = new AboutDialog(this);
aboutDialog->exec();
For some reason it doesn’t work (aboutDialog has some pointer although is not initialized), the checking if (aboutDialog == 0) doesn’t work either. I don’t want to create the instance every time, but only once. How do I check this properly?
Thanks!
You need to initialize aboutDialog to 0 explicitly somewhere. Probably in the constructor of the class this member belongs to.
(!aboutDialog)is a correct test.Consider the following:
Try to compile that and run it multiple times. Chances are it will print something different each time. Why? Because
datais uninitialized. It contains garbage. It has as much chance of being a null pointer than 0x1234ABCD or 42.To fix it,
dataneeds to be initialized somewhere. The simplest way here is just to do that inThing‘s constructor like below: