468,741 Members | 1,742 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,741 developers. It's quick & easy.

copying a deque with dynamically allocated objects - memory leak?

Here's my problem:

I have a class A that uses new to create objects of class B and push them onto a deque, like this (code stripped down for clarity):

Expand|Select|Wrap|Line Numbers
  1. void ImageOperations::FormMask() {
  2.     std::deque<BoundingBox*> boundBoxDeque;
  3.     if (condition met) {
  4.        BoundingBox pBox = new BoundingBox(x,y,z);
  5.        if (second condition met) {
  6.           boundBoxDeque.push_back(pBox);
  7.        }   
  8.        else {
  9.            delete pBox;
  10.        }
  11.    }
Then, later in the same function, I want to send the deque with the boxes in it as a paramter to a constructor for a different class C, like this:

Expand|Select|Wrap|Line Numbers
  1.    if (boundBoxDeque.size() > 0)  {
  2.       Mask = new ImageMask(boundingBoxDeq);
  3.    }
The constructor for Mask does this:

Expand|Select|Wrap|Line Numbers
  1. ImageMask::ImageMask(&boundingBoxDeq) {
  2.     new_deq = boundingBoxDeq;
  3. }
I have a copy constructor for the BoundingBox class and the program behaves properly (the objects get copied to new_deq) until it seg faults. I know that I am responsible for deleting the BoundingBox objects in the deque, but I can't figure out how to do it. For the new_deq, I have a loop like this in the destructor for the ImageMask class:

Expand|Select|Wrap|Line Numbers
  1. while (new_deq.size() > 0) {
  2.    BoundingBox *tempbox = new_deq.front();
  3.    new_deq.pop_front();
  4.    delete tempbox;
  5. }
But with printf's it doesn't seem like the destructor ever gets called. Occasionally the program runs to completion, and a printf within the destructor never appears. The program usually seg faults at the ImageMask constructor, with a stack trace showing that deque(const allocator_type&_a = allocator_type()) gets called. The trace ends with a call to malloc_consolidate.

Also, I can't figure out how to delete the objects in the original boundBoxDeque. When I put a similar loop after the deque gets passed to the constructor, the boxes seem to be removed from new_deque as well. Can anyone help me out?
May 6 '07 #1
1 1992
9,207 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. ImageMask::ImageMask(&boundingBoxDeq) {
  2.     new_deq = boundingBoxDeq;
  3. }
is suspicious because a pointer is passed in and the new ImageMask object makes a COPY OF THE POINTER rather than a copy of the data pointed at by the argument.

Later this pointer will be deleted either by the new ImageMask object or the object used in the ImageMask constructor. The deque will be deleted leaving the remaining object pointed at deleted memory. A crash will follow shortly.

Generally, classes with pointers cannot make copies of the pointers since when you delete a crash will occur unless you are certain there are no other copies of the pointer elsewhere in the program. That means reference counted pointers and that requires.

I would need to see your code for class B as these are the ones that go in the deque. In addition, I want to see how your are creating the B objects and how you specialize your deque.
May 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Jeff Williams | last post: by
11 posts views Thread by stp | last post: by
21 posts views Thread by Matteo Settenvini | last post: by
3 posts views Thread by Tony Johansson | last post: by
4 posts views Thread by mast2as | last post: by
9 posts views Thread by toton | last post: by
29 posts views Thread by NvrBst | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.