Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could cause a returning function to crash? C++

So I have been debugging this error for hours now. I writing a program using Ogre3d relevant only because it doesn't load symbols so it doesn't let me stack trace which made finding the location of the crash even harder. So, write before I call a specific function I print out "Starting" then I call the function and immediately after I print "Stopping". Throughout the function I print out letters A-F where F is printed right before the function returns (one line above the last '}') The weird thing is when the crash occurs it is after the 'F' is printed but there is no 'Stopping'. Does this mean that the crash is happening in between somewhere? The only thing I can think of is something going wrong during the deallocation of some of the memory allocated during the function. I've never had anything happen like this, I will keep checking to make sure it's going wrong where I think it is.

like image 564
JeanOTF Avatar asked Feb 26 '23 09:02

JeanOTF


2 Answers

Most of the times when something weird and un-understandable happens, it's because of something else.

You could have some dangling pointers in your code (even in a place far away from that function) pointing to some random memory cells.

You might have used such dangling pointer, and it might have resulted in overwriting some memory cells you need. The result of this is that you changed the behavior of your program by changing some variable defined elsewhere, some constants, or even some code!

I'd suggest you to debug your application using some tool able to check and report erroneous memory accesses, like Valgrind.


Anyway if you are able to localize the source of your crash and to write a really small piece of code that will crash post it here -- it could be just a simple error in your function, although it sounds unlikely, from your description.

like image 137
peoro Avatar answered Mar 04 '23 05:03

peoro


This probably means that the error is happening when the function returns and some destructor is firing. Chances are that you have some destructor trying to free memory it doesn't own, or writing off the end of some buffer in a log, etc.

Another possibility to be aware of might come up if you aren't flushing the output stream. It's possible that "Stopping" is getting printed, but is being buffered before hitting stdout. Make sure to check for this, since if that's what's going on you'll be barking up the wrong tree.

like image 39
templatetypedef Avatar answered Mar 04 '23 07:03

templatetypedef