I am trying to debug an optimized version of a program, where an application crashes when I click a button. In the debug version, the application does not crash. I suspect a memory leakage somewhere.
I am not familiar with valgrind, I tried to use it but could not understand the output. I will come back to it later, but I would like initially to see where precisely in the code the crash occurs.
Through adding printouts, I was able to discover that the crash occurs when the last line of the following code is executed.
std::string selected;
int row = 20;
if (objectX == nullptr)
std::cout << "objectX is a nullptr" << std::endl;
std::cout << "row count of objectX: " << objectX->rowCount() << std::endl;
objectX->getAlteredName(row, selected);
Here is the method getAlteredName:
bool getAlteredName (int row, std::string& name) const {
std::cout << "In getAlteredName function" << std::endl;
// Do some fancy string stuff here
return true;
}
My output is as follows:
row count of objectX: 75
So, that means:
- objectX is not a nullptr.
- it is possible to call methods with objectX (e.g. rowCount())
The error occurs with the call to getAlteredName. The std::cout on the first line of the getAlteredName method does not execute. This is what I cannot understand. It is as if the call to getAlteredName itself causes a crash. Why can objectX->rowCount() be called without a problem but merely the call to getAlteredName() causes a crash on the line right after? Could something have happened to cause objectX to become a nullptr between those two lines ? Or maybe there is something else going on that I am missing completely.
I know I do not provide enough information here to find my memory leakage, I just would like an explanation of what are the possible causes of the above behaviour, to give me a few leads for my investigations.
Note that the rowCount() method is declared in the .h file whereas the getAlteredName() method is declared also in the .h file, but as a virtual method. Perhaps this is a clue to the problem.