🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Newsletter #50 - Debugged Lists

posted in IfThen Software
Published October 15, 2009
Advertisement



News

No news to report this week.


From the Programmer
Written by Invisible

I ran into a rather odd problem this week. I was creating the workbench for the rendering system, which seemed to be working fine... Until I ran it in debug mode and it crashed.

After poking around in the code for a bit, I found out that the crash was being caused by attempting to iterate through an std::list. However, I was unable to recreate the bug in a testbed project. Out of ideas, I was about to quit for the day when I had a sudden inspiration: The rendering system's workbench was being ran in debug mode, but the static libraries that it used were compiled in release mode. Sure enough, after using debug versions of the static libraries, the program ran perfectly fine.

Curious to know exactly what was causing the problem, I made a copy of the workbench and the libraries that it used and slowly stripped away code that wasn't affecting the crash. Here is the code that I was left with:

http://codepad.org/ibTVTcOv
/*****test.h*****/	#ifndef HEADER_TEST	#define HEADER_TEST	#include 	using namespace std;	void FunctionA(list<int> *src_lstList);	#endif/*****test.libRuntime: Multi-threaded DLL*****/	/*****	test.cpp	*****/		#include "test.h"				void FunctionA(list<int> *src_lstList)		{			list<int>	*lstList = src_lstList;						// If this line is removed, the error does not happen.			if (1 == 0) lstList->begin();		}/*****test.exeRuntime: Multi-threaded Debug DLLLinks with test.lib*****/	/*****	main.cpp	*****/		#include "test.h"				int main(int argc, char *argv[])		{			list<int>	*lstList = new list<int>;						FunctionA(lstList);						list<int> *lstList2 = new list<int>;						// This is the line that errors.  The error is:			// First-chance exception at 0x00401023 in test.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.			list<int>::iterator i = lstList2->begin();						return (0);		}


Line #29 interests me in particular. Since that if statement should never be entered, you would think that lstList->begin(); would have no affect on anything. However, if that is changed to lstList->size(); or simply removed, the problem does not occur. The only explanation I can think of is that std::list::begin() is inline, which must somehow escape the if statement or cause corruption of the code. A debug version of std::list is being placed into a release version on line #26, but since only a pointer is assigned, std::list should be none the wiser.


Artist's Easel
Written by GreyKnight

iScribble Sketches #8

(Click to enlarge)


(Click to enlarge)



Community Spotlight
Written by Invisible

Jaythemage has released another community spotlight video. Check it out here:
">
0 likes 1 comments

Comments

Emmanuel Deloget
Conclusion of the story : always ensure that our build system is set up in a way that prevent you to mix debug and release binaries.

I tend to compile everything in static in order to avoid this kind of problem. When this is not possible, I always make sure that

1) my program depends on my dlls
2) I have a separate output directory for debug and release

That way, I avoid this kind of bug (which can be tedious to understand).
October 16, 2009 07:53 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement