Hullo hullo :-)

Just to let you know, after finishing typing this, I realized it's pretty long. :-)

I've recently moved to bigger and badder things, but not wanting to have to rewrite my entire D3D engine, I just made copies of my .cpp files and threw a new project together with the intent of editing the old ones to meet my new needs.

Unlike I expected, this actually went pretty smoothly.

But an error came up with my new code that wasn't coming up before, even though all I did was delete code (no additions yet).

Here's some really simplified code of my scenario (Just assume all links/headers I don't write in are included as well since that's not what the problem comes from):
Note: The key fact is that m_pD3DDevice in both classes point to the same thing
Code:
Main.cpp
int main(void)
{
	Class1 *MyClass1 = NULL;
	MyClass1 = new Class1();

	if(MyClass1 != NULL)
	{
		delete MyClass1;
		MyClass1 = NULL;
	}

	return 0;
}
The Two Classes
class Class1
{
public:
	Class1(void)
	{
		m_pD3DDevice = NULL;
		MyClass2 = NULL;

		MyClass2 = new Class2(m_pD3DDevice);
	}
	virtual ~Class1(void)
	{
		if(MyClass2 != NULL)
		{
			// This line, fired before the next if,
			// will cause MyClass1's destructor to
			// fire. I'm pretty sure this is what's
			// causing the error (more description soon)
			delete MyClass2;
			MyClass2 = NULL;
		}
		
		if(m_pD3DDevice != NULL)
		{
			m_pD3DDevice->Release();
			m_pD3DDevice = NULL;
		}
	}
private:
	IDirect3DDevice8 *m_pD3DDevice;
	Class2 *MyClass2;
}

class Class2
{
public:
	Class2(IDirect3DDevice8 *pD3DDevice)
	{
		m_pD3DDevice = pD3DDevice;
	}
	virtual ~Class2(void)
	{
		// This is the central problem I'm focussing on.
		// When these 5 lines are here (below) I get
		// the windows error when I'm trying to exit
		// (The "Send Report to Microsoft" thing).
		// If I take them out, it all runs smoothly...
		if(m_pD3DDevice != NULL)
		{
			m_pD3DDevice->Release();
			m_pD3DDevice = NULL;
		}
	}
private:
	IDirect3DDevice8 *m_pD3DDevice;
}
Okay, I think that should all be rather fine. It might look a tad confusing at first, but all it really is, is two classes pointing to the same address.

So, the problem, I delete and NULL m_pD3DDevice in the destructor of Class2. Then, the destructor of Class1 tries to finish what it's doing, and it hits the If statement about it's own m_pD3DDevice.

Now that should technically already be NULL shouldn't it? Because I think the problem that's arising is from this line, and for some reason the program is still trying to do something with that variable, which causes the "Send Error Report" from windows.

What I'm wondering...
1) Is it okay to just remove the whole If statement from Class2 since Class1 will eventually delete and NULL it anyways? Will there be any reprecussions to Class2's member variable?
2) Is there a way to keep them both in? Because in my last project (the one I'm taking this code from), it's always been like that, and I've never encountered this problem.
3) How come the If statement in Class1's destructor isn't catching that it's already NULL?


Thanks for reading this all, it seems like a minor problem not worth such a long question being asked.