Who wants to have some fun memory debugging?

Sean Kelly sean at invisibleduck.org
Tue May 12 21:43:09 PDT 2009


Robert Fraser wrote:
> Simpler version, sans printf:
> 
> module leak;
> 
> import tango.core.Memory;
> 
> struct Data
> {
>     Data* prev;
>     char[4092] something;
> }
> 
> public void main()
> {
>     Data* data;
>     Data* newData;
>     int i;
>     while(true)
>     {
>         for(i = 0; i < 10_000; i++)
>         {
>             newData = new Data;
>             newData.prev = data;
>             data = newData;
>         }
>         data = null;
>         newData = null;

At this point it's quite possible that you still have a reference to 
newData in a register.  If you want to be sure the collect call below 
works as intended for this test, try adding:

           newData = new Data;

here.

>         i = 0;
>         GC.collect();
>     }
> }

Beyond that, you'll just potentially end up with a bunch of false 
positives, since you have a big static array in the same block as a 
pointer.  But since you aren't actually setting the array contents in 
this test, that clearly isn't a problem here.


More information about the Digitalmars-d-learn mailing list