Tuesday, November 27, 2007

It was just a 'void' !!!

When I compiled a small application I wrote in C++ in Release Mode, the resulting 'exe' file crashed unexpectedly. Indeed the same code compiled in Debug Mode works fine. Initially I thought that it must be due to some failures in variables initialization, but no. When coding, I am usually very careful with that details.

I spent the whole afternoon trying to solve the problem. After a while I had already located the problem. The error occurs when accessing a list. Then I just had to figure out what I did wrong and solve it. But this apparently simple task was indeed a hard challenge. Finally, when I discovered the problem I was astonished by how strange it was.

I am using a list of pointers to instances of a class I defined. However, when I created the class, it was like this:

class MyClass : public {
(...)
MyClass();
(...)
}

Here, the class constructor does not have any parameters. It works perfectly fine in Debug Mode but it generates an error in Release Mode when using a list of pointers to this class. The solution was simple. I just have to change the constructor as depicted below.

class MyClass : public {
(...)
MyClass(void);
(...)
}

Damn! So much time lost because of a simple 'void'. Damn!

No comments: