Help, my C++ debugger ate baadf00d

When I debugged my brandnew bada application my simulator suddenly crashed in debugging mode on an operation that worked fine in the release compilation. After having a look at the variables I stumbled upon a pointer that was initialized with the hexadecimal memory value “#baadf00d”  (the 0s are zeros 😉 ). An error happened when the runtime stepped over code that looked like:

if (_ptr == null) {
delete _ptr;
_ptr = null;
}

As said this runs fine in the release build (no crash) so what has happened? I simply forgot to initialize the _ptr with null when I constructed the instance. As it seems the release build macros either take care of that or the delete function somehow recognizes the baadf00d pointers and either interpret them as zero or step over them. The good thing: if you run this code in debugging mode you must initialize the pointers thereby fixing potential problems that might occur in the future (hands up all C++ developers who debugged more than two hours to find an uninitialized but assumed null pointer).

It turns out that this is the usual verdict: it’s not a bug, it’s a feature. Some feature that prevents uninitialized variables.

Here are some links to follow for you:

http://blog.wolffmyren.com/2008/10/26/baadf00d/

There are even more hex values that are particularly documented by Visual Studio:

http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s