PDA

View Full Version : Segmentation Fault when accesing more than once



KillGabio
11th February 2012, 07:40
OK i dont know if it is the hour or what but this lines cause the program to finish unexpectedly, only if the process is called more than once. I mean everything goes fine when i click the button for the first time but if I click it again bamm app exits:



QPixmap bg;
RandomInfo *randurl = new RandomInfo ();
if (url.contains (QString ("ya_cerramos")))
bg = QPixmap (url);
else{
bg = QPixmap (randurl->getRandomUrl ());
}
this->ui->label_imagen->setPixmap (bg);
QStringList frase_autor = QStringList ();
frase_autor = randurl->getFraseRandom ();




PS: this is the code of the method that returns the list, just in case:



QStringList frase_autor;
frase_autor << query->value (1).toString ().trimmed ()<< query->value (2).toString ().trimmed ();
return frase_autor;

BTW i dont get any error while executing this, the list is returned...the problem is when I assign the return list to the list of the mainclass...

mvuori
11th February 2012, 08:12
First thing to do would obviously be to find out where is crashes...
Success of all 'new's shoud always be checked. If the second time return null, of course the application crashes.

ChrisW67
11th February 2012, 08:59
Success of all 'new's shoud always be checked.
C++ new either returns successfully with memory allocated or throws a bad_alloc exception which will generally terminate the program unless you have coded to catch it. Checking the return value is largely unneeded.

KillGabio: If this is the same program you had the other day, where the ui was hacked about to replace the main window central widget, then my money is on the ui->label_imagen pointer being invalid the second time. Put:


Q_ASSERTX(ui->label_imagen, "This code", "UI is mangled :(");

before line 8.

BTW: Do you deallocate the RandomInfo object every time or are you leaking memory? Does this object need to be on the heap?

KillGabio
11th February 2012, 17:18
the problem is this line:
frase_autor = randurl->getFraseRandom ();
The list is returned but fails to assign. If I delete the code and just leave the part of assigning the image the program works just fine. The list frase_autor is crashing the program I think.

I changed as you suggested the other day by adding QStacked so what is below is this


if (stack_ventanas->currentIndex () != 0){
stack_ventanas->removeWidget (stack_ventanas->widget (1));
stack_ventanas->setCurrentIndex (0);
}
so now the ui is not hacked anymore.


BTW: Do you deallocate the RandomInfo object every time or are you leaking memory? Does this object need to be on the heap?

Im leaking the memory :(

Added after 18 minutes:

while debugging I recieve the following error:


(Internal error: pc 0x1 in read in psymtab, but not in symtab.)

KillGabio
11th February 2012, 18:32
OH Lord its driving me crazy.. I found the error is here:


frase_autor = randurl->getFraseRandom ();
ui->label_author->setText (QString ("hola"));
ui->label_text->setFont (QFont("Courier", 8));
ui->label_text->setText (QString ("foo.bar ()"));

I`ll post the full code just in case:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
stack_ventanas = new QStackedWidget;
stack_ventanas->addWidget (this->ui->centralWidget);
this->setCentralWidget (stack_ventanas);
randurl = new RandomInfo ();
frase_autor = QStringList ();
}

void MainWindow::accionFinalizada_Cancelada (QString url){
ui->menuBar->show ();
frase_autor = randurl->getFraseRandom ();
ui->label_author->setText (frase_autor.at (1));
ui->label_text->setFont (QFont("Courier", 8));
ui->label_text->setText (frase_autor.at (0));
if (stack_ventanas->currentIndex () != 0){
stack_ventanas->removeWidget (stack_ventanas->widget (1));
stack_ventanas->setCurrentIndex (0);
}
}

void MainWindow::on_actionFacturacion_triggered()
{
this->ui->menuBar->hide ();
tipo_factura->exec ();
wfact = new widgetFacturacion(this, this->handler, tipo_factura->getTipoFactura ());
connect (wfact, SIGNAL(widgetClosed (QString )) ,this, SLOT(accionFinalizada_Cancelada(QString)));
stack_ventanas->addWidget (wfact);
stack_ventanas->setCurrentIndex (1);
}

KillGabio
12th February 2012, 02:33
FML,

For those interested the problem was that i was opening the local database multiple times. Apparently the sucesive reopen caused a segmentation fault in the dll of the oracle db. So, as always it had nothing to do with the code i posted.