My app keeps a pointer (locked chunk) to a class around for the lifetime of
the app.
This class is getting rather large, and I fear it's causing fragmentation
and in turn thrashing and crashing when the memory gets tight.
So I decided to see if I could make it movable! This is kind of weird.
What do you think?
// Allocate a movable chunk that's the same size as the class:
static VoidHand ghMyClass = MemHandleNew( sizeof(MyClass));
// "Construct" the class:
MyClass* pMC = (MyClass*) MemHandleLock( ghMyClass );
pMC->Initialize(/* construction arguments */ );
MemHandleUnlock( ghMyClass ) ; pMC = NULL;
// -- in another function, far, far away...
MyClass* pMC = (MyClass*) MemHandleLock( ghMyClass );
pMC->DoStuff();
// Time to delete the class:
pMC->~MyClass();
MemHandleUnlock( ghMyClass ); pMC = NULL;
MemHandleFree( ghMyClass );
At first glance, I thought to myself "gee, this will crash as soon as I try
to call a method of my locked handle, because pMC is just pointing to the
uninitialized memory of MemHandleNew." My jaw dropped when some simple test
functions worked fine. These were really simple functions that just
assigned arguments to member variables, maybe I just got lucky as a fluke??
If I got lucky and it was a fluke, I don't see why I can't just instantiate
a temporary "real" object, then MemMove the bytes of that real object on top
of my locked handle, as part of its creation. This will clone the guts of
the class into my locked, movable chunk so any subsequent function calls
should execute valid code.
If it _wasn't_ a fluke, does anyone see any big problems with this
admittedly half-baked idea?
* Only able to do lazy construction (� la MyClass:Initialize())
* Must perform explicit destruction
Food for thought!
-Jeff Ishaq
Vanteon (formerly The Windward Group)
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palm.com/devzone/mailinglists.html