By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

copying a deque with dynamically allocated objects - memory leak?

P: 1
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
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
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.