5

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
Chris Krycho
  • 3,125
  • 1
  • 23
  • 35
kaneda
  • 5,981
  • 8
  • 48
  • 73
  • 6
    Being put in a register is not the only possible optimization that may have occurred here. – Oliver Charlesworth Jan 27 '11 at 11:54
  • 2
    could you check your compiler logs? probably you have somewhere overwritten your -O0 ? – fazo Jan 27 '11 at 12:32
  • should you have '()' QGridLayout *layout = new QGridLayout->HERE<-; ?? – fazo Jan 27 '11 at 12:34
  • 1
    @fazo: The empty parenthesis are optional if the class contains a default constructor. It will slightly change the meaning for POD types (will force default-initialization) but for types that have non-implicitly defined default constructor the meaning is the same. – David Rodríguez - dribeas Jan 27 '11 at 12:56

3 Answers3

3

What other compiler flags have you set?

Try:

-g -O0 -fno-inline
GrahamS
  • 9,980
  • 9
  • 49
  • 63
2

Use volatile. Maybe, it'll help you!

Why do we use volatile keyword in C++?

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
2

An easy way to force a variable to be allocated is to copy a pointer to it to a global variable.

QGridLayout **dummy; // at file scope

//...
dummy = &layout; // in function

This will force the compiler to spill the variable to memory before calling any non-inlined function, as it cannot prove that the function would not access the variable directly.

This may, of course, have performance impacts, but they're likely to be small compared to the cost of calling the GUI member functions on QGridLayout.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • I was thinking something similar; declared a pointer on the global scope, to force compiler to place a pointer to it in memory. But what I did was just `QGridLayout *dummy; // global scope` but I guess its slighty differente. – kaneda Jan 27 '11 at 17:49
  • That only forces the object _pointed to by_ layout to be flushed to memory; this is already implied by calling functions on layout. The key is to force the pointer variable itself to be flushed to memory where gdb can get to it. – bdonlan Jan 28 '11 at 03:54