Yesman
Yesman

Reputation: 427

Leaking when tried to free vector element

please help to figure this out. I have some leaking code, and I don't know how to handle it

vector <ItemClass> items( 10 );
items[1] = ItemClass( "DVD Player", 560 );
items[5] = * new ItemClass( "Blu Ray Player", 900 );

How should I free memory for items[5] ? I'm getting error on my attempts of freeing memory

delete &items[5];

delete [] &items[5];

I even tried something like

ItemClass * delItem = &items[5];
items[5] = item4;
delete delItem;

I'm getting "corruption of the heap" in VS2010 Ultimate

Upvotes: 0

Views: 84

Answers (2)

Alex
Alex

Reputation: 5439

There is a strange use and mix up of storing new-allocated objects in vectors in your code. Usually you should handle lists all in the same way. Thus my examples below will be more explicit, and independent from each other which should help you understand the differences.

Use delete for objects only. See code below.

Use delete [] for "native" arrays only, not for vector or similar container classes or objects inside them.

I will not give an example for arrays though, since arrays might be more confusing.

Default example stack:

vector <ItemClass> items( 10 );

// does not need to be deleted because item is on the stack
ItemClass item("device1", "10"); 
items.push_back(item);

Default example with heap allocation:

vector <ItemClass*> items( 10 );

ItemClass* pItem = new ItemClass("device2", "20");
items.push_back(pItem);

// delete all items inside vector
for (int i = 0; i < items.size(); i++)
{
    ItemClass* pToDelete = items[i];
    delete pToDelete;
    items.erase(i);
}

The following examples should be avoided and are for clarifying things up only! Use at own risk.

Storing addresses of stack variables:

vector <ItemClass*> items( 10 );

// does not need to be deleted because item is on the stack
ItemClass item("device3", "30"); 
items.push_back( &item ); // storing a reference to item

// No need to delete this item that points to something on the stack.
// However you might not be able to tell items apart which have been 
// created on the heap or the stack, so just dont do it.

If you mix up with the example before and store references and pointers you must handle it yourself. My recommendation: Do not do this.

Storing dereferenced items:

vector <ItemClass> items( 10 );

ItemClass* pItem = new ItemClass("device4", "40"); 
items.push_back(*pItem);

// must be deleted because allocated on the heap
// Again you can not tell which item is allocated on stack or heap. Avoid this.

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385910

You don't call delete on objects that you don't own. You don't own items[5]. The items vector owns it.

What you need to delete is the ItemClass object that you are currently leaking, which is the one you are creating via new ItemClass(...) and then copying to items[5].

ItemClass *temp = new ItemClass("Blue Ray Player", 900);
items[5] = *temp;
delete temp;

If you want to remove items[5] from the vector, ask the vector to remove it:

items.erase(items.begin() + 5);

Note that this may be rather inefficient, because all later items in the vector have to be moved down one position, which entails calling the ItemClass assignment operator once for each moved item.

You might want to use a vector of pointers to ItemClass instead, or a vector of shared_ptrs, or a boost::ptr_vector.

Upvotes: 1

Related Questions