Boost logo

Boost Users :

Subject: Re: [Boost-users] [SmartPtr] Memory Leak Perhaps
From: Benjamin Sobotta (mayday_at_[hidden])
Date: 2010-09-20 04:50:36


Hi!

shared_ptr to this are dangerous. As soon as you do that twice you already
have a problem.

If you want to create shared_ptrs to this, use shared_from_this.

http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/enable_shared_from_this.html

Cheers,

Benjamin

On Monday 20 September 2010 02:27:22 am Hossein Haeri wrote:
> Dear Igor,
>
> Thank you very much for your suggestions.
>
> > It's not a memory leak, but memory corruption: it seems
> > that you try to free some memory block twice.
>
> Yes, I did think about that one too. And, that's why I monitored the
> use_count() of my shared_ptr.
>
> > > On the other hand, the problem only occurs upon
> >
> > insertion of a SPECIFIC entry into my container. Strangely
> > enough, if I delete that specific entry manually before the
> > end of my program, everything goes fine! I even checked the
> > count of that specific shared_ptr and it was 1, i.e., I'm
> > not deleting anybody else's memory. I am really confused and
> > left with no clue... Any suggestions?
> >
> > Maybe you accidentally store a raw pointer to an object
> > managed by shared_ptr.
>
> Well, my only suspicion is about the fact that I store "this" pointers in
> my shared_ptr's. However, those objects are dynamically allocated by
> myself, namely, they are not of automatic storage type anyway. In other
> words, my row pointer and "this" -- which themselves are different objects
> -- both point to the same memory dynamically allocated by myself.
> Furthermore, my following observation goes against this guess: (This is
> available above too.)
>
> > > Strangely enough, if I delete that specific entry manually
> > > before the end of my program, everything goes fine!
>
> And, by this, I meant that if I call container.erase(bad_entry), everything
> goes fine. Now, if bad_entry accidentally points to somebody else's
> resources, and this causes trouble for the container clean-up, then, why
> can I erase it from my container and dismiss the nasty exceptions?
>
> > Or maybe your object internally allocates and frees some
> > memory, which is also managed in some other place...
>
> Actually it does allocate memory for other objects (which are stored in the
> same container). But, I have tested that and the result is that everything
> -- including this extra allocation -- works perfectly fine JUST if I drop
> the insertion of this very specific object in the same container.
>
> > I guess it's worth searching calls to "delete" and "free"
> > in your code.
>
> Thank God there are only very few instances of delete in my code. I've
> checked them up and none of them got touched over my debug sessions.
>
> Am I missing anything? I would especially be keen on knowing whether you
> see any problems in storing "this" pointers of my own dynamically allocated
> objects in shared_ptr's.
>
> TIA,
> --Hossein
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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