Dangling Pointer: How to effectively remove memory leaks?

Started by
4 comments, last by tom_mai78101 13 years, 1 month ago
EDIT: The One-Line Problem occurred. I'm trying to get feedback to the admins. :(

Here are 2 codes:

WinMain():
<BR>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR CommandLine, int ShowCommand)<BR>{<BR>//================================================================================<BR>// DirectX - Something I can't make it out of...<BR>//================================================================================<BR>ID3D10Device* pD3DDevice = NULL;<BR>IDXGISwapChain* pSwapChain = NULL;<BR>ID3D10RenderTargetView* pRenderTargetView = NULL;<BR><BR> //...<BR><BR>Shutdown(&pD3DDevice, &pSwapChain, &pRenderTargetView);<BR>return (int) msg.wParam;<BR>}<BR>

Shutdown():
void Shutdown(ID3D10Device** pDevice, IDXGISwapChain** pSwapChain, ID3D10RenderTargetView** pTarget)<BR>{<BR>//TODO: Fix this problem. No return data of NULL is returned to its owner, so...<BR>if (pTarget != NULL)<BR> (*pTarget)->Release();<BR>if (pSwapChain != NULL)<BR> (*pSwapChain)->Release();<BR>if (pDevice != NULL)<BR> (*pDevice)->Release();<BR>}

In Shutdown(), the TODO marks where the memory leaks occur. I think I have done it correctly, but whenever I stop my application process, I can see clearly that in the Task Manager, there are 2 (MSBuild.exe *32) in it.

I suspected it has to do with my code. And I suspected it's from here. So, can anyone help finding it for me? If you wanted all of the codes please let me know. :)
Advertisement
Don't have an answer to your question, but:


EDIT: The One-Line Problem occurred. I'm trying to get feedback to the admins.

I've been meaning to report this too, but I was afraid it was just me :| For me at least, when looking through posts in the archives, all newlines are removed from code samples, leaving them as single-line excerpts of dubious readability. (Maybe I should go ahead and submit this...)
The key to fixing pretty much any resource leak (memory or otherwise) is to make sure that you're not managing that resource manually. Any time you do this, there's ample opportunity for various kinds of mismanagement.

For COM objects, why not use CComPtr or something similar? boost's intrusive_ptr could also be made to work in this case, which would probably be my preference as it avoids ambiguities w.r.t operator=.
Hm? I'm not using any COMs or the boost. Again, I apologize for the buggy trouble, as the code I posted contains the messages that let others know where the problem is really at.

I was just asking how to solve the dangling pointer from functions within functions. Without relying on the currently-buggy CODE tags:

[color="#ff0000"][font="Courier New"]void function(ID3DDevice** A, IDXGISwapChain** B, ID3D10RenderTargetView** C)
{
[color="#00ff00"]//...
return;
}
[/font]
How do I pass the pointer (Pointer A already pointing to a device.) and use a pointer X pointing to pointing A and deallocating it (In DirectX term, Release().)? It's not about DirectX, it's just about how to implement a lot of indirections from a variable, to remove the memory leak I'm having in my program.

Hm? I'm not using any COMs or the boost.

DirectX uses COM quite heavily. ID3D10Device, IDXGISwapChain, ID3D10RenderTargetView, ... they're all COM interfaces.

I'm suggesting you use a smart pointer such as CComPtr or boost::intrusive_ptr to manage their lifetimes more easily.


Again, I apologize for the buggy trouble, as the code I posted contains the messages that let others know where the problem is really at.
[/quote]
Sure, I understand it's not something you can help.


I was just asking how to solve the dangling pointer from functions within functions.

[...]

How do I pass the pointer (Pointer A already pointing to a device.) and use a pointer X pointing to pointing A and deallocating it (In DirectX term, Release().)? It's not about DirectX, it's just about how to implement a lot of indirections from a variable, to remove the memory leak I'm having in my program.
[/quote]

For what it's worth, I'm sorry for not giving you the direct answer to your question, but this is one of those give-a-man-a-fish vs give-him-a-fishing-rod situations.

So again, I'd suggest reworking your code so that you don't have to release your resource manually. In fact I'd advise trying not to release anything manually. Let the appropriate language constructs manage that for you i.e. use RAII in the form of CComPtr, boost::instrusive_ptr or an equivalent.

However, if you're having trouble with pointers-to-pointers and more complicated indirection, that's a somewhat separate issue to resource management.

Taking the address of a pointer yields a pointer to a pointer. Dereferencing a pointer to a pointer yields a pointer.


// Example
A a;
A* ap = &a;
A** app = &ap;

A* ap2 = *app;
A a2 = *ap2;
Thank you. I should bookmark this for future reference.

This topic is closed to new replies.

Advertisement