dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Heap corruption error in C program on VS2010 when freeing memory

  1. #1
    Join Date
    Feb 2012
    Posts
    5

    Heap corruption error in C program on VS2010 when freeing memory

    Hi all;
    I have written a C program on Vs2010. My program is using linked list to save and use the data.
    It is generating the correct results. I am having problem in freeing the linked list using free() function. Each linked list node is created using the malloc() function. If i set debug x64 on windows 7 and VS2010 option and system throws an error
    Debug Assertion failed
    expression _CrtIsValidHeapPointer(pUserData)

    The while loop that generated the error is as follows and the source line of error is marked as #
    p=Lsol; //Lsol is list pointer
    while(p!=NULL)
    {
    //delete members first
    free((p->xInf1));
    free((p->xSup1));
    free((p->B1));
    //now delete node itself
    q=p->next; #
    free(p);
    p=q;
    }
    When i execute my program in Release x64 mode then following error i am getting

    Unhandled exception at 0x77986cd2 (ntdll.dll) in BernsteinBasicAlgoSerial8.exe: 0xC0000374: A heap has been corrupted.

    I have checked my code several times but unable to locate any error in it. I have also checked that the list which i am going to delete can be traversed fully. But there is problem in freeing memory of 23 rd node and list has 128 nodes.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Heap corruption error in C program on VS2010 when freeing memory

    1) Please use code tags when posting code. The code you posted is almost unreadable.

    2) The code that you did post gives no information. The reason is that heap corruption could have occurred long before that code is even executed.

    To properly know what is going on, you have to give us a complete program that duplicates the error. There is nothing wrong with calling the free() function -- what is wrong is if you are mismanaging pointers, corrupting the heap due to the pointer mismanaging, and then you're calling free() on invalid pointers.
    It is generating the correct results. I am having problem in freeing the linked list using free() function. Each linked list node is created using the malloc() function. If i set debug x64 on windows 7 and VS2010 option and system throws an error
    Debug Assertion failed
    expression _CrtIsValidHeapPointer(pUserData)
    As I stated, your issue is this:
    Code:
    SomePointer* p = malloc( whatever );
    //....
    //...You are messing up and corrupting code
    //...
    free( p );
    It is the stuff in-between the calls to malloc() and free() that are in question, and not the calls to malloc() and free() that are the problem.
    I have checked my code several times but unable to locate any error in it.
    And there is no way we can help you if we don't see this code for ourselves.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,651

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Welcome to codeguru

    Somewhere in your code you have written more data than you have allocated space for or maybe you even write at a completely invalid place.

    Unless you buy something like BoundsChecker you don't have that many options besides a thorough test & debug of your code.

    One good start is to change so each node allocates several times more than it needs, pre-fill the allocated space with a known pattern and then run in the debugger to check where the out of bounds write occur.

    If possible, the absolute easiest way is to switch to C++ and use stl containers instead.

    Please use code tags when posting code [code]Paste your code here[/code]
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    Jan 2012
    Location
    Belleville, Michigan, USA
    Posts
    13

    Re: Heap corruption error in C program on VS2010 when freeing memory

    STL containers are nice in general, but come with their own set of problems.

    BoundsChecker helps in this kind of case by alerting you to invalid heap operations earlier on, letting you know that doing certain things can lead to later corruption. Not that it catches every possible way to mess things up, but it catches quite a few of them.

    To be fair, there are other products out there. None of them are perfect. All of them help to some extent, though it costs money to make use of them . . . with the exception of the so-called Application Verifier that comes with Visual Studio. That tool is free, though it isn't very user-friendly.

    <BLATANT-ADVERTISEMENT>
    It is possible to buy a limited time (99 days) for BoundsChecker for $99, which is far cheaper than the normal minimum license cost of $695. See http://www.componentsource.com/news/...b-edition.html for more information.
    </BLATANT-ADVERTISEMENT>

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by rpapo View Post
    STL containers are nice in general, but come with their own set of problems.
    Such as?

    To be fair, there are other products out there. None of them are perfect. All of them help to some extent, though it costs money to make use of them . . .
    If a program is not written using Windows-specific coding, and if a programmer has access to a Linux environment, valgrind is free and extremely helpful.

  6. #6
    Join Date
    Jan 2012
    Location
    Belleville, Michigan, USA
    Posts
    13

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by Lindley View Post
    Such as?
    For starters, it is very easy to write sloppy code with STL, where you wind up chewing through vast amounts of memory (and CPU cycles) trying to accomplish something.

    Mind you, I use STL when I want to do something quickly. But when I have to get down to tuning an application for speed or capacity, or both, one of the first things to get removed and replaced is STL. STL constructs are incredibly useful and flexible, but also quite wasteful.

    Of course, a good part of the blame has to rest with how much it makes use of the heap.

    FWIW, my notions of fast and efficient are at least an order of magnitude higher than most people's. Java and .NET are piggishly slow monstrosities to my point of view, though they are the right tools for certain kinds of jobs.

    I would love to be able to spend the time to make BoundsChecker faster . . . 8-/

    <RANT>
    Case in point. Right now I am working on a parser of Japanese text, on my own time, at home. The parser has just got done loading the dictionary file and generating conjugations of such words as can be conjugated. There are basic word definitions (157,534 strings, 12,581,538 bytes), plus all the associated words and their conjugations, a total of 7,078,456 strings, 134,059,906 bytes. The application has not done much of anything with these strings except to organize them in a map of dictionary definitions with associated sets of words. Yet the application already consumes 1.3Gb of RAM! That's ten times the memory consumed by just the strings themselves. And it hasn't even created the master search index yet, after which the application will consume over 8Gb of RAM. The waste of memory may just be something stupid I missed, but the structure of STL does not really help you to avoid mistakes, but rather helps you to get the job done in spite of your mistakes. That's a worthy goal for quick and dirty programming, but not for fast, efficient coding for the long haul of production use.

    And this isn't the first example of such behavior I've seen, of my own or other's code.
    </RANT>
    Last edited by rpapo; February 3rd, 2012 at 05:24 PM.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by rpapo View Post
    For starters, it is very easy to write sloppy code with STL, where you wind up chewing through vast amounts of memory (and CPU cycles) trying to accomplish something.

    Mind you, I use STL when I want to do something quickly. But when I have to get down to tuning an application for speed or capacity, or both, one of the first things to get removed and replaced is STL. STL constructs are incredibly useful and flexible, but also quite wasteful.
    If you're doing things like copying containers around, then yeah, you're going to be wasting time. And constantly bringing a vector into and out of scope in an inner loop is stupid. But it's pretty easy to learn to avoid doing that stuff, and once you do, STL containers are often within 10&#37; of the speed of hand-optimized code, especially now that they support move semantics.

    Keep in mind, that assumes all unnecessary bounds-checking is DISABLED, which does not happen by default in Microsoft's implementation (even under Release!).

    I would be interested to see an example of simple code using STL containers that is significantly slower than hand-optimized code doing the same thing.

    Of course, a good part of the blame has to rest with how much it makes use of the heap.
    STL containers are able to use whatever allocators you like. I think someone on here designed a stack allocator for them awhile back. It had limitations but it worked for the most part.
    Last edited by Lindley; February 3rd, 2012 at 05:21 PM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by rpapo View Post
    For starters, it is very easy to write sloppy code with STL, where you wind up chewing through vast amounts of memory (and CPU cycles) trying to accomplish something.
    Show us where it is difficult to write sloppy code with any aspect of C++. .

    The very nature of C++ makes it easy to write sloppy code. Passing objects by value is one such example, and passing objects by value have nothing to do with STL, but with the inexperience of the programmer. If that programmer passes vectors around by value, then that same inexperienced programmer will pass Widget objects by value (assuming a Widget is a user-defined class and not an STL class).
    Mind you, I use STL when I want to do something quickly.
    And others use it so their programs work without bugs.
    But when I have to get down to tuning an application for speed or capacity, or both, one of the first things to get removed and replaced is STL.
    You left out that when you start replacing STL with your own code, you are risking bugs and the maintenance costs goes up due to the amount of code you now have to take care of. A "fast" application is useless if it doesn't work or if it is practically impossible to update or maintain without breaking code.

    Also, as Lindley pointed out, I have yet to see an application written using STL containers perform so much slowly as you've mentioned. Either you're doing things wasteful, or you're not timing fully optimized builds. I would like to see a test case where STL is slow, and the equivalent code that isn't STL is so many magnitudes faster.
    STL constructs are incredibly useful and flexible, but also quite wasteful.
    Show us a test case of this of this "wasteful" construct, and code the same thing without STL to show us what isn't wasteful.
    Of course, a good part of the blame has to rest with how much it makes use of the heap.
    Write your own allocator and use that instead of the default allocator for the STL containers.
    Case in point. Right now I am working on a parser of Japanese text, on my own time, at home.
    We have no idea what components of STL you're using, whether your design is inefficient, etc. so unless we see for ourselves what you're doing, you can't make your case and blame STL for the issues you're having.
    The waste of memory may just be something stupid I missed,
    Exactly my point. Let's all see this code, so that we can conclude that yes, STL isn't the right choice, or no, your design is flawed and/or your code is doing things inefficiently.
    That's a worthy goal for quick and dirty programming, but not for fast, efficient coding for the long haul of production use
    There are many C++ applications, probably you use them now, that use STL containers and algorithms heavily. They are hardly "quick and dirty" programs.

    Also, STL isn't just about containers -- what about the algorithm functions? Do you write your own sorting code, partitioning code, etc?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 4th, 2012 at 06:43 AM.

  9. #9
    Join Date
    Jan 2012
    Location
    Belleville, Michigan, USA
    Posts
    13

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Sleeping on a topic is a good way to cool down.

    (1) Fully one half of the wasted memory in the text parser I mentioned earlier turned out to be due to copying strings into the index as keys, rather than simply referencing them since what was being indexed was not going to change during the life of the index. So, instead of requiring about 7Gb to run, the application now takes about 4Gb. That's a big improvement, but still awfully wasteful.

    (2) I personally do not use STL for anything other than the containers. I do not use the algorithms at all, though part of that is an aversion to adding more layers of indirection into code.

    (3) The various difficulties that have been presented by STL for me in the past have all been related to it's use of the heap, both with regards to time spent when the heap gets excessively fragmented or very large, and in lack of control over how much heap is used or is available to be used. Custom allocators help, but do not provide a complete solution to the problem, at least in the kinds of environments I have been dealing with.

    (a) Performance with large fragmented heaps: That would probably not be as much of an issue today, with the new techniques being used for "low fragmentation heap" in recent years.

    (b) Control over how much heap is used: That was with a server-side application which could not be allowed to take more than a certain amount of memory. Swapping virtual memory could not be allowed to happen, for performance reasons. We were able to constrain a lot of the activity through custom allocators, but not all, and the remaining memory was problematic.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by rpapo View Post
    (2) I personally do not use STL for anything other than the containers. I do not use the algorithms at all, though part of that is an aversion to adding more layers of indirection into code.
    I don't understand what you're saying here. So your code does no sorting, no collating, nothing except store strings?

    An algorithm is supposed to work with any container type, as long as the container elements can be retrieved using an iterator type. Therefore a container can be something as complex as a std:eque, or it could be as simple as a plain old array since both can access elements using a type that supports +=, -=, ++, etc.

    For example, you want to sort integers:
    Code:
    #include <algorithm>
    
    int main()
    {
       int ar[] =  { 8, 2, 4, 93, -434, 5, 78, 0};
       std::sort(ar, ar + sizeof(ar) / sizeof(ar[0]) );
    }
    The ar is now sorted in ascending order. There are no STL containers in that entire code, only a plain old array. I didn't create a vector<int> or a list<int> to sort the array.

    So you don't need to use STL containers to use STL algorithms, so I don't see the indirection that you speak of.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Feb 2012
    Posts
    5

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Dear Paul Thanks a lot for your suggestion i will follow it.

    I am getting the error in release mode on x64 platform as
    Unhandled exception at 0x775d6cd2 (ntdll.dll) in BernsteinBasicAlgoSerial8.exe: 0xC0000374: A heap has been corrupted.
    i checked the event application log and found the following error message
    Log Name: Application
    Source: Application Error
    Date: 04-02-2012 20:49:33
    Event ID: 1000
    Task Category: (100)
    Level: Error
    Keywords: Classic
    User: N/A
    Computer: TeslaGPU
    Description:
    Faulting application name: BernsteinBasicAlgoSerial8.exe, version: 0.0.0.0, time stamp: 0x4f2d4aeb
    Faulting module name: ntdll.dll, version: 6.1.7600.16385, time stamp: 0x4a5be02b
    Exception code: 0xc0000005
    Fault offset: 0x0000000000022d0d
    Faulting process id: 0x1370
    Faulting application start time: 0x01cce34f72703076
    Faulting application path: E:\DhabeSyscon\CUDA\PHD-CUDA-Programs\FinalPrograms\MultiBernstein\Matrix-Method\MatrixFinalPrograms\Bernstein Algorithm\BernsteinBasicAlgoSerial8\x64\Release\BernsteinBasicAlgoSerial8.exe
    Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
    Report Id: a379912a-4f43-11e1-a5bc-001cc0b2480b
    Event Xml:
    <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
    <System>
    <Provider Name="Application Error" />
    <EventID Qualifiers="0">1000</EventID>
    <Level>2</Level>
    <Task>100</Task>
    <Keywords>0x80000000000000</Keywords>
    <TimeCreated SystemTime="2012-02-04T15:19:33.000000000Z" />
    <EventRecordID>82937</EventRecordID>
    <Channel>Application</Channel>
    <Computer>TeslaGPU</Computer>
    <Security />
    </System>
    <EventData>
    <Data>BernsteinBasicAlgoSerial8.exe</Data>
    <Data>0.0.0.0</Data>
    <Data>4f2d4aeb</Data>
    <Data>ntdll.dll</Data>
    <Data>6.1.7600.16385</Data>
    <Data>4a5be02b</Data>
    <Data>c0000005</Data>
    <Data>0000000000022d0d</Data>
    <Data>1370</Data>
    <Data>01cce34f72703076</Data>
    <Data>E:\DhabeSyscon\CUDA\PHD-CUDA-Programs\FinalPrograms\MultiBernstein\Matrix-Method\MatrixFinalPrograms\Bernstein Algorithm\BernsteinBasicAlgoSerial8\x64\Release\BernsteinBasicAlgoSerial8.exe</Data>
    <Data>C:\Windows\SYSTEM32\ntdll.dll</Data>
    <Data>a379912a-4f43-11e1-a5bc-001cc0b2480b</Data>
    </EventData>
    </Event>

    Does it means that i have to reinstall ntdll.dll only or the whole OS?. I am using Winodws 7. What will be the simplest way to do it? Thanks

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,545

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by dhabeps View Post
    ...
    I am getting the error in release mode on x64 platform as
    Unhandled exception at 0x775d6cd2 (ntdll.dll) in BernsteinBasicAlgoSerial8.exe: 0xC0000374: A heap has been corrupted.
    i checked the event application log and found the following error message
    Log Name: Application
    ...
    Faulting application name: BernsteinBasicAlgoSerial8.exe, version: 0.0.0.0, time stamp: 0x4f2d4aeb
    Faulting module name: ntdll.dll, version: 6.1.7600.16385, time stamp: 0x4a5be02b
    Exception code: 0xc0000005
    Fault offset: 0x0000000000022d0d
    ...
    Does it means that i have to reinstall ntdll.dll only or the whole OS?. I am using Winodws 7. What will be the simplest way to do it? Thanks
    Why "reinstall ntdll.dll only or the whole OS"?
    What do they both have to do with the error in your BernsteinBasicAlgoSerial8.exe application?

    Well, You already have an offset: 0x0000000000022d0d, So if you had a map file you could try to find out the place of the failure in your own code.
    Victor Nijegorodov

  13. #13
    Join Date
    Jan 2012
    Location
    Belleville, Michigan, USA
    Posts
    13

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by Paul McKenzie View Post
    I don't understand what you're saying here. So your code does no sorting, no collating, nothing except store strings?
    Speaking in general, over all the things I've worked on over the years, I've never needed the extra algorithms contained in STL for the simple reason that I've known how to do those things the hard way since the 1970s.

    Speaking specifically of the program I was working on yesterday, most of the sorting and searching needed in that application are provided implicitly by the map, set and multiset containers.

    Feel free to call me a dinosaur, though at least I work on PCs, not mainframes.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by rpapo View Post
    Speaking in general, over all the things I've worked on over the years, I've never needed the extra algorithms contained in STL for the simple reason that I've known how to do those things the hard way
    Everyone that is answering you on this thread also know how to code basic algorithms and containers. So why do most people who will answer you use the STL algorithms and containers and not code their own?

    Look at the sort code above. Now how would you write that code I posted earlier?

    I can tell you right now that my code works. I don't need to run it or debug it, and everyone here who understands the sort() function knows it works without having to run anything. So I have correct code, no maintenance cost, every programmer at first glance knows it works, it works for all input data, and the sort() must perform within algorithmic limits (it must be logarithmic in complexity), so I'm guaranteed I'm not stuck with a horrid sort algorithm being invoked. A tiny two-line program does the job -- you can also add type-safety as sort() knows the types it's sorting (unlike qsort() which knows nothing about the types you're sorting).

    If you posted your home-made solution, there is no way I would or anyone else would know it isn't bug free without having to go through the code line-by-line, running it with various input data, etc. And even after that, we're still not 100&#37; sure of things.
    Feel free to call me a dinosaur, though at least I work on PCs, not mainframes.
    In this day and age of C++, not knowing STL limits you in exploring the wealth of the C++ library, and also in many companies, they do not hire programmers who want to write their own home-made classes when the STL containers and algorithms exist.

    http://www.parashift.com/c++-faq-lite/containers.html

    The same thing can be said for algorithms.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 4th, 2012 at 04:26 PM.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Heap corruption error in C program on VS2010 when freeing memory

    Quote Originally Posted by dhabeps View Post
    Does it means that i have to reinstall ntdll.dll only or the whole OS?. I am using Winodws 7. What will be the simplest way to do it? Thanks
    It means you need to fix your program.

    There is nothing wrong with the operating system, so why are you talking about "reinstalling the OS"? It is your program that needs to be fixed. You are corrupting memory, and you need to discover where you're corrupting memory.

    What I think the issue is: You saw "good" output, so you think your program is working and the only thing messed up is the OS. Wrong! Just because you see good output doesn't mean your program is working correctly.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 4th, 2012 at 04:37 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)