In my experiments/research to answer this question, I discovered a big problem with what you're doing...
It's like this... there are 3 forms of new[], each have their own coresponding "clean up":
1) new[] (allocate memory + call ctors)
2) operator new (allocate memory only)
3) placement new (call ctors only)
Typically you either just do 1, or you do 2+3. You're doing 1 + a weird version of 3
1) new[]
1 2 3 4
|
T* foo = new T[10]; // allocate 10 objects
// appropriate clean up:
delete[] foo;
|
2) operator new
1 2 3 4 5
|
// allocate space for 10 objects, but do not construct them
T* foo = static_cast<T*>(operator new(sizeof(T) * 10));
// appropriate clean up:
operator delete(foo);
|
3) placement new
1 2 3 4 5 6 7 8
|
T* foo = some_memory_that_has_been_allocated_but_not_constructed;
for(int i = 0; i < 10; ++i)
new (foo + i) T(); // call ctors for all 10 objects
// appropriate clean up:
for(int i = 0; i < 10; ++i)
foo[i].~T(); // explicitly call the dtors
|
Note here that I'm using new for placement and not new[]. I tried using new[] on my machine and it got ALL screwed up. It looks like using new[] uses an extra 4 bytes (presumably to record how many objects were constructed) but I'm figuring that's platform dependent, so I wouldn't advise relying on that.
So it's very possible that you are corrupting the heap if you do NewHandle(FreeStore, 10), or even NewHandle(FreeStore, 7)!