LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-22-2007, 07:21 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
Qt: seg fault when calling treewidget


I have a custom QWidget class which creates a QTreeWidget in the constructor.. From the constructor I call a private method which should create and insert a QTreeWidgetItem.

Code:
void MyClass::insertItem()
{
        QMessageBox::information(0, "First", "Here we go..");
        QTreeWidgetItem *blah = new QTreeWidgetItem(myTree, QStringList(QString("Test item")));
        myTree->insertTopLevelItem(0, blah);
        QMessageBox::information(0, "Second", "Ta-daa!!");
}
Right after the first messagebox I get a segmentation fault. If I create and insert the QTreeWidgetItem inside the constructor everything works fine, though.

Even if the problem is that the QTreeWidgetItem disappears when the function is done the second messagebox should appear, since it's called inside the method.

If I comment out the insertTopLevelItem()-call the code runs fine, so the seg fault is caused by that line..

Please, any ideas? I'm stuck here.. :/

Last edited by Ephracis; 02-22-2007 at 07:22 AM.
 
Old 02-22-2007, 07:34 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Load it up in gdb, break on the offending line and display myTree and blah. What do you get?
 
Old 02-22-2007, 08:22 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by matthewg42
Load it up in gdb, break on the offending line and display myTree and blah. What do you get?
I cannot find the offending line in gdb, and I don't know what you mean by "display myTree and blah" so without a breakpoint this is what gdb gives me:

Quote:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1222527312 (LWP 15285)]
0xb7e40995 in QTreeWidget::insertTopLevelItem () from /usr/lib/qt4/libQtGui.so.4
(gdb) disas 0xb7e40995
I tried to disas main and go my way to the offending line but it was too complex for me. Is there an easy way to locate that line of code in gdb?
 
Old 02-22-2007, 08:44 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
To set the breakpoint, you just look up the line number in the source file in which that line of code exists, for example, maybe it's in "myprog.cpp", and the line number is 47. In this case you load the program with gdb, and at the gdb> prompt, before running it, type:
Code:
gdb> b myprog.cpp:47
By the way, you need to compile your program with the -g option to be able to meaningfully debug it.

Anyhow, once it's broken in, you print details about program variables using "p variablename", for example:
Code:
gdb> p myTree
What you're looking for in particular is that maybe myTree is a NULL pointer or something like that. You should get type information too, which might be useful.

In fact, there's not really a reason to set the breakpoint, you can let the program seg fault in gdb, and still example the state of the variables. In fact, if you let it segfault, you can also ask for a backtrace which can give useful clues about what is going on. Just use the "bt" command in gdb for that. Post it here (in [CODE] tags and I can have a look at it.

I would imagine that it will be necessary for you to post more of your program for anyone to really be able to understand why you have an error.
 
Old 02-22-2007, 12:48 PM   #5
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Kinda hard to explain the program but the only actual relevant part is that I have a custom widget and I have a QTreeWidget in it.. Now, I can add QTreeWidgetItems in the constructor but not in the methods.. This is what the code looks like:

Code:
class Pizza : public QWidget
{
    Q_OBJECT
    public:
        Pizza(QWidget *parent=0);
    private:
        QTreeWidget *persTree;
        QList<QTreeWidgetItem*> persTreeItems;
        //QTreeWidgetItem *testItem;

        void insertPerson();
};

Pizza::Pizza(QWidget *parent)
    : QWidget(parent)
{
    persTree = new QTreeWidget;
    persTree->setColumnCount(5);
    persTreeItems.append(new QTreeWidgetItem(persTree,
                                            QStringList(QString("blah"))));
    // this will give me an item in the tree
    // I don't even have to add it via persTree->insertTopLevelItem()

    // calling the breaking method:
    insertPerson();
}

void Pizza::insertPerson()
{
    // I've tried both using the QList and the testItem
    // it makes no difference
    persTreeItems.append(new QTreeWidgetItem(persTree,
                                            QStringList(QString("blah2"))));
    // so far it does not seg fault
    // but this item does not show up in the tree
    // 
    // QList will grow for each time this method is called, tho.

    // this will break it
    //persTree->insertTopLevelItems(0, persTreeItems);
    // and so will this
    persTree->insertTopLevelItem(0, persTreeItems.last());
}
And btw, here is the output from gdb. I added the -g flag in my Makefile but I don't get any symbols so I can't print the object. Although the backtrace shows that as I thought the problem is in when I add the QTreeWidgetItem to the QTreeWidget.
Code:
(gdb) run
Starting program: /home/ephracis/development/pizza/cpp/cpp 
[Thread debugging using libthread_db enabled]
[New Thread -1222060368 (LWP 16482)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1222060368 (LWP 16482)]
0xb7eb2995 in QTreeWidget::insertTopLevelItem () from /usr/lib/qt4/libQtGui.so.4
(gdb) p persTree
No symbol "persTree" in current context.
(gdb) bt
#0  0xb7eb2995 in QTreeWidget::insertTopLevelItem () from /usr/lib/qt4/libQtGui.so.4
#1  0x08053caa in Pizza::insertPerson ()
#2  0x08055f5d in Pizza::Pizza ()
#3  0x08053588 in MainWindow::MainWindow ()
#4  0x0804fd22 in main ()
 
Old 02-22-2007, 01:05 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Works for me. I made a very slight change with the parenting, but apart from that, I get no segv:

http://porpoisehead.net/misc/pizza.tar.gz

QT version is 4.2.
 
Old 02-22-2007, 01:50 PM   #7
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by matthewg42
Works for me. I made a very slight change with the parenting, but apart from that, I get no segv:

http://porpoisehead.net/misc/pizza.tar.gz

QT version is 4.2.
Dude, that works for me too.. I must investigate my code.. I bet it's a typo.

But anyway, thanks for the help. At least I learned some gdb, and that's good cause I've always wanted to learn gdb.

EDIT:
Yeah! It works.. It seems the problem was
Code:
QTreeWidget *persTree = new QTreeWidget;
I changed it to
Code:
persTree = new QTreeWidget;
Is this because I am trying to declare it twice (both in the constructor and in the header file)?

Last edited by Ephracis; 02-22-2007 at 02:04 PM.
 
Old 02-22-2007, 02:35 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I don't know - I would have thought that if you declare it twice you'd get an error at compiple time. *shrug*. Glad I helped anyway

gdb is a very useful tool to know. It's used as the underlying debugger for several other tools, such as kdevelop's integrated debugging and so on. Even so, it's always nice to be able to talk to the actual tool which is doing the work. It isn't flash, but it works well.
 
Old 02-23-2007, 05:38 AM   #9
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by matthewg42
gdb is a very useful tool to know. It's used as the underlying debugger for several other tools, such as kdevelop's integrated debugging and so on. Even so, it's always nice to be able to talk to the actual tool which is doing the work. It isn't flash, but it works well.
Yeah I've used it a little before but it is a very complex tool and much of it's features are well hidden to the naked eye.. So it's not something you just sit down with and figure out on your own..

But there's no doubt that it's a good tool to know. Especially since I am very interested in security.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
seg fault vm_devadas Linux - Enterprise 1 12-05-2006 02:28 PM
Can't login - seg fault dbc001 Slackware 8 05-17-2006 02:42 PM
what could cause this fwrite() to seg fault? hedpe Programming 11 02-12-2006 10:02 PM
C seg fault drigz Programming 5 10-01-2004 03:35 PM
gnome_error_dialog seg fault?? Castro Programming 3 06-11-2003 01:28 PM


All times are GMT -5. The time now is 10:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration