Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get error message double free or corruption (out) when using QMap class
Forum Updated to NodeBB v4.3 + New Features

Get error message double free or corruption (out) when using QMap class

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.0k Views 2 Watching
  • 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.
  • H Offline
    H Offline
    HYKim
    wrote on 19 Jan 2016, 10:33 last edited by
    #1

    I wanted to use QMap class like this,

    QMap<int,QWidget*> _mapSystemId2vehicleItem;
    

    So I wrote it in my header file like this.

    #ifndef HEADER_H
    #define HEADER_H
    
    #include <QWidget>
    #include <QMap>
    
    class MyClass : public QWidget
    {
        Q_OBJECT
    public:
        MyClass();
        ~MyClass();
    signals:
    
    public slots:
    
    private:
        QWidget*        _childwidget;
    
        QMap<int,QWidget*> _mapSystemId2vehicleItem; //I added this.
    };
    
    #endif // HEADER_H
    

    But when running project exited, I got a message.

    =======================================
    *** Error in `/home/hykim/QtWorkSpace/build-160115_multiVehicleTest-Desktop_Qt_5_5_1_GCC_32bit-Debug/160115_multiVehicleTest': double free or corruption (out): 0x089c2c18 ***
    The program has unexpectedly finished.

    So I erased QMap<int,QWidget*> _mapSystemId2vehicleItem;. and then application didn't put any error.

    I don't know the reason why this error appeared.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 19 Jan 2016, 10:38 last edited by
      #2

      What is the code in your destructor? I think you deleted your QWidget instances manually, then the parent-children mechanism tries to delete them again.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      H 1 Reply Last reply 19 Jan 2016, 11:00
      0
      • J JohanSolo
        19 Jan 2016, 10:38

        What is the code in your destructor? I think you deleted your QWidget instances manually, then the parent-children mechanism tries to delete them again.

        H Offline
        H Offline
        HYKim
        wrote on 19 Jan 2016, 11:00 last edited by
        #3

        @JohanSolo
        Nothing in MyClass's destructor.

        MyClass::~MyClass(){
        }
        

        But in MainWindow.cc, ~MyClass is called its destructor.

        MainWindow::~MainWindow()
        {
            myClass->~MyClass();
            delete ui;
        }
        
        1 Reply Last reply
        0
        • J Offline
          J Offline
          JohanSolo
          wrote on 19 Jan 2016, 11:01 last edited by JohanSolo
          #4

          I don't like this line (is it just me?):

          myClass->~MyClass();
          

          If you want to free the allocated memory, why don't you simply call delete myClass?

          Edit:
          I think this is only wiping out the content of you MyClass instance, but the allocated memory is never freed, which then may trigger the error when the MainWindow destructor trips on the myClass pointer.

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          H 1 Reply Last reply 19 Jan 2016, 11:05
          1
          • J JohanSolo
            19 Jan 2016, 11:01

            I don't like this line (is it just me?):

            myClass->~MyClass();
            

            If you want to free the allocated memory, why don't you simply call delete myClass?

            Edit:
            I think this is only wiping out the content of you MyClass instance, but the allocated memory is never freed, which then may trigger the error when the MainWindow destructor trips on the myClass pointer.

            H Offline
            H Offline
            HYKim
            wrote on 19 Jan 2016, 11:05 last edited by
            #5

            @JohanSolo Oh, the problem solved.
            Thank you for your advice.

            J 1 Reply Last reply 19 Jan 2016, 11:06
            0
            • H HYKim
              19 Jan 2016, 11:05

              @JohanSolo Oh, the problem solved.
              Thank you for your advice.

              J Offline
              J Offline
              JohanSolo
              wrote on 19 Jan 2016, 11:06 last edited by
              #6

              @HYKim said:

              @JohanSolo Oh, the problem solved.
              Thank you for your advice.

              You're welcome, I'm glad I could help!

              `They did not know it was impossible, so they did it.'
              -- Mark Twain

              V 1 Reply Last reply 19 Jan 2016, 13:32
              0
              • J JohanSolo
                19 Jan 2016, 11:06

                @HYKim said:

                @JohanSolo Oh, the problem solved.
                Thank you for your advice.

                You're welcome, I'm glad I could help!

                V Offline
                V Offline
                ValentinMichelet
                wrote on 19 Jan 2016, 13:32 last edited by ValentinMichelet
                #7

                @JohanSolo said:

                I don't like this line (is it just me?):

                myClass->~MyClass();
                

                I would like to give my two cents here, for potential interested readers.
                Calling explicitly a destructor is really rare, and you have to do it very carefully.
                Basically, you explicitly invoke a destructor when you need to destroy something without freeing the memory (both are done when calling delete). This may happen if you implement a container (a std::vector like for instance) and you provide a pop_back() method: the element has to be destroy while the memory is managed separately.

                For more information, here is an interesting topic on Stack Overflow:
                http://stackoverflow.com/questions/16720201/calling-destructor-explicitly

                1 Reply Last reply
                2

                1/7

                19 Jan 2016, 10:33

                • Login

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