Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Destructor array of object
Forum Update on Monday, May 27th 2025

Destructor array of object

Scheduled Pinned Locked Moved C++ Gurus
24 Posts 5 Posters 14.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stukdev
    wrote on 17 Feb 2012, 13:30 last edited by
    #21

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 17 Feb 2012, 13:48 last edited by
      #22

      The code looks ok. I don't know what's going wrong here. Can you provide a complete test case?

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stukdev
        wrote on 17 Feb 2012, 15:46 last edited by
        #23

        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...

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on 17 Feb 2012, 16:14 last edited by
          #24

          Use [[Doc:QStringList]] and get rid of memcopy and all the dangerous pointer stuff. It's not necessary here.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0

          21/24

          17 Feb 2012, 13:30

          • Login

          • Login or register to search.
          21 out of 24
          • First post
            21/24
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved