Unhandled exception - LeaveCriticalSection

Hello guys,

I am writing a small simulation program where I make thousands of a function calls to simulate a location based problem. When I use the program to simulate , for instance, at about 60 locations the program works fine. However when I tried to simulate it for 100 locations the program crashed showing the error message below, it does not point out the exact line though. I have tried to simulate each individual points separately it works fine. The problem occurs when increasing the number of simulation locations just above 60. I have tried to carefully release all the dynamically allocated memory to avoid any memory leak. Can someone help me figure out the problem please.


Unhandled exception at 0x7519c41f in myProg.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x006ca3a8..

First-chance exception at 0x7519c41f in myProg.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x006ca3a8..

1
2
3
4
5
6
7
8
9
10
11
12
void __cdecl _unlock (int locknum)
{

        /*

         * leave the critical section.

         */

        LeaveCriticalSection( _locktable[locknum].lock );

}



Thank You!
Last edited on
You have not given us enough code, to even begin helping you.
it sounds like an index out of bounds.

It could also an uninitialized varible/pointer.

Or

Deleting the same object more than once.

...
it sounds like an index out of bounds.

It could also an uninitialized varible/pointer.

Or

Deleting the same object more than once.


The reference page for the std::bad_alloc exception doesn't seem to fit any of those cases. Where are you getting these from?

According to:

http://www.cplusplus.com/reference/new/bad_alloc/

std::badalloc is thrown by new when it fails to allocate memory correctly.

To the OP:

How big are the objects representing these locations that are being instantiated? Is it possible they're big enough that your system is simply running out of memory?
> the program crashed showing the error message below, it does not point out the exact line though
$ g++ -ggdb main.cpp -o program.bin
$ gdb program.bin
> run
(crash)
> backtrace
> it sounds like an index out of bounds.
> It could also an uninitialized varible/pointer.
> Deleting the same object more than once.

Yes. That or something similar is possible (very probable).

An allocation failure resulting in a std::bad_alloc being thrown can be caused by corruption of the heap.
The reference page for the std::bad_alloc exception doesn't seem to fit any of those cases. Where are you getting these from?
This all could cause heap corruption. What would happen if locknum is/was out of bounds?


an interesting ling for detecting heap problems:

http://stackoverflow.com/questions/1010106/how-to-debug-heap-corruption-errors

This all could cause heap corruption. What would happen if locknum is/was out of bounds?

Fair point - guess I wasn't really thinking about it carefully. Sorry!
Last edited on
Thank you so much guys for your suggestions.
Topic archived. No new replies allowed.