tags:

views:

139

answers:

2

program runs fine. When I put a breakpoint a segmentation fault is generated. Is it me or GDB? At run time this never happens and if I instantiate only one object then no problems.

Im using QtCreator on ubuntu x86_64 karmic koala.

UPDATE1:

I have made a small program containing a simplified version of that class. You can download it at:

example program

simply put a breakpoint on the first line of the function called drawChart() and step into to see the segfault happen

UPDATE2: This is another small program but it is practically the same as the mandlebrot example and it is still happening. You can diff it with mandlebrot to see the small difference.

almost the same as mandlebrot example program

+2  A: 

To answer your question: Yes, you should be able to debug multiple threads using GDB. This depends on the concurrent design to be sound.

There is a chance you have a race condition on data that your threads access. It is possible that the problem does not show when you run the program normally, but attaching a debugger changes timing and scheduling. Even so, you should be able to use the debugger to break when the segfault happens. Understanding where this happens can inform you about the race condition or corruption, whatever the case may be.

It is worth looking into because even if it doesn't happen under most 'run time' conditions, it may manifest under different system load conditions.

Adam
these are different instances of the same object though. So there should be data somewhere that all three are using?
yan bellavance
I was speaking in generalities based on your description of the problem. To answer this we would need a little more background on the class you're using and how you're using multiple instances.Can you share some example code?
Adam
@Adam: If you're interested, you can learn a little more about the problem by looking at his/her previous two questions, which are both about slightly different aspects of the problem.
Caleb Huitt - cjhuitt
@Adam: I basically took the mandlebrot example and modified it a little bit. So the program would have three of them at the same time, different position. The code is not much different from the mandlebrot. I will try to post some code when i have some time but I am quite sure I havent fed it any ressources that they would be accessing at the same time and only called the constructor and did an initial paint event.
yan bellavance
@Caleb: If I were to make 3 copies of the source code of the class, give each one a unique name, instantiate one of each and then the problem would go away, could that indicate that the problem is actually GDB?
yan bellavance
@Adam I am talking about 3 instantiations of a class that contains a qthread.
yan bellavance
@yan bellavance: I took a quick look at the mandelbrot example from Qt, and didn't see any obvious pitfalls. It is of course possible that gdb has a problem... but very unlikely. Without seeing your code, it would be nearly impossible to answer properly beyond Adam's original answer, which is that GDB handles multiple instances of the same object, even if the objects are threads.
Caleb Huitt - cjhuitt
@yan bellavance: If I understand your comment above correctly, you made the three instances unique classes, and the problem went away. This strengthens the the case that this is a thread synchronization problem. Look at the leaf class for access of static or singleton variables. Understand the contents of any macros that are present in the class definition. Also consider rendering resources that the instances might share.Regretfully, I am not in a position to mock this up myself right now.
Adam
@Caleb : ok I am putting the classes in a simple program and will send you a link to download it. I am also removing everything not related to the problem so its simpler for you to look at. Would you download it if I sent you a link? I have tried 3 mandlebrots in 1 program and there was no breakpoint problems yet with mine there was. A big difference between the 2 is I use qpainter and QBrush to paint the QImage.
yan bellavance
@Adam: @Caleb: Ok I have been able to create the same segmentation fault with the Mandlebrot example but with a small modification. hopefully this will show me where I went wrong or if there is something about the structure of Mandlebrot that doesnt fit my needs. I will post a link to the modified mandlebrotThe delta between the two is small enough that if you could help me find the problem that would be great!
yan bellavance
other samml difference is I have a QMainwindow.
yan bellavance
A: 

Are you Calling into Qt's drawing code from multiple threads? (particularly widget methods)

http://doc.qt.nokia.com/4.3/threads.html#reentrancy-and-thread-safety

Seems like Qt is like GTK+ and you should only be touching GUI stuff from one thread (in particular the main one)

I'm not familiar enough with Qt to give you advice on how to change your code, but I'd suggest changing it to be event based (ie rendering starts in response to an event, then triggers an event in the main thread when it's done, every thread has it's own mainloop) that way you can probably completely avoid mutexes and synchronization.

Spudd86
my code is already event based. It is safe to draw on a QImage from outside the mainthread, in fact I used a qt example to base myself on it. The problem arises without even involving reentrancy and thread safety as far as the code I wrote goes. I do not know about qt itself though.
yan bellavance
I see... what have you changed from the example? (also it's possible there is a bug in the example...) Also does the Qt example code do the same thing?
Spudd86