Hi everyone.

I've memory corruptions that happens once in a while in random places of my code. One of those places looks like the one below...

Code:
Viewport *pViewport; // initialize pViewport IS ALWAYS a valid viewport
pViewport->getTransformation( TRUE )->unMap(
	&m_origin,
	pViewport->getBounds()
);
The program crash in pViewport->getTransformation since pViewport is unresolved.
The problem is I've stepped through the debugger a lot of times and make sure that the program execute in exactly the following order
Code:
1. // initialize pViewport
2. pViewport->getBounds(){return this->m_bound; /* a valid rect structure*/ }
3. pViewport->getTransformation(); // missing pViewport here... pViewport suddenly points to 0x000000c once in a while
4. pTransformation->unMap();
BUT when executing getBounds, pViewport is STILL valid, but when executing getTransformation(), pViewport disappeared causing access violation ending in crash.

Is there anyway around this problem? Could it be caused by my bad coding habit rather than "memory corruption" which I presumed? The access violation happens once in a while (once after millions of loops maybe) and it always occur in different places (rarely hit the same place). However, it always happen in situation resembling the one above ( missing reference suddenly)

Thanks a lot for the help!