Tips on memory management with C++ and Qt
Jeff Mitchell
mitchell at kde.org
Mon Feb 16 14:52:08 CET 2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Maximilian Kossick wrote:
>> 3)
>> Never, ever, use private d-pointer classes in QObject derived subclasses:
>>
>> What can happen is that you do a "delete d;" in your destructor, and
>> then Qt goes ahead and auto-deletes other QObject pointers contained
>> in the private class again, through means of its automatic deleting of
>> QObjects with a parent Object. -> <BOOOOM>
>>
>> Read more about this topic in Michael Pyne's interesting blog article:
>>
>> http://www.purinchu.net/wp/2009/02/04/another-programming-tidbit/
>
> This is not correct. Using d-pointers in QObjects is perfectly fine as
> long as the destructors are written correctly. The referenced article
> only says that one has to be careful about deleting a private
> d-pointer when other classes keep pointers to members of the private
> class around.
Right. There are two keys to doing this safely:
1) Always write your own destructor. NEVER use QObject's default
destructor in this case (you *can* do it safely, but you have to be very
careful, keeping #2 in mind...).
2) Control access to members of the private d-pointer class as much as
possible.
- --Jeff
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
iEYEARECAAYFAkmZb4cACgkQANYdqNCuGCWWOwCggdGosSp38p3c8xijJA0WIovc
nFMAn2Lgu9VKH3m/cwH6TBNOD9Ra+pb5
=/rK/
-----END PGP SIGNATURE-----
More information about the Amarok-devel
mailing list