none
_CrtMemDifference works for new but not for HeapAlloc; why? RRS feed

  • General discussion

  • Hi,

    I wrote the following two code pieces ( sample 1 & sample 2 ). Executing them leads to the output 1 & output 2.
    Does somebody know why the memory allocted with HeapAlloc isn't detected by the _CrtMemDifference function?

    It should be detected and therefore the output 2 should tell me that 15 bytes has been allocted.
    But unfortunatly this isn't the case. Does somebody know why?

    Thanks for your help in advance.


    sample 1:
    ==========
       _CrtMemState S1, S2, SDiff;
       _CrtMemCheckpoint( & S1 );

       int *Pointer = new int;

       _CrtMemCheckpoint( & S2 );
       int AreDifferent = _CrtMemDifference( &SDiff, &S1, &S2 );
       _CrtMemDumpStatistics( & SDiff );

       
    output 1:
    =========
       0 bytes in 0 Free Blocks.
       4 bytes in 1 Normal Blocks.
       0 bytes in 0 CRT Blocks.
       0 bytes in 0 Ignore Blocks.
       0 bytes in 0 Client Blocks.
       Largest number used: 0 bytes.
       Total allocations: 4 bytes.   

       
       
       
    sample 2:
    ==========   
       _CrtMemState S1, S2, SDiff;
       _CrtMemCheckpoint( & S1 );

       void *Pointer = (void*) HeapAlloc( HeapHandle, HEAP_ZERO_MEMORY, 15 );

       _CrtMemCheckpoint( & S2 );
       int AreDifferent = _CrtMemDifference( &SDiff, &S1, &S2 );
       _CrtMemDumpStatistics( & SDiff );
       
    output 2:
    =========
       0 bytes in 0 Free Blocks.
       0 bytes in 0 Normal Blocks.
       0 bytes in 0 CRT Blocks.
       0 bytes in 0 Ignore Blocks.
       0 bytes in 0 Client Blocks.
       Largest number used: 0 bytes.
       Total allocations: 0 bytes.   

       
       


      
    Wednesday, March 11, 2009 5:43 PM

All replies

  • Maybe the _CrtMemDifference() function applies only to the application process heap.

    Try with HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 15) just to see if there is any difference in the output.

     

    Wednesday, March 11, 2009 6:04 PM
  • Ok, I did the test with:
               HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 15);

    The result is the same as above.
    ==> The error is somewhere else. Further idea?



    Wednesday, March 11, 2009 6:15 PM
  • The _CrtMemCheckPoint() function relies on information maintained by the CRT debug heap allocator.  HeapAlloc() is a native Windows API function to allocate memory.  It has no built-in facility to provide the kind of info that _CrtmemCheckPoint() requires to make a snapshot.

    Avoid using HeapAlloc() directly, the CRT already does.  But with the bells and whistles you need to debug allocation problems.

    Hans Passant.
    Wednesday, March 11, 2009 6:20 PM
    Moderator
  • ok, thanks
    Wednesday, March 11, 2009 7:00 PM