Thread: Error Message when run program

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Error Message when run program

    When running my program I get an error message that says this:

    Debug Error!

    HEAP CORRUPTION DETECTED: before Normal block(#1262858) at 0x268F3148.
    CRT detected that the application wrote to memory before start of heap buffer.
    What could this meen. Sometimes when running the program this error doesn´t come up and sometimes it does.

    It is to much code to present here but what I do is to read .txt files into vectors and 2D vectors.
    I know it perheps is difficult to know what it could depend on, but any idéas, guesses is welcome on this one.
    I dont really dont know where to begin my search in the code.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are using an index in a vector or string that is invalid you might get something like that. Use at() instead of [] and you will get an exception that might help you identify the source of the problem.

    Also, you should be able to debug your program and look at the call stack to see where in your code this corruption is occurring. Then you can post just that code and whatever is relevant to it so we can take a look.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    sorry if asking but what is the "call stack". Is this something inside VC++ that creates when just debugging the program and not run it ?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes. When you run your program through the debugger (in VC++ that's F5 to Start or Run, not Ctrl-F5 or Execute), one of the windows that you can open is the Call Stack. When you get the error you mentioned or any other unhandled exception, VC++ will stop the program and you can view where it occurred and what the data is in the location that is being executed.

    So try running it instead of executing it, then do whatever you need to do to get the error to occur. When it gives you the option you should break and then find the call stack window and look for code that you wrote and recognize. Then double click on that code and it should take you to about the place where the error is occurring.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Call stack is the sequence of functions in the order they called each other.
    So if main calls A, which calls B, which calls C, there's a little window called "Call stack" in Visual Studio (when running the debugger) that shows how these functions called each other.
    So you should be able to see where in what function the exception occurs.

    And heap corruption means you wrote to some place in memory where you shouldn't have. You wrote beyond the end of your buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I succeded to get an error while running the program with key "F5". When the error message came up, I choosed "Break".
    The Errormessage was this:
    An unhandled exception of type ´System.AccessViolationException´
    occured in Form1.exe

    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    Then I got a Break at line 141. I attach some lines before and after. Does this say anything meeningful. In the callstack window, It point to:
    >Form1.exe!std::allocator<int>::deallocate(int* _Ptr = 0x25F7A298, unsigned int __unnamed001 = 200) Line 141 C++
    Code:
    132	template<class _Other>
    133		allocator<_Ty>& operator=(const allocator<_Other>&)
    134		{	// assign from a related allocator (do nothing)
    135		return (*this);
    136		}
    137
    138	void deallocate(pointer _Ptr, size_type)
    139		{	// deallocate object at _Ptr, ignore size
    140		::operator delete(_Ptr);
    141		}
    142
    143	pointer allocate(size_type _Count)
    144		{	// allocate array of _Count elements
    145		return (_Allocate(_Count, (pointer)0));
    146		}
    147
    148	pointer allocate(size_type _Count, const void _FARQ *)
    149		{	// allocate array of _Count elements, ignore hint
    150		return (allocate(_Count));
    151		}
    Last edited by Coding; 02-28-2008 at 06:53 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    So look in the call stack window and there should be something below that line that comes from your code. Double click on the line that is in your code.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Under the the line(141) that is the first line here. There is also these lines present under this line. Does this meen that there is something wrong with all of these lines ?

    Code:
    >	Form1.exe!std::allocator<int>::deallocate(int* _Ptr = 0x25F7A298, unsigned int __unnamed001 = 200) Line 141	C++
     	Form1.exe!std::vector<int,std::allocator<int> >::_Tidy() Line 1139	C++
     	Form1.exe!std::vector<int,std::allocator<int> >::~vector<int,std::allocator<int> >() Line 559 + 0x9 bytes	C++
     	Form1.exe!std::vector<int,std::allocator<int> >::`scalar deleting destructor' + 0x1e bytes	
     	Form1.exe!std::_Destroy<std::vector<int,std::allocator<int> > >(std::vector<int,std::allocator<int> >* _Ptr = 0x0D3648C0) Line 59 + 0xa bytes	C++
     	Form1.exe!std::allocator<std::vector<int,std::allocator<int> > >::destroy(std::vector<int,std::allocator<int> >* _Ptr = 0x0D3648C0) Line 161	C++
     	Form1.exe!std::_Destroy_range<std::allocator<std::vector<int,std::allocator<int> > > >(std::vector<int,std::allocator<int> >* _First = 0x0D3648C0, std::vector<int,std::allocator<int> >* _Last = 0x0D4A84C0, std::allocator<std::vector<int,std::allocator<int> > >& _Al = {...}, std::_Nonscalar_ptr_iterator_tag __unnamed003 = {...}) Line 234 + 0xc bytes	C++
     	Form1.exe!std::_Destroy_range<std::allocator<std::vector<int,std::allocator<int> > > >(std::vector<int,std::allocator<int> >* _First = 0x0D2C0040, std::vector<int,std::allocator<int> >* _Last = 0x0D4A84C0, std::allocator<std::vector<int,std::allocator<int> > >& _Al = {...}) Line 226	C++
     	Form1.exe!std::vector<std::vector<int,std::allocator<int> >,std::allocator<std::vector<int,std::allocator<int> > > >::_Destroy(std::vector<int,std::allocator<int> >* _First = 0x0D2C0040, std::vector<int,std::allocator<int> >* _Last = 0x0D4A84C0) Line 1124 + 0x10 bytes	C++
     	Form1.exe!std::vector<std::vector<int,std::allocator<int> >,std::allocator<std::vector<int,std::allocator<int> > > >::_Tidy() Line 1137	C++
     	Form1.exe!std::vector<std::vector<int,std::allocator<int> >,std::allocator<std::vector<int,std::allocator<int> > > >::~vector<std::vector<int,std::allocator<int> >,std::allocator<std::vector<int,std::allocator<int> > > >() Line 559 + 0x9 bytes	C++
     	Form1.exe!Form1::Form3::Optimize_Click(System::Object^ sender = 0x014edd44, System::EventArgs^ e = 0x0156c254) Line 4213 + 0x1a4 bytes	C++
    When doubleclicking all of these lines, it takes me to code I haven&#180;t wrote. It is I beleive debugging code in any way.

    The funny thing is that Form1 that it refer to here doesn&#180;t do any calculation. This form Only open new forms, so on this form there is 7 buttoncontrols only. Not one calculation, vector, string, int or anything here.
    Last edited by Coding; 02-28-2008 at 07:05 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's the call stack. It means that each function is calling the function above it. The one on the bottom appears to be your code? Scroll down to the line that has the code you wrote and only double-click that line.

    If you haven't written any of them, at least you should recognize some, like the bottom one, which references Form3 and an Optimize_Click function.

    By the way, you should make it clear if you are using Forms and .NET programming or C++/CLI or whatever that is. That isn't standard C++ and is covered that often in this forum. I probably won't be able to help much because I know little to nothing about the interaction of a standard C++ container like vector and System and Form stuff.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I was looking wrong, line 4213 was the the closing brace of the buttoncontrol. I will have to look and try things out I beleive, experiment and see...

    Thank you for your help !
    Last edited by Coding; 02-28-2008 at 07:26 PM.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    you using .NET or something? I never use .NET, but for c at the beginning of your code you could try adding

    #define new new(__FILE__, __LINE__)

    which should provide file and line information to the debug heap when running in debug mode which would result in something like:

    Code:
     			 				Debug Error!
    
    HEAP CORRUPTION DETECTED: before Normal block(#1262858) at 0x268F3148.
    CRT detected that the application wrote to memory before start of heap buffer.  File: c:\[whatever dir your code in is]\[filename error occurred].cpp (5)
    Where that (5) in parenthesis would be where the initial memory that is causing the corruption would be found

    i.e.
    Code:
    line#5: float *a = new float[10]; <--- the originally memory that got corrupted.
    ...
    line#500 memset(a - 1, 0,  sizeof(float) * 10) <--- follow the var "a" to find the corruption, which occurred here.
    Your error sounds like the result of pushing a [non-dynamically allocated] variable onto a "vector" and then modifying it using pointers or incorrectly using one of the c++ standard library functions like strn_cpy(); printf(); etc. on it and then sometime down the line when you goto remove that item from the "vector" it tries to deallocate it, but can't because the memory block is screwed.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think I already told you not to mix Native and Managed.
    Go back to C# and stay with Managed. OR go Native C++.
    C++/CLI is standard, but mixing native and managed is not recommended. At least for newbies.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes, I use .NET. Thank you for the information. I am not sure if I understand how to use
    #define new new(__FILE__, __LINE__) correctly but I think you are right about:
    "pushing a [non-dynamically allocated] variable onto a "vector" "

    Sometimes I use push_back for vectors and sometimes not. I am just giving them a size and start put strings and int:s, doubles to the elements using count = (count + 1).

    Then I do sort these vectors and something might go wrong here. Though sometimes I can run my program as I did a test yesterday, with the exact same procedure, over 60 times in a row and then the Error came up. So the program works but at the same time not.
    So not using push_back perheps can cause Errors like this ?
    Thanks

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So not using push_back perheps can cause Errors like this ?
    If you access positions that don't exist, then yes. But you can do that with push_back, too.

    The problem is occuring when a vector is being destroyed in the Form3::Optimize_Click function. Is there a local vector inside there? Was there anything else lower in the call stack?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, as I have mentioned, do not mix native and managed. Scrap your project. You do not know what you are doing.
    Stay only native or only managed and these headaches will go away.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  4. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  5. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM