Memory problem - malloc, pointers and vector

This is a discussion on Memory problem - malloc, pointers and vector within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by tabstop That's one too many times. The layers in the vector and the layer sent to the ...

  1. #16
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Quote Originally Posted by tabstop View Post
    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.
    No, it is not that simple. Each layer contains its own pointer and memory buffer. The layers in the vector gets freed on exit but the one sent to the undo system is not in the vector then so freeing things in the vector doesn't cause any issues the first time around. The second time the vector is filled in the undo pointer starts pointing to the same memory as the vector layer though for some reason so yes they share memory in some way.
    home = hat.position;

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by DrSnuggles View Post
    The layers in the vector gets freed on exit but the one sent to the undo system is not in the vector then so freeing things in the vector doesn't cause any issues the first time around.
    But it's a copy, you said. If you're just copying the pointer, then they point to the same place.

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So does your code make sure that no memory block is referenced both in the vector and in the undo system?

    I have a feeling that it takes psychic powers to tell what the problem is without any actual code.
    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).

  4. #19
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    I think I may be confusing myself here a bit too.... I found the issue. The fact that I'm undoing the layer delete makes the undo buffer in fact get the deleted on tool exit because I push back the same layer onto the vector... ahhrg. The strange thing is it did not complain when the undo destructor was called, only later when trying to use the buffer. I just need to check in the undo class if the layer has been pushed back and if so do not delete the buffer.

    So problem solved, thanks a lot for helping out!
    home = hat.position;

Page 2 of 2 FirstFirst 12
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, 02:13 PM
  3. vector of pointers
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21