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 SherifOmran

    I am trying to delete an object that has been created. When i gave delete p it did not work, but when i set p=NULL and then delete p it worked. I don't see why ? however when i give free(p) it also works.

    it does not work without p=NULL

    stack *p = new stack();
    for (int i=0; i<4; i++)
    {
        p->push(i);
    }
    for (int i=0; i<4; i++)
    {
        p->pull();
    }
    
    p=NULL; delete p;
    

    error message deleting an object that does not exist if i don't use p=NULL. If i use free (p) instead of p=NULL and delete it works. But what is the explaination?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #4

    @SherifOmran

    but when i set p=NULL and then delete p it worked

    It "worked", in the sense that in this case it does not delete anything!

    You should never free() an object created by new, only delete.

    error message deleting an object that does not exist if i don't use p=NULL

    Sounds more like an error in the destructor for stack referring to something else (e.g. there's something wrong in your push-me-pull-you code)!

    As the others say: post whole message, and show what is going on inside stack?

    1 Reply Last reply
    2
    • S Offline
      S Offline
      SherifOmran
      wrote on last edited by
      #5
      This post is deleted!
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #6

        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

        jsulmJ 1 Reply Last reply
        0
        • S Offline
          S Offline
          SherifOmran
          wrote on last edited by
          #7

          I found the mistake
          it was in

          ClassList::~ClassList()
          {
              //destructor
              //delete temp; /
              delete head;
          }
          

          it was delete temp

          JonBJ 1 Reply Last reply
          2
          • S SherifOmran

            I found the mistake
            it was in

            ClassList::~ClassList()
            {
                //destructor
                //delete temp; /
                delete head;
            }
            

            it was delete temp

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #8

            @SherifOmran

            it was delete temp

            I was just about to tell you that! Your temp wasn't even initialized! As you can see, as predicted the error was indeed inside the destructor for stack, which is only called if you delete stack.

            1 Reply Last reply
            2
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on 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

                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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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 last edited by
                  #11

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

                  jsulmJ 1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SherifOmran
                    wrote on last edited by
                    #12

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

                    jsulmJ 1 Reply Last reply
                    0
                    • S SherifOmran

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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 last edited by
                        #14

                        yes thank you i corrected this comment

                        1 Reply Last reply
                        0
                        • S SherifOmran

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

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 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 last edited by
                            #16

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

                            jsulmJ JonBJ 2 Replies Last reply
                            0
                            • S SherifOmran

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

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 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

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

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 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 last edited by
                                  #19

                                  thank you guys for suggested corrections

                                  1 Reply Last reply
                                  0

                                  • Login

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