[.net] AccessViolationException Mind-twister

Started by
2 comments, last by break_r 18 years, 10 months ago
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.
Advertisement
What if you put a breakpoint in your destructor? Could it be that you're deleting the same instance twice?
Yes, i've tried breakpoints, but no hint as to what's going on yet. Deleting a null pointer is a safe operation in C++.
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.

This topic is closed to new replies.

Advertisement