Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, May 2007
How to turn off compiler optimization?


Message 1 in thread

Hi,

I've been going insane lately with Qt segmentation faults. My only guess 
now is that compiler optimization is the cause. How do I turn it off? 
Can this be done in the project file? I'm using Qt 4.2.3 with gcc 4.1.1 
on Fedora Core 6.

I keep running into the weirdest problems that are causing segfaults.  
I'm attaching an example of my latest problem in the hopes that someone 
can point out why this code segfaults.

Thanks in advance,

John

#include "main.h"
#include "main.h"

int main(int argc, char * argv[]) 
{
	QApplication *MyApp = new QApplication(argc, argv);
	Test *widget = new Test();
	widget->show();
	return MyApp->exec();

}
 
Test::Test(QWidget *parent) : QWidget(parent)
{
	MyName = QString("Sx");
	Label = new QLabel("Sx", parent);

	for (int i = 0; i < 11; i++) {
		CheckBox[i] = new QCheckBox(parent);
	}

	Update();
}

void Test::Update(void)
{
	Label->setText(MyName);
}
 


#include <QApplication>
#include <QApplication>
#include <QtGui>

class Test : public QWidget
{
	Q_OBJECT

public:
	Test(QWidget *parent = 0);
	void Update(void);

private:
	QString		MyName;
	QCheckBox	*CheckBox[10];
	QLabel		*Label;
};


 
######################################################################
######################################################################
# Automatically generated by qmake (2.01a) Tue May 1 14:48:45 2007
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += main.h
SOURCES += main.cpp
CONFIG += debug

Message 2 in thread

John Voltz wrote:
> Hi,
>
> I've been going insane lately with Qt segmentation faults. My only 
> guess now is that compiler optimization is the cause. How do I turn it 
> off? Can this be done in the project file? I'm using Qt 4.2.3 with gcc 
> 4.1.1 on Fedora Core 6.
>
> I keep running into the weirdest problems that are causing segfaults.  
> I'm attaching an example of my latest problem in the hopes that 
> someone can point out why this code segfaults.
>
John,

You have a fence post error. Your array initialization is off by one.

In .h:
QCheckBox    *CheckBox[10];

In .cpp
for (int i = 0; i < 11; i++) {
    CheckBox[i] = new QCheckBox(parent);

Your array of 10 is indexed 0-9. Make that i < 10.

--Justin
begin:vcard
begin:vcard
fn:Justin Noel
n:Noel;Justin
org:ICS;Engineering
adr:;;54B Middlesex Trpk;Bedford;MA;01730;USA
email;internet:justin@xxxxxxx
title:Sr. Consulting Engineer / Certified Qt Instructor
tel;work:(617) 621-0060
url:http://www.ics.com
version:2.1
end:vcard


Message 3 in thread

On Tuesday 01 May 2007 20:52, John Voltz wrote:
> Hi,
>
> I've been going insane lately with Qt segmentation faults. My only guess
> now is that compiler optimization is the cause. How do I turn it off?
> Can this be done in the project file? I'm using Qt 4.2.3 with gcc 4.1.1
> on Fedora Core 6.
>
> I keep running into the weirdest problems that are causing segfaults.
> I'm attaching an example of my latest problem in the hopes that someone
> can point out why this code segfaults.
>
> Thanks in advance,
>
> John

The keyword is not compiler but programmer optimization :-))

>Test::Test(QWidget *parent) : QWidget(parent)
>{
>ÂÂÂÂÂÂÂÂMyName = QString("Sx");
>ÂÂÂÂÂÂÂÂLabel = new QLabel("Sx", parent);
>
>ÂÂÂÂÂÂÂfor (int i = 0; i < 11; i++) {

This goes from CheckBox[0] to [10]. That are 11 items!

>ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂCheckBox[i] = new QCheckBox(parent);
>ÂÂÂÂÂÂÂÂ}
>
>ÂÂÂÂÂÂÂÂUpdate();
>}
>
>class Test : public QWidget
>{
>ÂÂÂÂÂÂÂÂQ_OBJECT
>
>public:
>ÂÂÂÂÂÂÂÂTest(QWidget *parent = 0);
>ÂÂÂÂÂÂÂÂvoid Update(void);
>
>private:
>ÂÂÂÂÂÂÂÂQStringÂÂÂÂÂÂÂÂÂMyName;
>ÂÂÂÂÂÂÂÂQCheckBoxÂÂÂÂÂÂÂ*CheckBox[10];

Here CheckBox has only 10 items.

>ÂÂÂÂÂÂÂÂQLabelÂÂÂÂÂÂÂÂÂÂ*Label;
>};


