[Qt-interest] memory problem
Sujan Dasmahapatra
sdh at lmglasfiber.com
Tue Mar 24 12:05:07 CET 2009
No Bailly they are class members I wrote it like that for convenience
basically they belowng to TableWidget class
Kind Regards,
Sujan Dasmahapatra
-----Original Message-----
From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of Yves Bailly
Sent: Tuesday, March 24, 2009 4:32 PM
Cc: qt-interest at trolltech.com
Subject: Re: [Qt-interest] memory problem
Sujan Dasmahapatra wrote:
> Dear Friends I am getting segmentation fault for some memory
> allocation.Could u tell me anyone whats wrong in this code below.
>
>
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////
> // In the header file:
> QList <QTableWidget *> item;
> QList <Geometry *> geoMetry;
> int count;
Do you mean those are global variables, not class members? This is
usually seen as a very (very) bad idea.
>
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////
>
> TableWidget::TableWidget()
> {
> count = 0;
> }
>
> TableWidget::~TableWidget()
> {
> If(count != 0)
Better: if ( not item.isEmpty() )
> {
> delete item[count];
If "count" holds the number of items (i.e. "item.size()"), then
item[count]
is one-past the last item, i.e. is invalid.
> delete geoMetry[count];
Same comment.
> }
> }
>
> TableWidget::fileNameUpdate(QString filename, Geometry *geometry)
> {
> Item[count] = new QTableWidgetItem(filename,0);
Assuming "Item" is in fact "item"...
This is wrong: initially "count" is 0 and "item" is empty. So "item[0]"
doesn't exists. So the previous line is wrong.
> setItem(count,0,item[count]);
> geoMetry[count] = new Geometry;
Same comment.
> geoMetry = geometry;
There I don't get at all. "geoMetry" is of type "QList<Geometry*>",
while
"geometry" is of type "Geometry*"... so they're inconsistent, you should
not
do that. Does it compiles? without even a little warning?
> count ++;
> }
>
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
///////
>
> Remember geometry and geometry are different here. geometry is a QList
> pointer and geometry is Geometry. Can u check the code and let me know
> if there's any reason for segmentation fault.The way I am deleting is
> correct or not.?????
No it's not. Never, NEVER name a parameter using the same name as a
global
or local variable. Never, even if it's syntactically correct.
--
/- Yves Bailly - Software developper -\
\- Sescoi France http://www.sescoi.fr -/
"The possible is done. The impossible is being done. For miracles,
thanks to allow a little delay."
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list