This project has moved and is read-only. For the latest updates, please go here.

Some memory problem on windows phone 8

Mar 26, 2013 at 3:55 AM
Edited Mar 26, 2013 at 3:58 AM
I recently migrated a program of xna to WindowsPhone8. I wrap the dtk classes in a winrt class for c# code to call. For example:

public ref class Drawer
public :
void Draw()
PrimitiveBatch.Begin();// dtk class
PrimitiveBatch.DrawIndex();// dtk class

In dtk source code I found many ComPtr<> object(like WRL::ComPtr<ID3D11Buffer> buffer;) was not set nullptr or call it's Release().I think both Comptr object and SomeWinrtType^ object will release their resources automatically when Garbage collection called.

But after I wrap these classes in winrt classes and called in c# code , there are some memory problems.
For example , I press the Home button and then I Press back button , the memory use of my program is larger.Seems some resources were not released.

I really do not have to set nullptr or call it's release() for both Comptr<> object and WinrtType^ object? Are there some more action I should do for resource release?
Winrt is new to me.
Mar 26, 2013 at 6:45 AM
Your example doesn't explain how you create an instance of PrimitiveBatch. Note you may want to check out SharpDX which already has some WinRT wrappers for DirectXTK functionality.
Mar 26, 2013 at 10:17 AM
Edited Mar 26, 2013 at 10:28 AM
  1. my code is such like this:
    unique_ptr<PrimitiveBatch<XRTVertexPosition2D>> m_p2DPrimitiveBatch;
    void initialize()
    auto vMaxCount=102410;
    m_p2DPrimitiveBatch.reset(new PrimitiveBatch<XRTVertexPosition2D>(context,(vMaxCount-1)
2.There are some Comptr objects in my class like these:
ComPtr<ID3D11InputLayout> m_p2DInputLayout;
ComPtr<ID3D11InputLayout> m_p2DTInputLayout;
ComPtr<ID3D11RenderTargetView> m_renderTargetView;
ComPtr<ID3D11Device1> m_d3dDevice;
ComPtr<ID3D11ShaderResourceView> m_resourceView;
and I never set them nullptr in destructor.

Is that means we do not have to set nullptr or call it's release() for both Comptr<> object and WinrtType^ object ,and they will automatically release there resource when they are not referenced?
  1. Thanks for the link of sharpDX which provide a new way to wrap dx functions!But I don't know why there is not any source code to be downloaded for sharpdx, only Bin and Samples ^_^.
Mar 26, 2013 at 9:29 PM
Edited Mar 26, 2013 at 9:30 PM
unique_ptr and ComPtr will indeed clean themselves up if used properly. The main thing of note is that the final device needs to be released last (all child objects of a Direct3D device are immediately invalid once the device itself is freed).

There are a few shared resources used by DirectXTK that can't be easily freed through the API. Have you looked to see what objects the Direct3D debug layer is reporting as 'leaking'?
Mar 28, 2013 at 6:17 AM
" The main thing of note is that the final device needs to be released last" this really helps me.
My program is a "Direct3D with XAML app", the Device sometimes renew which is different with a "XAML and Direct3D apps ".This may be the reason I think .

Now I will learn to use "Direct3D debug layer " which I have never used in XNA4.0.
Thanks for your generous advice!