-- 
 [ signature omitted ] 

Message 4 in thread

But is there a possibility to deactivate optimization?

best regards

seb

On Tuesday 01 May 2007 21:05:13 Reinhardt Behm wrote:
> On Tuesday 01 May 2007 20:52, John Voltz wrote:
> > Hi,
> >
> > I've been going insane lately with Qt segmentation faults. My only guess
> > now is that compiler optimization is the cause. How do I turn it off?
> > Can this be done in the project file? I'm using Qt 4.2.3 with gcc 4.1.1
> > on Fedora Core 6.
> >
> > I keep running into the weirdest problems that are causing segfaults.
> > I'm attaching an example of my latest problem in the hopes that someone
> > can point out why this code segfaults.
> >
> > Thanks in advance,
> >
> > John
>
> The keyword is not compiler but programmer optimization :-))
>
> >Test::Test(QWidget *parent) : QWidget(parent)
> >{
> >ÂÂÂÂÂÂÂÂMyName = QString("Sx");
> >ÂÂÂÂÂÂÂÂLabel = new QLabel("Sx", parent);
> >
> >ÂÂÂÂÂÂÂfor (int i = 0; i < 11; i++) {
>
> This goes from CheckBox[0] to [10]. That are 11 items!
>
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂCheckBox[i] = new QCheckBox(parent);
> >ÂÂÂÂÂÂÂÂ}
> >
> >ÂÂÂÂÂÂÂÂUpdate();
> >}
> >
> >class Test : public QWidget
> >{
> >ÂÂÂÂÂÂÂÂQ_OBJECT
> >
> >public:
> >ÂÂÂÂÂÂÂÂTest(QWidget *parent = 0);
> >ÂÂÂÂÂÂÂÂvoid Update(void);
> >
> >private:
> >ÂÂÂÂÂÂÂÂQStringÂÂÂÂÂÂÂÂÂMyName;
> >ÂÂÂÂÂÂÂÂQCheckBoxÂÂÂÂÂÂÂ*CheckBox[10];
>
> Here CheckBox has only 10 items.
>
> >ÂÂÂÂÂÂÂÂQLabelÂÂÂÂÂÂÂÂÂÂ*Label;
> >};


