0

I have a simple gui that has a text field, a drop-down menu, and a go button. I can specify the name and class of a part I'm looking for, and call a function by connecting the "go" button to a slot that runs a function that I already made.

However, when the slot function is done with everything, it calls a function in xstring, that's deleting some massive xstring. It goes to this function:

void _Tidy(bool _Built = false,
    size_type _Newsize = 0)
    {   // initialize buffer, deallocating any storage
    if (!_Built)
        ;
    else if (this->_BUF_SIZE <= this->_Myres)
        {   // copy any leftovers to small buffer and deallocate
        pointer _Ptr = this->_Bx._Ptr;
        this->_Getal().destroy(&this->_Bx._Ptr);
        if (0 < _Newsize)
            _Traits::copy(this->_Bx._Buf,
                _STD addressof(*_Ptr), _Newsize);
        this->_Getal().deallocate(_Ptr, this->_Myres + 1);
        }
    this->_Myres = this->_BUF_SIZE - 1;
    _Eos(_Newsize);
}

And my program executes a break at this->_Getal().deallocate(_Ptr, this->_Myres + 1);.

Here's the code for the gui:

#include <QtGui>
#include <QApplication>
#include <QComboBox>
#include "gui.h"
#include <vector>

std::vector<std::string> PartClasses;

gui::gui(QWidget *parent) : QDialog(parent){

    getPartClasses(PartClasses); //my own function, does not affect how the gui runs, just puts strings in PartClasses

    label1 = new QLabel(tr("Insert Name (Optional):"));
    label2 = new QLabel(tr("Class Name (Required):"));
    lineEdit = new QLineEdit;

    goButton = new QPushButton(tr("&Go"));
    goButton->setDefault(true);
    connect(goButton, SIGNAL(clicked()), this, SLOT(on_go_clicked()));

    cb = new QComboBox();
    for(int i = 0; i < PartClasses.size(); i++)
        cb->addItem(QString::fromStdString(PartClasses[i]));

    //*add widgets to layouts, removed for space*

    setWindowTitle(tr("TEST"));
    setFixedHeight(sizeHint().height());
}

void gui::on_go_clicked(){
    std::string str(cb->currentText().toStdString());
    updateDB(str, lineEdit->text().toUtf8().constData()); //my function, does not affect the gui.
    QApplication::exit();
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    gui *stuff = new gui;
    stuff->show();
    return app.exec();
}

What is it doing? When I'm done with the slot, shouldn't the gui just come back up, so that I can specify a new object? How can I get it to either not delete this object, or get it to succeed?

Steve The Squid
  • 63
  • 1
  • 1
  • 5
  • 3
    Sounds like you have a crash in your program that's completely unrelated to signals and slots – Chris Aug 19 '13 at 19:35
  • I just tried adding `QApplication::exit()` after the call to my function in `on_go_clicked()`, and it executes quit, but the gui doesn't close, and the program still breaks. – Steve The Squid Aug 19 '13 at 19:35
  • Chris, nothing should be happening in my program after the function runs. I wrote the gui without the functions, and it worked. I wrote the function without the gui, and it works. I put the function in the gui, and it calls the function, finishes, and returns to the gui, but then crashes when the slot function finishes. – Steve The Squid Aug 19 '13 at 19:36
  • If I comment out the `updateDB()` code, the application runs `QApplication::exit()` just fine, and quits. If I get rid of `QApplication::exit()`, there is no break. The issue is deleting temporary objects created in the function and used as arguments in the function... but why can't it delete them? – Steve The Squid Aug 19 '13 at 20:42

1 Answers1

0

Here is my best guess at what is happening:

An object that you are accessing is getting deleted somewhere where it shouldn't.

That _Tidy function looks like it is doing some cleanup after string manipulation. Chances are the const-ness of your char * didn't make it down, and you are deleting a const pointer.

To fix this I would make a deep copy of your variable and pass that into your updateDB that does the xstring LaTex stuff. Or you could create an xstring object right then and there and pass it down.

I would also consider using strcpy or something similar, or maybe just the std::string.

Also the crash code that comes out can be helpful, too.

EDIT:

Here is what your code should probably look like...

void gui::on_go_clicked(){
    std::string str(cb->currentText().toStdString());
    std::string line_edit_str(lineEdit->text().toUtf8().constData());

    updateDB(str, line_edit_str); //my function, does not affect the gui.
    QApplication::exit();
}

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • For some reason it was because I used `toStdString()` instead of `.toUtf8().constData()`. I hate that it won't let me use the normal way for some reason. – Steve The Squid Aug 20 '13 at 17:39
  • QString is extremely powerful and can handle all the craziness of localization in UTF, Wide Character, and internet standards. If you put the variable into a local scoped variable, you shouldn't have an issue, but if you pass in an immutable const element down, you are asking for trouble. http://stackoverflow.com/questions/4214369/how-to-convert-qstring-to-stdstring – phyatt Aug 20 '13 at 18:55
  • http://stackoverflow.com/questions/12495226/immutable-object-in-collections-c-and-qt – phyatt Aug 20 '13 at 18:58