Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Debugging multi-threaded applications in Borland C++ Builder 1

Status
Not open for further replies.

michielv

Programmer
Aug 16, 2006
4
NL
Hi,

I'm developing a multi-threaded application with Borland C++ Builder Personal version 6.0. At run-time, sometimes (too often ;-) ) one of the threads stops while the others (and also the main program thread) continue. I don’t know the reason why the thread stops; I suppose some kind of error occurs. Unfortunately I’m not able to investigate the problem in the debugger since the debugger is not triggered by the stopped thread. Placing breakpoints doesn’t help either since such breakpoints only take me to those parts of the application that still run fine (hence do not contain the problem at hand).

Does anyone know how to effectively debug multi-threaded applications?

Thanks very much for your help,

Michiel.
 
I looked through my notes and didn't find anything that deals with your problem. Since I don't know exactly how your program works I don't know if this will help or not. Instead of trying to use the debugger, try using message boxes. You will have to set them up so when a certain event is triggered the box will pop up.

Code:
// Temp. Don't forget to comment out after debugging
AnsiString ErrMsgStr = "Opps!. I got " + IntToStr(SomeInt) + " when I should have gotten " + IntToStr(SomeOtherInt);
Application->MessageBox(ErrMsgStr.c_str(), "Thread Stopped!", MB_OK);
// Temp. See note above

James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Hi James,

Thanks for the tip. At this moment I haven't been able yet to solve the problem, but I'll let you know how things are going. For now, I'm also considering to add an additional 'thread-manager' thread to the program. The purpose of this thread would than be to check the status of the other threads. Should one of those other threads be terminated, the thread manager thread would then restart the terminated thread.

Bye, bye,

Michiel.
 
Debugging multi-threaded apps can be very difficult and frustrating. Keep in mind that whatever you do when debugging can change the timing of the execution of the code and hence affect how or when a bug reveals itself.

That being said, the first thing to attempt to do is to isolate where in your code the bug is causing the thread to terminate. I would suggest creating a log file in which you log status messages placed strategically throughout your code (you could also log messages to a TMemo etc.) If logging to a file, make sure that you flush your output file properly (by flushing or closing it) or you may lose debug messages. I would first log messages anywhere a segment of code begins and exits, such as at the beginning and end of any functions or methods in the thread as well as before any return call etc. The idea is to first isolate the function where the error occurs. Then when the thread terminates, you should be able to examine the log and see a function entry log message as your last line in the log, but no function exit message.

Once you have isolated the function where the error is showing up, then place more log messages within this segment of code to further determine where the thread is terminating. Eventually you should be able to isolate where the thread is crashing and hopefully figure out why.

Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top