Trending
Opinion: How will Project 2025 impact game developers?
The Heritage Foundation's manifesto for the possible next administration could do great harm to many, including large portions of the game development community.
Common pitfalls when using vectors -- releasing references.
Often times it's tempting to simply turn a member variable to NULL when you're ready to use it or add a reference.
However, doing so can result in memory leaks.
Here's a quick suggestion:
GOOD:
VOID CMyClass::SetVertexBuffer (PVertexBufferWrapper pVBuffer )
{
//Release existing buffer
SAFE_RELEASE( m_pVBuffer );
// setthe member pointer to new pointer
m_pVBuffer = pVBuffer;
// adda ref
if (pVBuffer )
pVBuffer->AddRef ();
}
The above will release anyexisting buffer ptr first. This requires the member variable to be initializedto NULL. You should get in the habit of initializing values anyways, asdoing so helps eliminate many “release-only” bugs.
BAD:
VOID CMyClass::SetVertexBuffer (PVertexBufferWrapper pVBuffer )
{
// setthe member pointer to new pointer
m_pVBuffer = pVBuffer;
// adda ref
if (pVBuffer )
pVBuffer->AddRef ();
}
The above code will not release areference if m_pVBuffer already had a pointer to a different vertex buffer whenthis function is called. This causes memory leaks.
You May Also Like