Try/catch statement not catching bad_alloc

Jan 11, 2011 at 10:13am
Hi, GDB (debugger) tells me that my program is crashing when it tries to allocate memory. The output says I have a segfault. I have the allocation enclosed in a try bracket, like so:

1
2
3
4
5
6
7
8
9
try{
	data_ = new double [nR*nC*nD];  // allocate memory
}
catch(bad_alloc&){
	cout << endl << "Error in constructor of matrix2d when trying to allocate memory for data_." << endl << endl;
}
catch(...){
	cout << endl << "Some other error!" << endl << endl;
}


GDB says that the error is on the line "data_ = ...", the memory allocation one. Using the 'print' command tells me that nR, nC, and nD are all defined and valid, positive integer values. If it is having trouble, then why isn't it going into the catch part?

Thanks!
Jan 11, 2011 at 2:11pm
Probably the code before that line has already corrupted the heap. You should check all the previous code if it is correct.
Jan 11, 2011 at 2:52pm
segfault is not an exception.
However I think it should be catch(const bad_alloc &) or catch(bad_alloc)
Is the catch(...) block executed?
Last edited on Jan 11, 2011 at 2:56pm
Jan 11, 2011 at 4:21pm
Probably the code before that line has already corrupted the heap. You should check all the previous code if it is correct.


The only code before it in that function is

1
2
3
Matrix2d::Matrix2d(int nR, int nC, int nD) {
        assert(nR > 0 && nC > 0 && nD > 0);    // check that nc and nr both > 0.
        nRow_ = nR; nCol_ = nC; nDep_ = nD;


so I don't think that is it.

Is the catch(...) block executed?


Nope, gdb tells me the segfault is on the allocation line, and it doesn't seem to go further. The cout in the catch(...) definitely doesn't print out.

Here is the backtrace, that shows at what level it actually hits something wrong:

1
2
3
4
5
6
7
8
9
10
11
Program received signal SIGSEGV, Segmentation fault.
0xb7c5dfa1 in ?? () from /lib/libc.so.6
(gdb) backtrace 
#0  0xb7c5dfa1 in ?? () from /lib/libc.so.6
#1  0xb7c604f3 in malloc () from /lib/libc.so.6
#2  0xb7e55569 in operator new(unsigned int) () from /usr/lib/libstdc++.so.6
#3  0xb7e5569d in operator new[](unsigned int) () from /usr/lib/libstdc++.so.6
#4  0x0806637b in Matrix2d::Matrix2d (this=0xbffff0dc, nR=100, nC=24, nD=1) at Matrix2d.cpp:23
#5  0x080548d5 in SortList2d::initLists (this=0xbffff41c, ncell_in_x=100, ncell_in_y=24, npart_in=6000) at SortList2d.h:58
#6  0x08054721 in SortList2d::SortList2d (this=0xbffff41c, ncell_in_x=100, ncell_in_y=24, npart_in=6000) at SortList2d.h:22
#7  0x080519d6 in main () at dsmc2d.cpp:65 


(the code I've posted so far has been from Matrix2d(). )
Last edited on Jan 11, 2011 at 4:22pm
Jan 11, 2011 at 4:41pm
The only code before it in that function is


Where did I say in that function?
You have to check the code executed right from the start of the program till that line when it breaks. This includes also all the code run before the main function.

Jan 11, 2011 at 4:42pm
Yeah, you're right...damn it. Ok, but the only things that could have corrupted the heap are things that use new or delete, right?
Jan 11, 2011 at 4:45pm
The only code before it in that function is


Where did I say in that function?
You have to check the code executed right from the start of the program till that line when it breaks. This includes also all the code run before the main function. However, I would start checking from the code executed right before that function.

No one said it would be easy. C++ is not Java, it is for diehards.

Last edited on Jan 11, 2011 at 4:46pm
Topic archived. No new replies allowed.