Thread: 3 unfreed D3DX allocations in this code

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    3 unfreed D3DX allocations in this code

    D3DX reports 3 unfreed allocations in the following code. I can comment out the code and exit my game and D3DX reports 0 leaks.

    I cannot for the life of me find the leaked allocations here. I'm supposing that one or more of the calls increases the ref count. The SDK is sadly lacking in this type of information and gives no indication that these will increase the ref count.

    Code:
    void X3DMesh::Create(const std::string &File)
    {
        HRESULT hr = 0;
        
        ID3DXMesh *pTempMesh = 0;
        hr = D3DXLoadMeshFromX(File.c_str(),
            D3DXMESH_MANAGED,
            SpaceXApp::GetInstance()->GetDevice(),
            &m_pAdjBuffer,
            &m_pMatBuffer,
            0,
            &m_dwNumMats,
            &pTempMesh);
    
        if (FAILED(hr))
        {
            std::ostringstream ostrm;
            ostrm << "Mesh file " << File << " failed to load";
            ::MessageBox(0,ostrm.str().c_str(),"FILE ERROR",0);
        }
    
        //Change vertex format
        D3DVERTEXELEMENT9 elems[128];
        UINT uNumElements = 0;
        VertexPNT1::pDecl->GetDeclaration(elems,&uNumElements);
        pTempMesh->CloneMesh(D3DXMESH_SYSTEMMEM,elems,SpaceXApp::GetInstance()->GetDevice(),&m_pMesh);
    
        //Done with temporary mesh
        SAFE_RELEASE(pTempMesh);
    
        //Optimize mesh
        m_pMesh->GenerateAdjacency(0.05f,(DWORD *)m_pAdjBuffer->GetBufferPointer());
        m_pMesh->Optimize(D3DXMESH_MANAGED | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
            (DWORD *)m_pAdjBuffer->GetBufferPointer(),0,0,0,&m_pMesh);
        
        //Compute bounding sphere and box
        D3DXVECTOR3 min_vec,max_vec;
        VertexPNT1 *v = 0;
    
        m_pMesh->LockVertexBuffer(0, (void **)&v);
    
        D3DXComputeBoundingBox(&v[0].vecPos,
            m_pMesh->GetNumVertices(),
            sizeof(VertexPNT1),
            &m_MeshBB.minPt,
            &m_MeshBB.maxPt);
    
        D3DXComputeBoundingSphere(&v[0].vecPos,
            m_pMesh->GetNumVertices(),
            sizeof(VertexPNT1),
            &m_BSphere.m_vecPos,
            &m_BSphere.m_Radius);
    
        m_pMesh->UnlockVertexBuffer();
    
        //Load in materials
        if (m_pMatBuffer != 0 && m_dwNumMats != 0)
        {
            m_pMaterials = new D3DXMATERIAL[m_dwNumMats];
            D3DXMATERIAL *pD3DXMats = (D3DXMATERIAL *)m_pMatBuffer->GetBufferPointer();
    
            //TODO
            //Pass texture filenames to TextureManager and save off pointers
            for (DWORD i = 0;i < m_dwNumMats;++i)
            {
                m_pMaterials[i].MatD3D.Ambient = pD3DXMats[i].MatD3D.Diffuse;
                m_pMaterials[i].MatD3D.Diffuse = pD3DXMats[i].MatD3D.Diffuse;
                m_pMaterials[i].MatD3D.Specular = pD3DXMats[i].MatD3D.Specular;
                m_pMaterials[i].MatD3D.Power = pD3DXMats[i].MatD3D.Power;
            }
        }
    }
    Any help would be appreciated.
    Last edited by VirtualAce; 02-04-2009 at 10:05 PM.

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    40
    Look at the ID of the allocation that is unfreed and in the directx control panel set break on allocID to that ID and run the code, when it breaks, look at callstack/use a couple of step outs till you get to your own code and you'll get the exact line of your own code where it's getting allocated what it's getting freed

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Already did that and still lost since the functions it points to do not increase the ref count.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you know where the refcount of a given object is stored?

    Does the D3DX tell you which objects have leaked, or does it just say "there are 3" and leaves you to guess the rest.

    If you have a memory location where the refcount is stored, then a "data access on write" breakpoint will trap whenever something is writing where it shouldn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think I can get the refcounts from COM and I think every COM object gets ref counting from IUnknown. I will look into this. The extent of my COM experience is pretty much DirectX and most of it is done for you under the hood by helper functions in newer versions of DX.

    As to whether this would lead me anywhere is another issue since the leak happens at shutdown. There are two ID3DXBuffers in that code that are class members but they are released at shutdown. Perhaps DirectX is calling them leaks or perhaps the ref count on them has somehow been incremented thereby causing them not to cleanup.

    D3DX does not tell you which objects have leaked or what type of objects have been leaked. I wish it did have more error reporting but sadly it does not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM