Thread: Memory problem - malloc, pointers and vector

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes that should just copy the pointer, after that the layer in the vector is deleted so the pointer should only exist in the copied layer at that point. I set the pointers to NULL after freeing in the destructor but somehow when layers are created again a pointer is created to the same memory.

    There is no special implementation of copy or assignment.
    It's kind of hard to follow but I think you should google "The rule of three".

    Here's a quick example of what will happen if you violate it.

    Code:
    #include <iostream>
    #include <vector>
    
    struct CopyingBroken
    {
        int* p;
        CopyingBroken(int value): p(new int(value)) {}
        ~CopyingBroken()
        {
            std::cout << p << " destructor\n";
            delete p;
            p = 0;
        }
    
        //if you don't implement anything better, this is what the compiler provides for you
        //the pointer is shared but you have no idea how many other instances might be there,
        //all pointing to the same int
        CopyingBroken(const CopyingBroken& other): p(other.p)
        {}
        const CopyingBroken& operator= (const CopyingBroken& other)
        {
            p = other.p; //leaks memory held by p, and makes this share the pointer with other
            return *this;
        }
    };
    
    int main()
    {
        std::vector<CopyingBroken> vec;
        CopyingBroken broken(1);
        vec.push_back(broken);
    }
    My output (of course the program could do anything since the result of double deletion is undefined):

    0x3e2450 destructor
    0x3e2450 destructor
    One of the objects is called broken, and a copy is stored in the vector. When they go out of scope, they each deallocate the same memory. Setting a member's value to NULL in the destructor has naturally no effect to any other object, similarly as you won't expect that effect from this simple code:

    Code:
    int a = 10;
    int b = a;
    a = 0;
    //what is the value of b?
    Last edited by anon; 08-14-2009 at 08:03 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Thanks anon, yes the pointer is shared after it is copied which could be hazardous but directly after being copied the layer where it was copied from is removed from the vector without deleting any of its memory. That memory should then be pointed to only by the copied layer.

    I free the memory of the layers manually when I delete them otherwise. It is only the one in the undo system that has the automatic destructor. Could it be that since the vector is reused and cleared that the layer pointer will get the same adress as last time and therefore point to the same memory in some way? I don't get why malloc would not allocate completley new memory though when new layer pointers are created from scratch using malloc.

    int a = 10;
    int b = a;
    a = 0;
    //what is the value of b?
    Well, b is 10...
    Last edited by DrSnuggles; 08-14-2009 at 08:36 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by DrSnuggles View Post
    I free the memory of the layers manually when I delete them otherwise. It is only the one in the undo system that has the automatic destructor. Could it be that since the vector is reused and cleared that the layer pointer will get the same adress as last time and therefore point to the same memory in some way? I don't get why malloc would not allocate completley new memory though when new layer pointers are created from scratch using malloc.
    Because it is. (Once the memory has been freed, it is completely new memory again.) If you are holding a pointer to memory that has been freed, then that is your error, not the system's.

    What kind of error are you getting? I've kind of been assuming it was a double delete, but looking back I'm not entirely sure.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by tabstop View Post
    Because it is. (Once the memory has been freed, it is completely new memory again.) If you are holding a pointer to memory that has been freed, then that is your error, not the system's.

    What kind of error are you getting? I've kind of been assuming it was a double delete, but looking back I'm not entirely sure.
    The thing is memory is not freed in the undo system for use by the system, until after I have deleted all remaining layers and recreated them. Freeing the memory in the undo layer will then also free the memory in the corresponding layer in the vector, even though the vector has been filled up with layers and pointers created from scratch and new memory has been allocated for each new layer. Freeing the undo memory then shouldn't affect the layers in the vector in any way... but it does.

    I do get the double freeing error when trying to delete the layers in the vector when exiting the tool but the first issue I get is that it tries to acces memory in a buffer that has already been freed by the undo layer destructor.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by DrSnuggles View Post
    The thing is memory is not freed in the undo system for use by the system, until after I have deleted all remaining layers and recreated them. Freeing the memory in the undo layer will then also free the memory in the corresponding layer in the vector, even though the vector has been filled up with layers and pointers created from scratch and new memory has been allocated for each new layer. Freeing the undo memory then shouldn't affect the layers in the vector in any way... but it does.

    I do get the double freeing error when trying to delete the layers in the vector when exiting the tool but the first issue I get is that it tries to acces memory in a buffer that has already been freed by the undo layer destructor.
    I'm a bit confused here. Where do you call free -- in the destructor, in this mysterious "undo layer", both, or neither?

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by tabstop View Post
    I'm a bit confused here. Where do you call free -- in the destructor, in this mysterious "undo layer", both, or neither?
    For the layers in the vector I call free when the tool exists, then I empty the vector.

    For the layer that is sent to the undo system free is called on the layer when the destructor of the undo class that holds the layer is called. That doesn't happen in my testing until after the tool has exited and started again and the layers in the vector has been recreated. Freeing the undo layer memory will then also free the memory used in the vector.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by DrSnuggles View Post
    For the layers in the vector I call free when the tool exists, then I empty the vector.

    For the layer that is sent to the undo system free is called on the layer when the destructor of the undo class that holds the layer is called. That doesn't happen in my testing until after the tool has exited and started again and the layers in the vector has been recreated. Freeing the undo layer memory will then also free the memory used in the vector.
    That's one too many times. The layers in the vector and the layer sent to the undo system contain the same pointer, and cannot both be freed without causing double delete issues.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  2. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  3. vector of pointers
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 10:49 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM