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

Qt-interest Archive, July 2007
Heap Corruption on VS-2005 with QT 4.2.2


Message 1 in thread

Hi All,

I'm relatively new to QT and have started working on it around 3 months back
for the first time. I'm trying to build an application with a MainWindow
(inherited from QMainWindow). this mainwindow has exactly 217 different
widgets (including 3 custom QWidgets , and QLabel, qPushButtons , QFrames,
Qcombobox, Qpixmap, Q radiobuttons so on so forth).

All these 217 are creating on the heap using pointers. the pointer (Qlabel*
someLabel) are declared in the header file and they are created in the
constructor (someLabel = new QLabel(this)). thus all 217 widgets are child
of the mainwindow.

all these widgets are created once and not deleted explicitly in the
destructor as i believe QT deletes all its child widgets on termination.
i'm creating the mainwindow in main.cpp on the stack.

MainWindow MainWindow1;
 MainWindow1.show();
return app.exec();

I'm using Microsoft Visual studio 2005 for compilation and debugging on a
windows 32 bit platform. i'm having a problem "sometimes" of heap corruption
on execution. it also happens if i create one more widget on the heap too.
i've got a linklist which stores my data (on the heap) , the constructor
call is fine and memory is allocated exactly it should, however some time
later (after a few different clicks) the data in the link list gets
automatically corrupted and the first pointer in the linklist which should
show null actually has some garbage in it. thereby making the object
unusable.

i changed my linker setting of the project from /Subsystem:windows to
/Subsystem:not set. it reduces the occurence of this heap error. however it
resurfaces time and again.

i somehow suspect that memory corrupts due to memory management by visual
.net architecture (or the lack of it) . i've also tried to change the heap
commit/reserve and stack commit/reserve size in the linker setting but to no
avail.

can someone please help me out with this problem. give me the path i can
follow to solve my problem. what should i look for. i'm not attaching my
code as it is more than 8000 lines. and would not solve anything.

Regards
-- 
 [ signature omitted ] 

Message 2 in thread

Well... It sounds like you have multiple references to the same pointer,
and somewhere someone is deleteing them...

 

If you cant attach reproducible source code, your not gonna get much
help... I would reduce the 217 widgets into something smaller that
reproduces the problem...  Post it, and see if someone can help.

 

However, something you said seems wrong... Not all your widgets should
be children of the mainwindow... I guess its possible, but usually, your
gonna have some sort of layouts, and container widgets... So only the
"top level" children, maybe only the top layout, will be an actual child
of the mainwindow.  


The rest will be children of those top level children.. and so on and so
forth.

 

As to the linked list, if its going corrupt, someone is deleting it...
Put a breakpoint in ~QObject or ~QWidget and see where it is..

 

Scott

________________________________

From: Harsh Vardhan [mailto:vardhan.harsh@xxxxxxxxx] 
Sent: Friday, July 13, 2007 9:59 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: Heap Corruption on VS-2005 with QT 4.2.2

 

Hi All,

I'm relatively new to QT and have started working on it around 3 months
back for the first time. I'm trying to build an application with a
MainWindow (inherited from QMainWindow). this mainwindow has exactly 217
different widgets (including 3 custom QWidgets , and QLabel,
qPushButtons , QFrames, Qcombobox, Qpixmap, Q radiobuttons so on so
forth). 

All these 217 are creating on the heap using pointers. the pointer
(Qlabel* someLabel) are declared in the header file and they are created
in the constructor (someLabel = new QLabel(this)). thus all 217 widgets
are child of the mainwindow. 

all these widgets are created once and not deleted explicitly in the
destructor as i believe QT deletes all its child widgets on termination.

i'm creating the mainwindow in main.cpp on the stack. 

MainWindow MainWindow1; 
 MainWindow1.show();
return app.exec();

I'm using Microsoft Visual studio 2005 for compilation and debugging on
a windows 32 bit platform. i'm having a problem "sometimes" of heap
corruption on execution. it also happens if i create one more widget on
the heap too. i've got a linklist which stores my data (on the heap) ,
the constructor call is fine and memory is allocated exactly it should,
however some time later (after a few different clicks) the data in the
link list gets automatically corrupted and the first pointer in the
linklist which should show null actually has some garbage in it. thereby
making the object unusable. 

i changed my linker setting of the project from /Subsystem:windows to
/Subsystem:not set. it reduces the occurence of this heap error. however
it resurfaces time and again. 

