Sign in to follow this  
Shock

Virtual Functions and Memory Allocation

Recommended Posts

Hello All! Im using virtual functions for methods in my DLL classes. This works fine and all since most of my classes are singletons. The problem is some of these classes need to be allocated at runtime using my simple memory manager. My Memory Manager uses HeapAlloc() and HeapFree() to allocate and free memory. Now whenever I allocate memory for these classes it goes through but when I call any virtual method I get an exception. I Allocate like so (example): // TextureManager is a class, Allocate returns void pointer Allocate( sizeof(TextureManager)); Ive tried allocating the same thing with new/delete and there arnt any problems. I know new invokes the constructor but by the looks of what I see in the debug info stuff so does my Memory Manager. Its just that the __vfptr ( I think its called this ) is null with the Memory Manager from what I see in the debug info. Please help me, I would really like to allocate all my stuff using just my memory manager. Thanks !

Share this post


Link to post
Share on other sites
i believe that malloc & heapAlloc or whatever just give you the memory. you _need_ to use new/delete to actually set up the v-table (the thing that you need for virtual functions) and run the constructors of a class. so, you'll need to either overload the new/delete operators to use your method or change your memory manager to use new/delete.

-me

Share this post


Link to post
Share on other sites
Yes, HeapAlloc just gives you raw memory, you still need to call the constructor on the objects. You can use placement new with your predefined buffer to do this, or you can just use the new operator as already suggested.

Share this post


Link to post
Share on other sites
Hey, Thanks for the replies.

Well I want to go for overloading new and delete. But it is giving me problems!

C++
 
static void* operator new( size_t &Size )
{
return( Allocate( Size ));
}

static void operator delete( void *pMemory )
{
Delete( pMemory );
}





Ok no matter if I declare the above globally or in a class and use it, it still uses the normal new and delete operators. Please help me with this!

Thanks

BTW : My Compiler is VC7 .net if that makes a difference.

[Edited by - Shock on August 5, 2004 5:51:31 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this