"Timothy Dean" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > What is the prefered method for allocating memory on the heap dynamically > for objects created with C++? Right now I use the keyword new. Is it > better to use MemHandleNew? If so, how would you do this? Thanks.
I use "new" to allocate objects on the heap. The problem with using MemHandles is that you have to lock the handle in order to obtain a pointer. IOW, the chunk will be nonmovable anyway (because it's locked), so there's not much point in using a handle in the first place. I guess you could do it this way: --------------------------------- // NOTE: THROWAWAY EXAMPLE, MAY NOT WORK // Allocate object on the stack. // This way the constructor gets called and you don't have to delete the object explicitly. // You could also use new/delete. MyClass myClass; // Allocate movable chunk MemHandle h = MemHandeNew(sizeof(MyClass)); // Lock handle MemPtr p = MemHandleLock(h); // Copy class data to movable chunk MemMove(p, &myClass, sizeof(MyClass)); // Unlock handle MemHandleUnlock(h); // Lock handle to obtain pointer MyClass* myClass = (MyClass*) MemHandleLock(h); myClass->doSomething(); // Call delete to invoke destructor delete myClass; // Unlock pointer MemHandleUnlock(h); MemHandleFree(h); --------------------------------- Anyway, that's my take on it. As far as I can see, using MemHandles for classes entails a lot of overhead for no obvious gain. Maybe you could override the new and delete operators to encapsulate allocation and deallocation, but then you'd still have to keep track of the MemHandles. Regards -Laurens -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
