[.net] AccessViolationException Mind-twister
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.
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++.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement