none
Heap Corruption Detected need help RRS feed

  • Question

  • Hello,

    maybe someone can help me to get behind this. How can i get the position in the code, where the invalid Memory access happens ?

    HEAP CORRUPTION DETECTED: after Normal block (#1856) at 0x012CD980.

    CRT detected that the application wrote to memory after end of heap buffer.
    Normal located at 0x012CD980 is 236 bytes long.
    Debug Assertion Failed!

    Program: \Temp\App.exe
    File: MainPage.cpp
    Line: 123

    Directly after that an "Exception thrown at 0x40023127 in App.exe: Microsoft C++ exception: std::system_error at memory location 0x008A43B8" comes up.

    Thanks

    Holger


    Thursday, March 19, 2020 10:58 PM

Answers

  • I could be wrong, but it doesn't look like you are including the correct crtdbg.h files.  But, you have replace too much of your file path with PAT... to be able to tell.  I like that you are doing that, but keep enough of it so that we know where the file is.

    Your last sentence might tell I tall - you should be using the file from the Windows CE SDK.

    But enough about that - your subject for this thread is "Memory Leak Detected in SW" but then you proceed to tell us about "HEAP CORRUPTION DETECTED".  These are very different things.  A memory leak is when the app allocates memory, but then doesn't free it when it is finished using the memory.  Heap corruption has to do with using a pointer to heap that you shouldn't be touching.

    I am not sure that Michel's response will help you - he will probably jump in if I am wrong.  My read of the page that he linked to is that it is for memory leaks, which you don't seem to have.

    What you probably have is something like the following (and there are other options as well):

    1. An uninitialized pointer
    2. A pointer to heap that you allocated, but then deallocated.  So now it points to a location that you were able to write to, but now you can't.
    3. A pointer that has itself become corrupt, like a local pointer that was overwritten by an array out of bounds on the stack.
    4. An array out of bounds condition for an array that is in heap

    Finding those is not going to be easy.  I assume that this takes some time and luck to reproduce, is that correct?  Does it need to be in the field to reproduce?

    If possible, I would instrument the code to narrow down where it occurs.


    Bruce Eitman
    Senior Enginer
    Bruce.Eitman AT Synopsys DOT com
    My BLOG http://geekswithblogs.net/bruceeitman
    I work for
    Synopsys

    • Marked as answer by HRaschke1979 Saturday, March 21, 2020 8:35 PM
    Friday, March 20, 2020 5:53 PM
    Moderator
  • The link I posted shows how to modify the output to show the __FILE__ and __LINE__ number of where the heap corruption is detected.

    There is also the Remote Tool Resource Leak Detector and there is DevHealth; all tools to help you find memory leaks and heap corruptions.


    Good luck,

    Michel Verhagen, eMVP
    Check out my blog: https://guruce.com/blog

    GuruCE
    Microsoft Embedded Partner
    NXP Gold Partner
    https://guruce.com
    Consultancy, training and development services.

    Interested in WEC on i.MX6?
    Get the only 100% stable and best performing i.MX6 BSP for WEC7 and WEC2013 here: https://guruce.com/imx6

    • Marked as answer by HRaschke1979 Saturday, March 21, 2020 8:35 PM
    Saturday, March 21, 2020 12:13 AM
    Moderator

All replies

  • Check out this link: https://www.codeguru.com/cpp/w-p/ce/article.php/c3513/Memory-leak-detection-for-WinCE.htm


    Good luck,

    Michel Verhagen, eMVP
    Check out my blog: https://guruce.com/blog

    GuruCE
    Microsoft Embedded Partner
    NXP Gold Partner
    https://guruce.com
    Consultancy, training and development services.

    Interested in WEC on i.MX6?
    Get the only 100% stable and best performing i.MX6 BSP for WEC7 and WEC2013 here: https://guruce.com/imx6

    Friday, March 20, 2020 3:18 AM
    Moderator
  • Hello, 

    thanks for your Reply, but unfortunately it won't compile.

    Added necessary #include "crtdbg.h" in winmain.cpp and "Activation Code":

    And added the crtdbg.h file to the directory of my app. In Visual Studio this file is opened if i select it to open from Code.

    1>PATH...App\App\crtdbg.h(86): could be 'void *operator new(unsigned int,unsigned short *,int)'

    1>          while trying to match the argument list '(unsigned int, const wchar_t [75], int)'
    1>PATH...App\App\crtdbg.h(127): error C2664: 'wcscmp' : cannot convert parameter 1 from 'unsigned short *' to 'const wchar_t *'
    1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>PATH...App\App\crtdbg.h(129): error C2664: 'wcslen' : cannot convert parameter 1 from 'unsigned short *' to 'const wchar_t *'
    1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>PATH...App\App\crtdbg.h(130): error C2664: 'wcscpy' : cannot convert parameter 1 from 'unsigned short *' to 'wchar_t *'
    1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>PATH...sdk\crt\Include\xatomic.h(118): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(138): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(146): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(153): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(158): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(230): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(296): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(367): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(489): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(569): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(645): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(715): error C3861: '_ASSERTE': identifier not found
    1>PATH...sdk\crt\Include\xatomic.h(785): error C3861: '_ASSERTE': identifier not found

    FYI, in my sdk there are another crtdbg.h file…

    Holger

    Friday, March 20, 2020 1:30 PM
  • I could be wrong, but it doesn't look like you are including the correct crtdbg.h files.  But, you have replace too much of your file path with PAT... to be able to tell.  I like that you are doing that, but keep enough of it so that we know where the file is.

    Your last sentence might tell I tall - you should be using the file from the Windows CE SDK.

    But enough about that - your subject for this thread is "Memory Leak Detected in SW" but then you proceed to tell us about "HEAP CORRUPTION DETECTED".  These are very different things.  A memory leak is when the app allocates memory, but then doesn't free it when it is finished using the memory.  Heap corruption has to do with using a pointer to heap that you shouldn't be touching.

    I am not sure that Michel's response will help you - he will probably jump in if I am wrong.  My read of the page that he linked to is that it is for memory leaks, which you don't seem to have.

    What you probably have is something like the following (and there are other options as well):

    1. An uninitialized pointer
    2. A pointer to heap that you allocated, but then deallocated.  So now it points to a location that you were able to write to, but now you can't.
    3. A pointer that has itself become corrupt, like a local pointer that was overwritten by an array out of bounds on the stack.
    4. An array out of bounds condition for an array that is in heap

    Finding those is not going to be easy.  I assume that this takes some time and luck to reproduce, is that correct?  Does it need to be in the field to reproduce?

    If possible, I would instrument the code to narrow down where it occurs.


    Bruce Eitman
    Senior Enginer
    Bruce.Eitman AT Synopsys DOT com
    My BLOG http://geekswithblogs.net/bruceeitman
    I work for
    Synopsys

    • Marked as answer by HRaschke1979 Saturday, March 21, 2020 8:35 PM
    Friday, March 20, 2020 5:53 PM
    Moderator
  • Hi Bruce,
    yes, you are totally right, it's my fault! The correct Subject should be correctly named "Help on HEAP CORRUPTION DETECTED".
    My SDK contains a crtdbg.h file and with this original file it's possible to get a memory leak Output, During that i get this "HEAP CORRUPTION DETECTED" message. There are some leaked memory blocks which i want to find out and fix them.

    Ok, i guess there is no option/ tool to easily locate the position where the "bad" access  takes place? Maybe you have some tips?

    Thanks

    Holger

    Friday, March 20, 2020 11:18 PM
  • The link I posted shows how to modify the output to show the __FILE__ and __LINE__ number of where the heap corruption is detected.

    There is also the Remote Tool Resource Leak Detector and there is DevHealth; all tools to help you find memory leaks and heap corruptions.


    Good luck,

    Michel Verhagen, eMVP
    Check out my blog: https://guruce.com/blog

    GuruCE
    Microsoft Embedded Partner
    NXP Gold Partner
    https://guruce.com
    Consultancy, training and development services.

    Interested in WEC on i.MX6?
    Get the only 100% stable and best performing i.MX6 BSP for WEC7 and WEC2013 here: https://guruce.com/imx6

    • Marked as answer by HRaschke1979 Saturday, March 21, 2020 8:35 PM
    Saturday, March 21, 2020 12:13 AM
    Moderator
  • Yes, you mean #define new new(_T(__FILE__), __LINE__). I'll try it out, hopefully to find it.

    I've tried to use the leak detector provided with the Remote Tools Shell, but for me it wasn't clear how to understand it's output and also not all Tools of the Remote Tools Shell are working. So i found the possibility with crt. 

    DevHealth is unknown for me. Found that: http://download.microsoft.com/download/2/4/A/24A36661-A629-4CE6-A615-6B2910A1367A/DevHealth%20Memory%20Usage%20Tool%20for%20Windows%20Embedded%20Compact%207.pdf

    I'm using WEC 2013, is it possible to use DevHealth then ? And there is also a Tool called AppVerifier, but it's not usable with 2013, am i correct?

    Thanks

    Holger

     

    • Marked as answer by HRaschke1979 Saturday, March 21, 2020 8:35 PM
    • Unmarked as answer by HRaschke1979 Saturday, March 21, 2020 8:35 PM
    Saturday, March 21, 2020 8:34 PM
  • Correct, change that #define to include the __FILE__ and __LINE__ macros.

    DevHealth is great and of course also available in WEC2013. Exe here: \WINCEX00\public\common\oak\target (then in your target folder, like \armv7\retail).

    Application Verifier was what the Resource Leak Detector was called in CE 6.0.


    Good luck,

    Michel Verhagen, eMVP
    Check out my blog: https://guruce.com/blog

    GuruCE
    Microsoft Embedded Partner
    NXP Gold Partner
    https://guruce.com
    Consultancy, training and development services.

    Interested in WEC on i.MX6?
    Get the only 100% stable and best performing i.MX6 BSP for WEC7 and WEC2013 here: https://guruce.com/imx6

    Sunday, March 22, 2020 6:22 AM
    Moderator