[Digikam-devel] Re: delete does not reset the pointer in GCC (4.6.0)
Francesco R.
francesco at pnpitalia.it
Wed Jun 15 14:16:30 BST 2011
On Wednesday 15 June 2011 14:17:37 Gilles Caulier wrote:
> 2011/6/15 Andi Clemens <andi.clemens at gmx.net>
>
> > Right, we are still using too many raw pointers.
> > Qt, C++ stdlib and boost have many things to handle pointers
> > automatically.
>
> Yes. this one is exactly that we need :
>
> http://doc.qt.nokia.com/4.7/qpointer.html
>
> > See Item #13 in "Effective C++": Use objects to manage resources
> > A great book, everyone should read it...
>
> Do you have an url ?
This may be of interest:
http://labs.qt.nokia.com/2009/08/25/count-with-me-how-many-smart-pointer-
classes-does-qt-have/
for some more fun I've converted the previous example to use QPointer and
QObject, it's at the end of the mail.
>
> > Anyway sure auto_ptr and friends will "slow" down execution, but with
> > all
> > the checks we do anyway, I guess it will not be noticeable.
> > It is still much faster than garbage collection...
>
> Perhaps we will need to patch code about to use QPointer everywhere when
> it's necessary...
>
> Gilles
>
> > Andi
> >
> >
> > On Wed, 15 Jun 2011 11:55:50 +0200, Marcel Wiesweg
> > <marcel.wiesweg at gmx.de>
> >
> > wrote:
> > >> a few months ago, maybe 2 years.
> > >> I know that I always added
> > >> xyz = 0;
> > >> to my code before, but then it was removed again because it is not
> > >> neccessary with modern compilers (I don't know who said that).
> > >
> > > When googling and reading through the usual sources (stackoverflow
> > > etc.) I see noone saying that any compiler sets a pointer to 0, I
> > > would even guess delete should _not_ change the value of the passed
> > > pointer unless the C++ standard says so.
> > > There are the usual stories not to use raw pointers and use concepts
> > > to
> > > make sure you are always sure never to use deleted pointers again,
> > > but
> > > in the end, if you are playing with raw pointers (which we are doing
> > > still in too many areas IMO, but getting less) and need to delete
> > > and
> > > reuse the variable, be sure to set it to 0.
> > >
> > > Marcel
#include <iostream>
#include <QtCore/QObject>
#include <QtCore/QPointer>
/*
g++-4.5.2 -I /usr/include/qt4 -L/usr/lib64/qt4 -lQtCore -O0 -ggdb3 -fdump-
tree-gimple b.cpp && mv b.cpp.004t.gimple b.cpp.004t.gcc45.gimple
g++-4.6.0 -I /usr/include/qt4 -L/usr/lib64/qt4 -lQtCore -O0 -ggdb3 -fdump-
tree-gimple b.cpp && mv b.cpp.004t.gimple b.cpp.004t.gcc46.gimple
# to compare use:
sed -i -e 's:[0-9][0-9][0-9][0-9][0-9]:99999:g' b.*.gimple
*/
using namespace std;
// QPointer<QLabel> label = new QLabel;
// label->setText("&Status:");
// ...
// if (label)
// label->show();
main()
{
QPointer<QObject> p = new QObject;
p->dumpObjectInfo();
startLookingAtMe:
delete p;
// p = 0; // omitting DOES NOT crash
delete p;
if (p) delete p;
endLookingAtMe:
cout << (0/0); // my easy brakpoint
}
More information about the Digikam-devel
mailing list