Sign in to follow this  
break_r

[.net] AccessViolationException Mind-twister

Recommended Posts

Hi, Using Beta2 and C++/CLI, I've got a managed class that looks something like this: public ref class GammaRamp { private: D3DGAMMARAMP* m_pGammaRamp; public: GammaRamp() { m_pGammaRamp = new D3DGAMMARAMP; } public: ~GammaRamp() { delete m_pGammaRamp; } ... }; That works fine. Now, when I add another pointer... public ref class GammaRamp { private: D3DGAMMARAMP* m_pGammaRamp; private: D3DGAMMARAMP* pAnotherGammaRamp; public: GammaRamp() { m_pGammaRamp = new D3DGAMMARAMP; pAnotherGammaRamp = new D3DGAMMARAMP; } public: ~GammaRamp() { delete pAnotherGammaRamp; // ERROR! delete m_pGammaRamp; } ... }; It throws an AccessViolationException when trying to delete the new gamma ramp pointer. If I try flipping the deletion order, I get the same AccessViolationException: public: ~GammaRamp() { delete m_pGammaRamp; // ERROR! delete pAnotherGammaRamp; } So what happens when I add yet another pointer (any type of pointer)? public ref class GammaRamp { private: D3DGAMMARAMP* m_pGammaRamp; private: D3DGAMMARAMP* pAnotherGammaRamp; private: D3DLIGHT9* pSomethingUnrelated; public: GammaRamp() { m_pGammaRamp = new D3DGAMMARAMP; pAnotherGammaRamp = new D3DGAMMARAMP; pSomethingUnrelated = new D3DLIGHT9; } public: ~GammaRamp() { delete pSomethingUnrelated; delete pAnotherGammaRamp; delete m_pGammaRamp; } ... }; It no longer throws an AccessViolationException! =) Any ideas as to what is going on here? Regards.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What if you put a breakpoint in your destructor? Could it be that you're deleting the same instance twice?

Share this post


Link to post
Share on other sites
And now it suddenly works...like magic. I didn't change anything. Mysterious behavior indeed. I'm hoping it's nothing more than the beta2 IDE and not my code =)

However, any insights into AccessViolationException would be much appreciated.

cya.

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