Destructor array of object
-
wrote on 17 Feb 2012, 13:30 last edited by
If i run valgrind its say so that i've memory lost in
@
test = new Test();
@So is correct use delete on descrutor? But i don't understand why my app crash for corrupted double-linked.
-
wrote on 17 Feb 2012, 13:48 last edited by
The code looks ok. I don't know what's going wrong here. Can you provide a complete test case?
-
wrote on 17 Feb 2012, 15:46 last edited by
After hours i found the problem. The problem is a desctructor of QString. I have one class that load some string from a file and store it in a vector (now i can use a QList but this is old code). This vector is used by some class and is copy. In this old project there aren't delete and the program have much memory leak. The code is this:
@//Class that load string
class Loader
{
public:
Loader();
~Loader();
QString* getList();
private:
QString str[500];
};Loader::Loader()
{
for(quint16 a=0;a<500;a++)
str[a] = "a"; //This is example fill str with 'a'
}//This is another class that have loader class and call descructor
class Test : public QDialog, private Ui::Dprodotti {
Q_OBJECT
public:
Test(QWidget *parent = 0);
~Test();private:
Loader *loader;
QString myStr[500];private slots:
void on_pushButton_clicked();
};Test::Test(QWidget *parent) : QDialog(parent)
{
setupUi(this);loader = new Loader(); memcpy(myStr,product->getList(),sizeof(myStr)); setAttribute(Qt::WA_DeleteOnClose);
}
Test::~Test()
{
delete loader;
}void Test::on_pushButton_clicked()
{
close();
}@There something wrong in that QString vector but i don't understand what...
-
wrote on 17 Feb 2012, 16:14 last edited by
Use [[Doc:QStringList]] and get rid of memcopy and all the dangerous pointer stuff. It's not necessary here.
21/24