Telling a shared_ptr not to delete object?

This is a discussion on Telling a shared_ptr not to delete object? within the C++ Programming forums, part of the General Programming Boards category; Hello! Is there any way to tell a shared_ptr not to delete the object it's pointing to if the shared_ptr ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Telling a shared_ptr not to delete object?

    Hello!

    Is there any way to tell a shared_ptr not to delete the object it's pointing to if the shared_ptr is destroyed and it is the only shared_ptr pointing to that object?

    For example: I have a class 'image' containing a shared_ptr object, pointing to the place where the pixel data is stored. But sometimes that data is created to be used by a SDL_Surface in the first place, not by an image object, but is later pointed to from such an object. Thus, it shouldn't be destroyed by such an object either. I have to this point had a flag set in the object to tell if it is responsible for deleting[] the data or not when it's destroyed. Hm, I should probably use shared_array in this case.

    If I set the shared_ptr to point to NULL, will it still keep count of the number of shared_ptr:s pointing to that place and try to delete the object at that position when it's the last shared_ptr being destroyed?
    Come on, you can do it! b( ~_')

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    You could create a dummy shared_ptr that you aren't really using so that there is still one pointing to it when all of the shared_ptr's you're actually using are destroyed.

    Doesn't that defeat the purpose of a shared_ptr though?

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Kernel Sanders View Post
    Doesn't that defeat the purpose of a shared_ptr though?
    That's an excellent question. If you want an object to exist after the last reference to it has gone away, then it doesn't sound like shared_ptr is the type to be using for it.
    Last edited by whoie; 08-15-2008 at 06:53 PM.

  4. #4
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    I guess I could always create my own class, or even just add a counter directly into the image class. shared_ptr seems to be a class of quite low weight complexity, so it wouldn't be hard to reproduce it.
    Come on, you can do it! b( ~_')

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by TriKri View Post
    I guess I could always create my own class, or even just add a counter directly into the image class. shared_ptr seems to be a class of quite low weight complexity, so it wouldn't be hard to reproduce.
    What about auto_ptr? I believe it has a release member function that basically releases the memory from automatic destruction. You could carry it around in the auto_ptr until you hand it off, "release it," to the SDL system for management.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Well, I see two solutions here.

    One, and conceptually the better one, is to associate a shared_ptr with the SDL_Surface. If the SDL_Surface references the image data, the associated shared_ptr does so, too. Thus, you keep the reference counting correct.

    Two, when you create the shared_ptr and an SDL_Surface references the image data, you give it a noop deleter.
    Code:
    struct noop_deleter { void operator ()(void *) {} };
    shared_ptr<Whatever> sp(data, noop_deleter());
    However, I can't remember if shared_ptr supports set_deleter, and if it doesn't, this means that you cannot change whether an SDL_Surface refers to the data during the lifetime of the data.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Deleting object after list removal (C++ visual studio)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-20-2005, 06:06 PM
  5. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21