PDA

View Full Version : Memory Error on Deleting a widget



Harm
21st November 2011, 20:00
Hi together,

I´m quite confused, what´s wrong in my programm. I installed qwt 6.0.1 on qt 4.7.2. Had some smaller problems on the creator plugin, but so long no problem, I fixed this issue.
Now I create a new dialog, with a QTabWidget item and placed a QwtPlot in it.
So far no problem. Because I like the whole thing, I introduced the lib in my project.
Compiled as debug, fixed missing headers and library references (Ok, it´s the debug build, no problem, checked this also for qwtd.lib and dll).
But when I close the dialog again my VS stops with a memory corruption from QwtScaleDiv.

I´m quite helpless, tried to strip the whole down, erased the tabwidget and so on, but have no idea, what´s wrong with the system. I have no drawing, or something else, only a nude widget.

Any help is appreciated,
kind regards,
Thomas

mvuori
21st November 2011, 21:52
I have no drawing, or something else, only a nude widget.

I don't know anything about the classes involded. But just a guess: maybe you should have something other than a nude widget... as the corruption happens when you close the dialog, it might be that a destructor assumes something to contain something -- and if it doesn't, things get messed up. But doing just a little drawing might fix things.

Uwe
22nd November 2011, 07:03
But when I close the dialog again my VS stops with a memory corruption from QwtScaleDiv.

Nobody can tell you what you are doing wrong without knowing any code, but my guess is, that you are passing some parameters from the stack, that should be from the heap.

Uwe

Harm
22nd November 2011, 17:34
Hi Uwe,


Nobody can tell you what you are doing wrong without knowing any code, but my guess is, that you are passing some parameters from the stack, that should be from the heap.

I would be happy, if there would be some code which causes the error. I only have a small screen:
7121

I just inserted the plot widget, start the application, open the dialog, close it via close button and get a crash. I will try to implement some data though. Propably the lib is only shy because of painting a blank diagram.

Kind regards
Thomas

Harm
22nd November 2011, 21:14
I just inserted the plot widget, start the application, open the dialog, close it via close button and get a crash. I will try to implement some data though. Propably the lib is only shy because of painting a blank diagram.


Ok, drilled through the code a littlebit. I inserted some code in the constructor to have some data displayed.



Q_GraphView::Q_GraphView(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

QVector<QPointF> averageData;
averageData.push_back( QPointF(0,0) );
averageData.push_back( QPointF(1,1) );
averageData.push_back( QPointF(2,5) );
QwtPlotCurve *curve = new QwtPlotCurve( "Test" );
curve->setRenderHint( QwtPlotItem::RenderAntialiased );

QwtSymbol *symbol = new QwtSymbol( QwtSymbol::XCross );
symbol->setSize( 4 );
symbol->setPen( QPen( QColor( "red" ) ) );
curve->setSymbol( symbol );

curve->setSamples( averageData );
curve->attach( ui.PlotArea );



What I found out is the following: the data displays well, so far all ok, but the programm crashs in the destructor of the qwtplot. I will hunt in the detachItems now, because it seems that my problem is buried somewhere in there.

Harm
22nd November 2011, 23:43
Ok, additional, what I found out:
I added
ui.PlotArea->setAutoDelete( false ); in the constructor to avoid deleting the pointers, but the problem walks up the line, a screen from my callstack:
7122

There is an odd reason, where the plot believes it has to be deleted, but I can´t imagine why. Besides, I tried to code the plot as well, but the result is the same. :-(

Spitfire
23rd November 2011, 09:35
Try the same without using qt designer.
Just simple main window with plot set as a central widget.

Harm
23rd November 2011, 17:17
Try the same without using qt designer.

No Problem, tried with:


Q_GraphView::Q_GraphView(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

m_Plot = new QwtPlot( this );
m_Plot->setAutoDelete( false );

QVector<QPointF> averageData;
averageData.push_back( QPointF(0,0) );
averageData.push_back( QPointF(1,1) );
averageData.push_back( QPointF(2,5) );
QwtPlotCurve *curve = new QwtPlotCurve( "Test" );
curve->setRenderHint( QwtPlotItem::RenderAntialiased );

QwtSymbol *symbol = new QwtSymbol( QwtSymbol::XCross );
symbol->setSize( 4 );
symbol->setPen( QPen( QColor( "red" ) ) );
curve->setSymbol( symbol );

curve->setSamples( averageData );
curve->attach( m_Plot );
}


So I tried some tricks with the lib :-)

Ahhh and of course, I defined a QwtPlot pointer in the header file.

FelixB
24th November 2011, 09:27
which objects do you delete manually in the destructor (or elsewhere)?

Harm
24th November 2011, 16:59
None, I have only an empty destructor. I`m relying on the "Garbage Collector" of QT.

It looks like there is a point in QwtPlotDict where the plot is deleted and the widget wants to delete it afterwards...

BTW I checked with the examples, no problems with them. I suspect, it may be an issue with using plots in dialogs. Is anybody here, who can give a small example using a plot in a dialog?

Uwe
25th November 2011, 09:36
The problem is in


your code
your build environment

When you post the complete code of a crashing demo we could check at least 1).

Uwe

Spitfire
25th November 2011, 11:24
ui.setupUi(this);



I still think it's the designer which you're using to create the dialog.


#include "mydialog.h"

#include <QVBoxLayout>

#include <qwt_plot.h>

MyDialog::MyDialog(QWidget *parent) :
QDialog(parent)
{
QVBoxLayout* lay = new QVBoxLayout();

QwtPlot* plot = new QwtPlot(this);
lay->addWidget( plot );

this->setLayout(lay);
}Works perfectly fine here.

Harm
29th November 2011, 19:13
Hi all,

I found my error. Investigating the error message from the library I got the, which led me to MSDN Explanation (http://msdn.microsoft.com/en-us/library/ms235460(v=vs.80).aspx). So I checked my settings, Qwt is compiled with /MDd, whilst my programm is compiled with /MTd. This gives you the small difference, that I link static against CRT, and Qwt uses the dynamic CRT.
After creating a new DLL for my purpose and compile it with /MDd as well the programm returns no crash! Excellent! :)

I'm sorry, but I'm not able to compile my program with /MDd, because I use some other demanding libs which demand to be compiled in this manner. But gladfully the workaround helps me on my problem!

Thanks all for your hints!

Thomas