Thread: Object self-destruction

  1. #16
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    what about pointers to objects created inside DLL's?
    Use a smart pointer that understands the necessary protocol.

    Soma

  2. #17
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Use a smart pointer that understands the necessary protocol.
    But most smart pointers don't care what module they are in when they clean up. I have not encountered any thus far that take any considerations prior to cleaning up save the ref count. It would be nice if they did understand the protocol but alas most do not. So if you return a pointer from a DLL you must be careful to put in mechanisms to guard these pointers so that when it is time to clean up the guard mechanism in place understand that the pointer must be cleaned up in the DLL that created it.

    I don't think it's safe to delete a pointer that was allocated in another module like a DLL.
    It is not safe at all. Pretty much guaranteed heap corruption at some point.
    Last edited by VirtualAce; 01-20-2011 at 05:30 PM.

  3. #18
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I have not encountered any thus far that take any considerations prior to cleaning up save the ref count.
    O_o

    I can introduce you to dozens. Literally. In fact, I can introduce you to an infinite number considering that some of them are templates accepting templates as arguments.

    Consider some of the smart pointers from Loki or Boost. Many, if not all by now, allow you to pass a specific function to use use as the cleanup routine. In other words, if the protocol is "call the member function `void class_type::kill(void)'" you simply code a routine that does exactly that and pass it along to the smart pointer with the raw pointer.

    Some of Microsoft's own supplemental material has offered two that work specifically with some COM protocols taking a pair of functions to use on decrease/increase reference count operations.

    All of this is of course besides the point, it doesn't matter what bizarre protocol a given DLL may demand, a smart pointer that guarantees the protocol will be followed at prescribed points is still far superior to any programmer attempting to do it by hand even if the smart pointer must be coded for this purpose.

    If you happen to be worried about a given coder not properly using the API and the smart pointer correctly you could also wrap the relevant factory API in client code and return the properly crafted smart pointer instead.



    So if you return a pointer from a DLL you must be careful to put in mechanisms to guard these pointers so that when it is time to clean up the guard mechanism in place understand that the pointer must be cleaned up in the DLL that created it.
    This is true whether of not you associate the raw pointer with a smart pointer. The smart pointer just provides some guarantees that it happens at the prescribed places without programmer intervention.



    It is not safe at all. Pretty much guaranteed heap corruption at some point.
    It can be much worse that that. If the relevant deallocating protocol is not followed the destructor may also not be called. That can cause all sorts of immediate problems.

    Soma

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cpjust View Post
    For the vast majority of pointers, I'd agree with that, but what about pointers to objects created inside DLL's?
    That comes down to how you define the interface to the DLL (i.e. what functions it exports, and the way users of the DLL are required to behave).

    The approach I use is that no pointers created inside the DLL are ever passed outside the DLL. That way, the DLL manages every object it creates internally.

    That means, if a function outside the DLL requires data from inside the DLL, that it must provide and manage an appropriate buffer for that data to be copied to.

    The trade-off of this approach is performance as all data must be copied in order to cross the DLL boundary, rather than the DLL simply exposing a pointer to its internal data.

    However, there is no need for a "guard" protocol at all - if a caller provides a pointer, the callee knows is not allowed to deallocate.

    To handle third-party DLLs then I simply write another DLL to act as an intermediary (eg a facade). That works for factories that are in a DLL.

    It's a moot point if the overall application has some performance requirement that makes this approach prohibitive - if performance is that critical, I wouldn't explicitly be using any DLLs in the first place.

    I don't claim this is perfect, but I find it works. It is certainly possible to find corner cases where it won't work, but the trick is avoiding or disallowing corner cases in the first place.
    Last edited by grumpy; 01-21-2011 at 02:27 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Texture Management in OpenGL
    By Brafil in forum Game Programming
    Replies: 13
    Last Post: 07-16-2009, 04:32 PM
  2. Order of Object Destruction
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2006, 08:25 PM
  3. Finding object code
    By kidburla in forum C Programming
    Replies: 3
    Last Post: 11-29-2005, 01:09 PM
  4. 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
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM