Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-02-19 15:27:39


Joshua Little wrote:
> Hi again,
> Well I'm making pretty good progress in learning proper way to use
> shared_ptr's but I'm still a little nervous about leaking memory,
> especially if I manage to do something I'm not supposed to do and
> don't recognize that a memory leak may be occuring. I've used the
> overloading new/delete before to help track memory leaks and I was
> wondering if that would be possible with shared_ptrs.
> I tried my old header files with and saw tons of leaks but I think its
> because my New that I use logs the creation to a Tracer class but
> since the Delete happens in the shared_ptr, its not using the
> overloaded Delete that removes stuff from the tracer when its deleted.

Looking at the code you posted, it seems that you are using the Tracer from

    http://www.relisoft.com/book/tech/9new.html

Given that, I don't see why the operator delete overload from the page:

void operator delete (void * p)
{
    if (Tracer::Ready)
        NewTrace.Remove (p);
    free (p);
}

isn't called by shared_ptr.

[...]

> struct deleter {
> void operator() (void * p) const {
> delete p;
> }
> }

Deleting the pointer as void* is a very bad idea, and your compiler should
warn you about it. No destructor will be called, at best. I think that the
above operator delete overload should be sufficient and there should be no
need to use custom deleters.

You might also be interested in the two (underdocumented) source files in
libs/smart_ptr/src. sp_debug_hooks.cpp checks for illegal memory/shared_ptr
operations (but has no leak detection). sp_collector.cpp allows you to
detect unreachable cyclic shared_ptr references (which is pretty much the
only way to leak memory with shared_ptr) by calling

    std::size_t find_unreachable_objects(bool report);

You need to #define BOOST_SP_ENABLE_DEBUG_HOOKS for either of these to work.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net