View Full Version : how to check for NULL reference?

14th December 2009, 00:52
In my project I'm having a hash member of :

QHash<int,graphDialog *> dialogs;

where graphDialog class is a member of QDialog.

while I'm iterating through this hash, I want to make sure that the QDialog member I'm iterating is not null ( not destroyed ).
How do I do that?
I tried the following :

QHashIterator<int, graphDialog *> i(dialogs);
while (i.hasNext()) {
if(i.value() != NULL ){ ... }
although when the dialog is destroyed it is not equal to NULL and passes through if :/

then I used the so-called guarded pointer class QPointer and did the following:

QHashIterator<int, graphDialog *> i(dialogs);
while (i.hasNext()) {
QPointer<graphDialog> test = i.value();
if(!test){ .... }
however this time I get a segmentation error while the QPointer is being created.
I thought this class was meant to build for this mission, but it seems to suffer from the same issue (null reference).

also there is nothing like isClosed,isDestroyed,isNull for QDialog if I'm not mistaken ...
So how do I check for it , please help

14th December 2009, 02:14
Gaurd Pointer provides an API QPointer::isNull() for NULL check. Try using this API.
I hope it works. :)


14th December 2009, 02:23
You should use

QHash<int, QPointer<graphDialog> > dialogs;