[Qt-interest] error
Yves Bailly
yves.bailly at sescoi.fr
Fri Mar 20 09:55:56 CET 2009
Sujan Dasmahapatra wrote:
> I am writing like this for this error
>
> Error:
> *** glibc detected *** double free or corruption (out):
> 0x0000000000768340 ***
>
> ////////////////////////////////////////////////////////////////////////
> ////////
> //In the header file(tablewidget.h)
> QList <QTableWidgetIem *> item;
> QList <Geometry *> geometry;
> QList <QString> filenames;
> int count;
>
> ////////////////////////////////////////////////////////////////////////
> //////////
> //In the source file (tablewidget.cpp)
> Void TableWidget::~TableWidget()
> {
> if(count>0)
> {
> for(int i=0; i<count; i++)
> {
> delete item[count];
> delete geometry[count];
> }
>
> count = 0;
> }
>
> }
>
>
> Void TableWidget::fileNameUpdate(QString filename, Geometry *geometry)
> {
> item[count] = new QTbaleWidgetItem(filename,0);
> filenames.append(filename);
> setItem(count,0,item[count]);
> geometry[count] = new Geometry;
> geometry[count] = geometry;
> count ++;
> }
> ////////////////////////////////////////////////////////////////////////
> ///////////////////
>
> Can anyone plss tell whats the reason I am getting this error.
Because the code you posted is simply horrible.
- what is this type "Void"? I'm only aware of "void"... yes, C++ is
case-sensitive ;
- in ~TableWidget(), why use a loop when you're deleting only
one item, the one having index "count" (which is probably invalid,
as valid indices for an array of size N are between [0, N-1]) ;
- can you explain what the hell you intended to do with
geometry[count] = geometry ??? does it even compiles? if yes,
which compiler are you using?
Today is a good day... let's try this:
void TableWidget::~TableWidget()
{
foreach(QTableWidgetIem* one_item, item)
{
if ( one_item != 0 )
{
delete one_item;
}
}
}
void TableWidget::fileNameUpdate(QString filename,
Geometry* geometry_param)
{
item.append(new QTableWidgetIem(filename, 0));
filenames.append(filename);
setItem(item.size()-1, 0, item.last());
geometry.append(geometry_param);
}
Beware of not naming functions parameters using names already used
before, as you did here... also, there's no need to keep an extra
"count" variable, the QList<> has its own.
Hope this helps.
--
/- 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."
More information about the Qt-interest-old
mailing list