[ale] C++ Debuggery and the Path of Destruction

Bjorn Dittmer-Roche bjorn at sccs.swarthmore.edu
Fri Sep 24 22:45:54 EDT 2004


On Fri, 24 Sep 2004, Barry Rountree wrote:

> On Friday 24 September 2004 08:41 pm, John Mills wrote:
>> ALErs -
>>
>
>> Naturally, I got a bit too clever
>
> It's almost certainly going to be easier to rewrite it as something less
> clever, but hey, where's the fun in that?
>
> ;-)
>
>> Any suggestions how to track this self-administered wedgie down?
>
> Any chance of posting some code?
>
> I finally gave up on getting memory management right the first (second,
> nth...) time and started populating my code with debug statements to the
> effect of:
>
> __FILE__::__LINE__ is now creating a __WHATEVER__, address 0xabcddcab
> __FILE__::__LINE__ is now deleting a __WHATEVER__, address 0xabcddcab


This is handy. My trick is for each object that has a malloc or new, that 
object is responsible for deleting/free ing the data as well. Paired with 
a counter to keep track of mallocs in each object is handy and I tend to 
catch memory leaks fast:

for example:

constructor()
{
    malloc _whatever_ ;
    ++malloccount;
}
method1()
{
    malloc _another_whatever;
    ++malloccount;
}
method2()
{
    free _anbother_whatever;
    --malloccount;
}

destructor()
{
    free _whatever_ ;
    --malloccount;

    if( malloccount < 0 )
       throw FatalException( "Double free!" );
    else if( malloccount > 0 )
       throw FatalException( "Memory Leak!" );
}


My pseudo-code here is awful, but you get the idea....

 		bjorn

-------------------
Bjorn Dittmer-Roche
XO Audio LLC

http://www.xowave.com
http://www.xoaudio.com



More information about the Ale mailing list