Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare a variable not to be optimized (put into register) in order to debug in C++?

I'm developing a simple application in C++/Qt, and I have the following declaration:

QGridLayout *layout = new QGridLayout;

I'm debugging the application using gdb. I set a breakpoint, it works fine, and the debugger hits the line. But if I try to inspect the object declared above I get this output:

-data-evaluate-expression --thread 1 --frame 0 layout 
^done,value="<value> optimized out>"

I read that this message, "<value> optimized out>", occurs because the compiler optimized the code, and put the data into the register. I'm using g++ compiler, with flag -O0 (none optimization) set.

Is there something I'm missing, or does it exist a way to declare a variable not to be optimized, say, as opposed to the storage specifier register? I'm on Ubuntu 10.10 Maverick, kernel 2.6.35-24.

EDIT1

Some more code:

WorkspaceChooserDialog::WorkspaceChooserDialog(QWidget *parent) : QDialog(parent)
{
    setWindowTitle(tr("Select a workspace location"));
    QLabel *wpLabel = new QLabel(tr("Workspace:"), this);
    QLineEdit *wpLineEdit = new QLineEdit(QDir().homePath(), this);
    QPushButton *okButton = new QPushButton(tr("OK"), this);
    QPushButton *cancelButton = new QPushButton(tr("Cancel"), this);
    QGridLayout *layout = new QGridLayout;

    connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));

    qDebug() << "begin: " << layout << " :end";
    layout->addWidget(wpLabel, 0, 0);
    layout->addWidget(wpLineEdit, 0, 1, 1, 2);
    layout->addWidget(okButton, 1, 1);
    layout->addWidget(cancelButton, 1, 2);
    setLayout(layout);
}

EDIT2

For reasons unknown to me, after I compiled with verbose mode -v flag set, the error didn't appear anymore, even after unsetting it again. Now gdb creates the variable, and I'm able to inspect its value.

For who are interested, the compiler's flags set are:

g++ -O0 -g3 -Wall -c -fmessage-length=0
like image 979
kaneda Avatar asked Jan 27 '11 11:01

kaneda


1 Answers

What other compiler flags have you set?

Try:

-g -O0 -fno-inline
like image 130
GrahamS Avatar answered Sep 18 '22 13:09

GrahamS