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. issue regarding deleting an object in qt with delete and free
Forum Updated to NodeBB v4.3 + New Features

issue regarding deleting an object in qt with delete and free

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 1.9k Views 1 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.
  • S Offline
    S Offline
    sierdzio
    Moderators
    wrote on 15 May 2019, 11:02 last edited by
    #9

    @SherifOmran said in issue regarding deleting an object in qt with delete and free:

    ClassList::ClassList()
    {

    //main
    head = new Node;
    head=NULL;
    

    Huh? Looks like an instant memory leak.

    (btw. you should use nullptr)

    Frankly, I don't have time to analyse your whole code, sorry. But perhaps try running it through valgrind and address sanitizer - it should pick all the leaks for you.

    (Z(:^

    1 Reply Last reply
    4
    • S SherifOmran
      15 May 2019, 10:54

      This is the whole error message when i remove the null

      linkelist(34449,0x7fffa95e1380) malloc: *** error for object 0x3: pointer being freed was not allocated
      *** set a breakpoint in malloc_error_break to debug

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 May 2019, 11:02 last edited by
      #10

      @SherifOmran As far as I can see you did not initialise temp and try to delete it! No wonder it does not work.

      ClassList::ClassList()
      {
          head = new Node;
          head=NULL; // Why do you assign NULL after creating instance?!
      }
      
      ClassList::~ClassList()
      {
          delete temp; //Check whether temp points to anything before deleting
          delete head;
      }
      

      Change

      private:
          Node* head = nullptr; //number 1
          Node* current = nullptr; //current node
          Node* temp = nullptr;
          Node* temp2 = nullptr;
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • S Offline
        S Offline
        SherifOmran
        wrote on 15 May 2019, 11:03 last edited by
        #11

        what caused confusion is that i delete L at line 51 and it was accepted

        J 1 Reply Last reply 15 May 2019, 11:06
        0
        • S Offline
          S Offline
          SherifOmran
          wrote on 15 May 2019, 11:05 last edited by
          #12

          @jsulm
          because i want to check that the head it Null at the initialization

          J 1 Reply Last reply 15 May 2019, 11:08
          0
          • S SherifOmran
            15 May 2019, 11:03

            what caused confusion is that i delete L at line 51 and it was accepted

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 15 May 2019, 11:06 last edited by
            #13

            @SherifOmran

            delete L; // L became a null pointer
            

            The comment is wrong: the value of the pointer does not change when you call delete on it.

            delete L;
            L = nullptr; // Now it is null pointer
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • S Offline
              S Offline
              SherifOmran
              wrote on 15 May 2019, 11:07 last edited by
              #14

              yes thank you i corrected this comment

              1 Reply Last reply
              0
              • S SherifOmran
                15 May 2019, 11:05

                @jsulm
                because i want to check that the head it Null at the initialization

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 15 May 2019, 11:08 last edited by
                #15

                @SherifOmran said in issue regarding deleting an object in qt with delete and free:

                @jsulm
                because i want to check that the head it Null at the initialization

                As @sierdzio already told you: you created a memory leak this way.
                Just remove

                head = new Node;
                

                Else you create an instance and loose the pointer to it, so you never delete this instance -> memory leak.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SherifOmran
                  wrote on 15 May 2019, 11:10 last edited by
                  #16

                  yes i see.
                  how i delete temp that was initialized in ClassList::InsertNode ?

                  J J 2 Replies Last reply 15 May 2019, 11:11
                  0
                  • S SherifOmran
                    15 May 2019, 11:10

                    yes i see.
                    how i delete temp that was initialized in ClassList::InsertNode ?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 15 May 2019, 11:11 last edited by
                    #17

                    @SherifOmran said in issue regarding deleting an object in qt with delete and free:

                    how i delete temp that was initialized in ClassList::InsertNode ?

                    delete temp;
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • S SherifOmran
                      15 May 2019, 11:10

                      yes i see.
                      how i delete temp that was initialized in ClassList::InsertNode ?

                      J Offline
                      J Offline
                      JonB
                      wrote on 15 May 2019, 11:13 last edited by JonB
                      #18

                      @SherifOmran

                      how i delete temp that was initialized in ClassList::InsertNode ?

                      As @jsulm says it's still delete temp, but make sure you initialize temp = nullptr in either the declaration or the constructor, ClassList::InsertNode might never get called....

                      1 Reply Last reply
                      2
                      • S Offline
                        S Offline
                        SherifOmran
                        wrote on 15 May 2019, 11:20 last edited by
                        #19

                        thank you guys for suggested corrections

                        1 Reply Last reply
                        0

                        18/19

                        15 May 2019, 11:13

                        • Login

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