use btAlignedObjectArray<myObj *> to create new object ?

Post Reply
papaonn
Posts: 41
Joined: Wed Nov 20, 2013 4:14 pm

use btAlignedObjectArray<myObj *> to create new object ?

Post by papaonn »

Hi guys,

I am using btAlignedObjectArray<myObj *> to create new object, but when i try to remove it, i have problem.

My code is below :

Code: Select all


class myObj {
    int index;

    myObj(int index) ...
    ~myObj() ...
};

btAlignedObjectArray<myObj *> myArray;
myArray.push_back( new myObj(1) );
myArray.push_back( new myObj(2) );

// then i try to delete it, but the object is still there
myObj * ptrObj1 = myArray[0];

myArray.remove( ptrObj1 );

display << "obj still exists? = " << ptrObj1->index << endl;
my result shows that the object is removed from btAlignedObjectArray, but the memory created on the heap is still there,
and so later i checked into btAlignedObjectArray::remove() and found that it is just doing a destructor() call to the newly created object.

Does that imply that btAlignedObjectArray should not be used to call memory heap allocated objects?

If not, please let me know a better choice to remove the object.


Big Big Thanks!
gdlk
Posts: 62
Joined: Fri Oct 24, 2014 7:01 pm

Re: use btAlignedObjectArray<myObj *> to create new object

Post by gdlk »

Hi!

I don't use the btAlignedObjectArray, but I think it works similar to std::vector. If so, "remove" will remove the element from the array, but will not release the memory

a fast solution would be:

Code: Select all

myObj* buf;
for( unsigned int i = myArray.size() - 1; i >= 0; i-- ) {
   buf = myArray.at(i);
   myArray.remove(buf);
   delete buf;
}
Regards!
papaonn
Posts: 41
Joined: Wed Nov 20, 2013 4:14 pm

Re: use btAlignedObjectArray<myObj *> to create new object

Post by papaonn »

Thank you bro!
I changed to other style so to avoid complicated delete heap memory process.

and thanks for your answer =)
Post Reply