N‹§²æìr¸›zÇu©šŠ[hªØµêÞÚÞçÚè–[^r(›­†éì®&Þ{azˍç-¢»ayºȬµªÜ+Þjwbú+™«b¢xm¶Ÿÿ–+-²Úè–[^r(›ú­Š{^­ë-

Message 5 in thread

On Tuesday 01 May 2007 13:09, Sebastian Breuers wrote:
> But is there a possibility to deactivate optimization?
>
> best regards

Set your compiler flags to -O0.  You may find it useful to add -g3 as well.

QMAKE_CXXFLAGS_DEBUG += -g3 -O0
in your .pro file, or some such.

--
 [ signature omitted ] 

Message 6 in thread

On 01.05.07 13:12:32, Chris Thompson wrote:
> On Tuesday 01 May 2007 13:09, Sebastian Breuers wrote:
> > But is there a possibility to deactivate optimization?
> >
> > best regards
> 
> Set your compiler flags to -O0.  You may find it useful to add -g3 as well.
> 
> QMAKE_CXXFLAGS_DEBUG += -g3 -O0
> in your .pro file, or some such.

Or instead of hacking into variables that are seldomly needed just do 

CONFIG -= release
CONFIG += debug

That build with debug symbols and without optimizations

Andreas

-- 
 [ signature omitted ] 

Message 7 in thread

hey,

after realising that the proposed line does not do the intended i already 
tried your current suggestion and this worked. but why has 'release' to be 
removed. I expected that adding debug should have been sufficient. 

Why is this designed like this?

thanks in advance

seb


On Tuesday 01 May 2007 21:28:24 Andreas Pakulat wrote:
> On 01.05.07 13:12:32, Chris Thompson wrote:
> > On Tuesday 01 May 2007 13:09, Sebastian Breuers wrote:
> > > But is there a possibility to deactivate optimization?
> > >
> > > best regards
> >
> > Set your compiler flags to -O0.  You may find it useful to add -g3 as
> > well.
> >
> > QMAKE_CXXFLAGS_DEBUG += -g3 -O0
> > in your .pro file, or some such.
>
> Or instead of hacking into variables that are seldomly needed just do
>
> CONFIG -= release
> CONFIG += debug
>
> That build with debug symbols and without optimizations
>
> Andreas


--
 [ signature omitted ] 

Message 8 in thread

On 01.05.07 21:33:51, Sebastian Breuers wrote:
> after realising that the proposed line does not do the intended i already 
> tried your current suggestion and this worked. but why has 'release' to be 
> removed. I expected that adding debug should have been sufficient. 

IIRC it doesn't have to be removed always, but if you specify both the
setting that was used to compile Qt itself takes precedence. However it
may also be that the check for release is after the one for debug and
thus it takes precedence... You'd have to look into the source to find
out though.

You can also build in both debug and release with Qt4, use 

CONFIG += debug_and_release

for that. You should look into the QMake doc to find out how to rename
the target in case of the debug build, else you'll end up with binaries
that are always debug-built.

> Why is this designed like this?

Ask the qmake devs, somehow I have the impression that qmake isn't
really designed, its rather hacked together (sorry TT).

Andreas

-- 
 [ signature omitted ] 

Message 9 in thread

Thanks for everyone's help!

All of my problems that I have been having over the last couple of days 
appears to have been because of my "fence post" error. (I never knew it 
had a name, and even a Wikipedia entry!) It was causing some of the 
strangest problems I have ever experienced while programming. The glibc 
malloc check was trying to warn me but I had no idea what it was saying.

My control is on a tab widget and when I selected the tab, my app would 
go crazy and then segfault. When I tried debugging it the frame stack 
was pointing me in an entirely different (and wrong) direction. I was 
starting to blame Qt and/or the compiler! After all, how could I be 
wrong!? Who would have guessed that such a small mistake could have such 
far reaching consequences?

Anyway, thanks everybody for your help!

John

--
 [ signature omitted ] 

Message 10 in thread

Man, am I dumb or what!?

Good catch, Reinhardt!

John

--
 [ signature omitted ] 

Message 11 in thread

On 5/1/07, John Voltz <ninevoltz@xxxxxxxxxxxx> wrote:

> QCheckBox       *CheckBox[10];

> for (int i = 0; i < 11; i++) {
>     CheckBox[i] = new QCheckBox(parent);
> }

You're exceeding the bounds of the CheckBox array (by accessing an
eleventh element, but the size is only 10).

-- 
 [ signature omitted ] 

Message 12 in thread

On 5/1/07, Andrew Medico <a.medico@xxxxxxxxx> wrote:
> On 5/1/07, John Voltz <ninevoltz@xxxxxxxxxxxx> wrote:
>
> > QCheckBox       *CheckBox[10];
>
> > for (int i = 0; i < 11; i++) {
> >     CheckBox[i] = new QCheckBox(parent);
> > }
>
> You're exceeding the bounds of the CheckBox array (by accessing an
> eleventh element, but the size is only 10).

Since no one else has brought it up, I'd suggest using a Qt container
here - such as QList (http://doc.trolltech.com/4.2/qlist.html). The
containers provide bounds-checked append/insert/get operators that
make maintainence and debugging much easier.

For example, attempting to use the operator[] to access an
out-of-bounds index gives you the following assertion:

ASSERT failure in QList<T>::operator[]: "index out of range", file
/usr/local/Trolltech/Qt-4.2.3/lib/QtCore.framework/Headers/qlist.h,
line 376

Which is much more helpful than a generic "bus error" and allows you
to go to the responsible line of your code in the debugger since the
assertion is thrown before the function returns.

-- 
 [ signature omitted ]