PDA

View Full Version : Why is my program segfaulting?



Valheru
30th September 2006, 22:45
I can't figure out this one. It was working, and then I decided to refactor it and everthing went all pear-shaped :(

First, the code :

/edit : code attached below.

Valheru
30th September 2006, 22:48
The code under Server::addServerTab used to be in ServerList::fetchGroupsSlot(). Then I moved it to the Server class, and now it segfaults. It segfaults on the line


mw->getServerTabWidget()->addTab( newServerWidget, name ); Which seems to indicate that it SwatMw *mw isn't initialized, but it gets initialzed in the constructor which is very confusing. What's going on here?

wysota
30th September 2006, 23:09
I'm sorry, I don't want to be rude, but... do you expect us to read and understand 600 lines of code? What do you expect us to do with it? Couldn't you have pasted it as an attachment instead of displaying them inside posts? Is it really necessary to have all the 600 lines to find the problem? I doubt so...

Did you use a debugger? If not, use one (gdb?) and when the application crashes, check the backtrace (bt) and print values of mw, newServerWidget and mw->getServerTabWidget(). One of them will probably be NULL. Also the backtrace will tell you exactly which functions were called and in what order. Maybe you'll notice something weird. Remember to compile your application in debug mode before feeding it to the debugger.

Valheru
30th September 2006, 23:26
Well, I didn't think about attatching it to be honest. And what difference does it make as to weather or not I attatch it or inline it if you think that 600 lines is too much in the first place? I gave all the code as I wanted to be complete. Sorry if that made your life unbearable :crying:

I also think it's rather gay of you to lambaste me in the thread as well as PM'ing me about it BTW.

I can't sodding debug it because I'm using Arch Linux (switched from Gentoo because my KDE borked) and Qt isn't compiled with debug flags on Arch.

I'll see what I can come up with. I was hoping that maybe someone would spot something glaringly wrong wit hmy code - often after staring at your code for hours on end you tend to miss simple things.

wysota
30th September 2006, 23:40
And what difference does it make as to weather or not I attatch it or inline it if you think that 600 lines is too much in the first place?
Because I can ignore/grep/delete/print/do_whatever_I_want if it is attached and I have to scroll through all of it if it is inline. That's simply flooding, especially if you make 4 posts out of it when 1 would be enough.


I gave all the code as I wanted to be complete.
There is nothing wrong with it.... as long as you attach it.

Sorry if that made your life unbearable :crying:
Oh come on! :) I'm just trying to keep order here.


I also think it's rather gay of you to lambaste me in the thread as well as PM'ing me about it BTW.
Very strong words, you should speak them carefully. The PM was a result of an infraction you were given, just a side effect you might say, explaining what you should do to correct the situation (which you haven't done yet anyway).


I can't sodding debug it because I'm using Arch Linux (switched from Gentoo because my KDE borked) and Qt isn't compiled with debug flags on Arch.
Hmm... you might try to print those values directly in the application then, without using a debugger. You'll lose the backtrace ability, but if you're right about the place where it crashes (although it probably crashes somewhere inside Qt library), this might be enough.


I was hoping that maybe someone would spot something glaringly wrong wit hmy code
In 600 lines? :) My only guess when looking at your code was that if indeed "mw" was null/garbage then maybe because your constructor was invoked with an incorrect parameter (SwatMw* m I think). But it's only a wild guess - debugger helps in such situations, maybe you should consider rebuilding Qt in debug mode.

Valheru
30th September 2006, 23:53
Well, here are the files as attatchments. I was trying to edit my original posts but apparantly I can't attach files to edits.

I'm busy installing a debug version of Qt as we speak, it's going to take a while though since I am compiling it from source. I'd hoped I was finished with compiling Qt when I switched from Gentoo but apparently not.

Thanks for any/all help.

P.S. You can delete the superflous posts if you want, I can't it seems.

Valheru
1st October 2006, 00:19
Hmm, when I add mw in the class Server as a watch then it resolves to 0x0 which is plainly _WRONG_...although I can't possibly see how that can be since it is passed to the constructor from ServerList when it is added to the QList. And in ServerList it is initialized before that happens, so it should be right. Something is rotten in the state of Denmark...

jacek
1st October 2006, 01:43
it resolves to 0x0 which is plainly _WRONG_...although I can't possibly see how that can be since it is passed to the constructor from ServerList when it is added to the QList.
Then check whether you really pass a valid pointer to the constructor.


delete menu, model, connectToServer, newServer, editServer, deleteServer, separator;
Are you sure that this statement deletes all of these objects?

Valheru
1st October 2006, 09:02
I found the problem. It had to do with the order in which things were happening - mw was being passed as an object parameter before it was being initialized.



delete menu, model, connectToServer, newServer, editServer, deleteServer, separator;Are you sure that this statement deletes all of these objects?


I take it that you are trying to tell me that the delete command doesn't work this way?

wysota
1st October 2006, 09:09
I take it that you are trying to tell me that the delete command doesn't work this way?

Delete does not have anything to do with this. It is the "," (comma) operator which is responsible. It groups statements, not arguments.

http://msdn2.microsoft.com/en-us/library/zs06xbxh.aspx
http://www.cplusplus.com/doc/tutorial/operators.html (scroll down to "comma operator")

Valheru
1st October 2006, 10:55
Thanks, I've changed it so that each pointer is deleted seperately. Am I correct in thinking that you could put the pointers in a list and call delete on the list, like so :


delete {pointer_1, pointer_2,...,pointer_n};

wysota
1st October 2006, 10:59
No, you would try to delete the list object. But you can put all pointers into the list and call
qDeleteAll(list); or
for(int i=0;i<list.size();i++) delete list[i];