Boost logo

Boost Users :

Subject: Re: [Boost-users] [SmartPtr] Memory Leak Perhaps
From: Hossein Haeri (powerprogman_at_[hidden])
Date: 2010-09-19 20:27:22


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 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