i somehow suspect that memory corrupts due to memory management by
visual .net architecture (or the lack of it) . i've also tried to change
the heap commit/reserve and stack commit/reserve size in the linker
setting but to no avail. 

can someone please help me out with this problem. give me the path i can
follow to solve my problem. what should i look for. i'm not attaching my
code as it is more than 8000 lines. and would not solve anything. 

Regards
-- 
 [ signature omitted ] 

Message 3 in thread

  I had similar problems in the same development environment. In my case it
turned out I did not link the right Qt libraries to my project. If you are
working in "Debug" configuration, make sure all Qt libraries you link to are
ending on d.lib or d4.lib, e.g. qtmaind.lib, QtCored4.lib and so on.

Message 4 in thread

Are you using any 3rd party libraries?
If so, check that they are using the debug version of the CRT library
when you're compiling your application in debug. Your application and
its libraries cannot be using both the debug and release version of the
CRT, since that we create Heap problems/corruptions.

You can simply try running your application in release and see if the
corruptions stop. If so, that alone is a good indicator of what's going
on. If it doesn't help, you should probably try to reduce your code to
something small and reproducible.

--
 [ signature omitted ] 

Attachment: signature.asc
Description: OpenPGP digital signature


Message 5 in thread

I apologize to the delayed reply but i was busy working all the bugs for the
release to be out on time.

As Seneca right pointed out i was doing the same mistake he did, i was
attaching the wrong release libraries for my debug configuration and had not
worked with the release version yet. however after attaching the debug
libraries, everything just worked fine, (although i did find some logical
errors in my code) , but in the end everything worked out.

Once again thanks a lot guys to help me out.

cheers
Harsh


On 7/14/07, Marius Storm-Olsen <marius@xxxxxxxxxxxxx> wrote:
>
> Are you using any 3rd party libraries?
> If so, check that they are using the debug version of the CRT library
> when you're compiling your application in debug. Your application and
> its libraries cannot be using both the debug and release version of the
> CRT, since that we create Heap problems/corruptions.
>
> You can simply try running your application in release and see if the
> corruptions stop. If so, that alone is a good indicator of what's going
> on. If it doesn't help, you should probably try to reduce your code to
> something small and reproducible.
>
> --
> .marius
>
> > I'm relatively new to QT and have started working on it around 3 months
> > back for the first time. I'm trying to build an application with a
> > MainWindow (inherited from QMainWindow). this mainwindow has exactly 217
> > different widgets (including 3 custom QWidgets , and QLabel,
> > qPushButtons , QFrames, Qcombobox, Qpixmap, Q radiobuttons so on so
> forth).
> >
> > All these 217 are creating on the heap using pointers. the pointer
> > (Qlabel* someLabel) are declared in the header file and they are created
> > in the constructor (someLabel = new QLabel(this)). thus all 217 widgets
> > are child of the mainwindow.
> >
> > all these widgets are created once and not deleted explicitly in the
> > destructor as i believe QT deletes all its child widgets on termination.
> > i'm creating the mainwindow in main.cpp on the stack.
> >
> > MainWindow MainWindow1;
> >  MainWindow1.show();
> > return app.exec();
> >
> > I'm using Microsoft Visual studio 2005 for compilation and debugging on
> > a windows 32 bit platform. i'm having a problem "sometimes" of heap
> > corruption on execution. it also happens if i create one more widget on
> > the heap too. i've got a linklist which stores my data (on the heap) ,
> > the constructor call is fine and memory is allocated exactly it should,
> > however some time later (after a few different clicks) the data in the
> > link list gets automatically corrupted and the first pointer in the
> > linklist which should show null actually has some garbage in it. thereby
> > making the object unusable.
> >
> > i changed my linker setting of the project from /Subsystem:windows to
> > /Subsystem:not set. it reduces the occurence of this heap error. however
> > it resurfaces time and again.
> >
> > i somehow suspect that memory corrupts due to memory management by
> > visual .net architecture (or the lack of it) . i've also tried to change
> > the heap commit/reserve and stack commit/reserve size in the linker
> > setting but to no avail.
> >
> > can someone please help me out with this problem. give me the path i can
> > follow to solve my problem. what should i look for. i'm not attaching my
> > code as it is more than 8000 lines. and would not solve anything.
> >
>
>
>
>


-- 
 [ signature omitted ]