Results 1 to 3 of 3

Thread: QSharedPointer - does it work well when constructed independently?

  1. #1
    Join Date
    Jul 2010
    Posts
    12
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSharedPointer - does it work well when constructed independently?

    Suppose there is one object in my program, and several shared pointers for it. Will they count references to the object, if they are not created with assigning value to one another, but by constructing each independently from simple pointer.

    Like, this:
    Qt Code:
    1. MyObject *object = new MyObject;
    2. QSharedPointer <MyObject> a, b, c, d;
    3. a = QSharedPointer (object);
    4. b = QSharedPointer (object);
    5. c = QSharedPointer (object);
    6. d = QSharedPointer (object);
    To copy to clipboard, switch view to plain text mode 
    instead of this:
    Qt Code:
    1. MyObject *object = new MyObject;
    2. QSharedPointer <MyObject> a, b, c, d;
    3. a = QSharedPointer (object);
    4. b = a;
    5. c = b;
    6. d = c;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSharedPointer - does it work well when constructed independently?

    The first example won't work since the shared pointers have no knowledge of each other and object will get deleted four times over.

  3. The following user says thank you to spud for this useful post:

    Septi (23rd July 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    12
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSharedPointer - does it work well when constructed independently?

    Uh-oh, now I have to redesign my code a little. I thought they were counting references in some sort of static structure ((

    Well, spud, thanks for clarifying it.

Similar Threads

  1. QSharedPointer of type void
    By HERC in forum Qt Programming
    Replies: 0
    Last Post: 12th May 2010, 12:59
  2. Problem with QSharedPointer
    By weaver4 in forum Newbie
    Replies: 2
    Last Post: 19th April 2010, 14:22
  3. QSharedPointer - how to prevent delete?
    By Piskvorkar in forum Qt Programming
    Replies: 7
    Last Post: 31st March 2010, 15:46
  4. QSharedPointer vs Boost::tr1::shared_ptr()
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 16:48
  5. Replies: 3
    Last Post: 7th August 2009, 13:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.