Sign in to follow this  
flutey

making the function virtual..crashes..why?

Recommended Posts

i am trying to make work on my engine to make it hybrid OGl and DirectX so..i am trying to abstract vertex buffer over both APIs i made a class called cMeshCompiler, which is a base class for cDXMeshCompiler and cGLMeshCompiler i made a pure virtual function called DestroyVertexBuffer which takes a void pointer, catsts it internally into LPDIRECT3DVERTEXBUFFER9 and releases it.. here is the function..i was still testing void ReleaseVBuffer(void* pBuffer) { LPDIRECT3DVERTEXBUFFER9 pVBuffer=(LPDIRECT3DVERTEXBUFFER9)pBuffer; pVBuffer->Release(); } this is very simple any..when i make this a virtual function... as i wanna do inside the engine.. it crashes, and its working normally while it isnt a virtual function any hints?

Share this post


Link to post
Share on other sites
Perhaps the pointer to the virtual function table is incorrect, which could be possible if the object hasn't been constructed properly, such as if you try to allocate one with malloc. Other than that, it could be memory corruption in another part of the program. Do any other virtual functions fail? Are you able to reproduce the behaviour in a smaller program?

Share this post


Link to post
Share on other sites
Hey flutey,

What crashes exactly in your program ? The call to the function, or the execution of the function itself ?
What I can see right now, is that if your pBuffer parameter is NULL, or corrupted, or of another type than LPDIRECT3DVERTEXBUFFER9, it has a good chance to crash during the execution of the program.
Regarding the virtual part of it, well, I would say that when you change the declaration of DestroyVertexBuffer or ReleaseVBuffer (or whatever name you gave it), maybe the (bad_pointer)->Release() does not crash --even though it is an error.
So your first question should be : What makes it crash ? The call or the execution ? Once you start narrowing down the problem, it becomes easier to tackle it.
Good luck.
StratBoy61

Share this post


Link to post
Share on other sites
Assuming the problem is not related to uninitialized vars,
My guess, seeing your class structure:

If i remember correctly, calling virtual functions from the destructor of a base class is bad...

Just put the destroy functions in the derived class anc call them from their respective classes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kitt3n
If i remember correctly, calling virtual functions from the destructor of a base class is bad...

Just put the destroy functions in the derived class anc call them from their respective classes.

The same thing goes for calling virtual functions from the constructor of a base class, I believe. Like you said, the easiest way to do it is to just call it from the inherited class.

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