• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lucky6969b

D3DX Memory leaks

15 posts in this topic

3 leaks to get rid of. But I can't find them.

Can anyone who is sensitive enough to find them?

There is one thing remarkable. It should happen only when it is loading skinned meshes.

Thanks

Jack

 

D3DX: MEMORY LEAKS DETECTED: 3 allocations unfreed (28024 bytes)
D3DX: Set HKLM\Software\Microsoft\Direct3D\D3DXBreakOnAllocId=0x5f5c to debug
The program '[5416] PerfectSim.exe: Native' has exited with code 0 (0x0).
 

//////////////////////////////////////////////////////////////////////////

//                    Character Animation with Direct3D                    //

//                           Author: C. Granberg                            //

//                               2008 - 2009                                //

//////////////////////////////////////////////////////////////////////////



#ifndef SKINNED_MESH

#define SKINNED_MESH



#include <windows.h>

#include <d3dx9.h>

#include <string>

#include <vector>

 



//using namespace std;

struct D3DXFRAME_DERIVED : public  D3DXFRAME

{

    D3DXMATRIX CombinedTransformationMatrix;



    D3DXFRAME_DERIVED()  

    {

        Name = 0;    

        pFrameSibling = NULL;

        pFrameFirstChild = NULL;

        pMeshContainer = NULL;

    }



    ~D3DXFRAME_DERIVED()

    {



    }

        

};



struct D3DXMESHCONTAINER_DERIVED : public D3DXMESHCONTAINER

{

    LPD3DXMESH OriginalMesh;

    std::vector<D3DMATERIAL9> materials;

    std::vector<IDirect3DTexture9*> textures;



    D3DXMATRIX** boneMatrixPtrs;

    D3DXMATRIX* boneOffsetMatrices;

    D3DXMATRIX* currentBoneMatrices;



    DWORD NumAttributeGroups;

    D3DXATTRIBUTERANGE* attributeTable;





    D3DXMESHCONTAINER_DERIVED()  

    {

        Name = 0;

        pSkinInfo = 0;

        boneMatrixPtrs = 0;

        boneOffsetMatrices = 0;

        currentBoneMatrices = 0;

        attributeTable = 0;

        OriginalMesh = 0;

        pEffects = 0;

        pMaterials = 0;

        pAdjacency = 0;

        NumMaterials = 0;

        NumAttributeGroups = 0;

        

    }



    ~D3DXMESHCONTAINER_DERIVED()

    {

        int numTextures = (int)textures.size();



        for(int i=0;i < numTextures;i++)

        {

            if( textures[i] != NULL)

            {

                 textures[i]->Release();

                 textures[i] = NULL;

            }

        }

    



        if (Name != NULL)

        {

            delete [] Name;

            Name = NULL;

        }

    

        if ( MeshData.pMesh != NULL)  

        {

             MeshData.pMesh->Release();

             MeshData.pMesh = NULL;

        

        }



        if (MeshData.pPatchMesh != NULL)

    {

        MeshData.pPatchMesh->Release();

        MeshData.pPatchMesh = NULL;

    }



    if (MeshData.pPMesh != NULL)

    {

        MeshData.pPMesh->Release();

        MeshData.pPMesh = NULL;

    }

    



        if ( OriginalMesh != NULL)

        {

             OriginalMesh->Release();

             OriginalMesh = NULL;

        }

    

    

        if ( boneMatrixPtrs != NULL)

        {

            delete []  boneMatrixPtrs;

             boneMatrixPtrs = NULL;

        }



        if ( boneOffsetMatrices != NULL)

        {

            delete  boneOffsetMatrices;

             boneOffsetMatrices = NULL;

        }



        if ( currentBoneMatrices != NULL)

        {

            delete  currentBoneMatrices;

             currentBoneMatrices = NULL;

        }



        if ( attributeTable != NULL)

        {

            delete  attributeTable;

             attributeTable = NULL;

        }



        if ( pSkinInfo != NULL)

        {

             pSkinInfo->Release();

             pSkinInfo = NULL;

        }



        if ( pAdjacency)

        {

            delete  pAdjacency;

             pAdjacency = NULL;

        }

    



    

        if ( pEffects)

        {

            delete  pEffects;

             pEffects = NULL;



        }





        if ( pMaterials)

        {

            delete  pMaterials;

             pMaterials = NULL;

        }



    

        materials.clear();

        textures.clear();

    

    }

};



class CAllocateHierarchy: public ID3DXAllocateHierarchy

{

    public:

        STDMETHOD(CreateFrame)(THIS_ LPCSTR Name, LPD3DXFRAME *ppNewFrame);

        STDMETHOD(CreateMeshContainer)(THIS_ LPCSTR Name, CONST D3DXMESHDATA * pMeshData, CONST D3DXMATERIAL * pMaterials, CONST D3DXEFFECTINSTANCE * pEffectInstances, DWORD NumMaterials, CONST DWORD * pAdjacency, LPD3DXSKININFO pSkinInfo, LPD3DXMESHCONTAINER * ppNewMeshContainer);

        STDMETHOD(DestroyFrame)(THIS_ LPD3DXFRAME pFrameToFree);

        STDMETHOD(DestroyMeshContainer)(THIS_ LPD3DXMESHCONTAINER pMeshContainerBase);

};



class SkinnedMesh

{

public:

    SkinnedMesh();

    ~SkinnedMesh();



    bool Load(const std::wstring& filename);

private:

    void UpdateMatrices(D3DXFRAME_DERIVED* f, D3DXMATRIX* m);

    void Render(D3DXFRAME_DERIVED *f);

    void SetupBoneMatrixPointers(D3DXFRAME_DERIVED *f);

public:

    void Update(const D3DXMATRIX& mat);

    void Draw();



private:

    LPD3DXFRAME m_pRoot;

    LPD3DXANIMATIONCONTROLLER m_pAnimCtrl;

    CAllocateHierarchy Alloc;

};



 #endif

 

 

#include "skinnedMesh.h"



#pragma warning(disable:4996)



extern IDirect3DDevice9 *g_pDevice;

extern ID3DXEffect *g_pEffect;

extern std::ofstream g_debug;







HRESULT CAllocateHierarchy::CreateFrame(LPCSTR Name, LPD3DXFRAME* ppNewFrame)

{

    D3DXFRAME_DERIVED* newFrame = new D3DXFRAME_DERIVED();

    //memset (newFrame, 0, sizeof(D3DXFRAME_DERIVED));

    

    

    if (Name != NULL)

    {

        newFrame->Name = new char[strlen(Name)+1];

        strcpy (newFrame->Name, Name);

    }

    else

    {

        newFrame->Name = NULL;

    }

    

    D3DXMatrixIdentity(&newFrame->TransformationMatrix);

    D3DXMatrixIdentity(&newFrame->CombinedTransformationMatrix);



    newFrame->pFrameFirstChild = NULL;

    newFrame->pFrameSibling = NULL;

    

    *ppNewFrame = newFrame;



      

    

    return S_OK;

}



HRESULT CAllocateHierarchy::CreateMeshContainer(LPCSTR Name,

                                            CONST D3DXMESHDATA *pMeshData,

                                            CONST D3DXMATERIAL *pMaterials,

                                            CONST D3DXEFFECTINSTANCE *pEffectInstances,

                                            DWORD NumMaterials,

                                            CONST DWORD *pAdjacency,

                                            LPD3DXSKININFO pSkinInfo,

                                            LPD3DXMESHCONTAINER *ppNewMeshContainer)

{



    

    D3DXMESHCONTAINER_DERIVED *pMeshContainer = new D3DXMESHCONTAINER_DERIVED;  

    

    //ZeroMemory(&pMeshContainer, sizeof(D3DXMESHCONTAINER_DERIVED)); // don't do this

    

    if (Name != NULL)

    {

        pMeshContainer->Name = new char[strlen(Name)+1];

        strcpy (pMeshContainer->Name, Name);

    }

    else

    {

        Name = NULL;

    }

    



    

    pMeshContainer->MeshData.pMesh = pMeshData->pMesh;

    pMeshContainer->MeshData.Type = pMeshData->Type;

    pMeshContainer->MeshData.pMesh->AddRef();



    pMeshContainer->OriginalMesh = pMeshData->pMesh;

    pMeshContainer->OriginalMesh->AddRef();

    



 

    IDirect3DDevice9 *pDevice = NULL;    

    pMeshData->pMesh->GetDevice(&pDevice);



    for(int i=0;i<(int)NumMaterials;i++)

    {

        D3DXMATERIAL mtrl;

        memcpy(&mtrl, &pMaterials[i], sizeof(D3DXMATERIAL));

        pMeshContainer->materials.push_back(mtrl.MatD3D);

        IDirect3DTexture9* newTexture = NULL;



        if(mtrl.pTextureFilename != NULL)

        {

            char textureFname[200];

            strcpy(textureFname, "Data/");

            strcat(textureFname, mtrl.pTextureFilename);



            //Load texture

            D3DXCreateTextureFromFileA(g_pDevice, textureFname, &newTexture);

            pMeshContainer->textures.push_back(newTexture);

        }

        else

        {



            pMeshContainer->textures.push_back(newTexture);

        }



        

    

    }



    if(pSkinInfo != NULL)

    {

        //Get Skin Info

        pMeshContainer->pSkinInfo = pSkinInfo;

        pSkinInfo->AddRef();    //Add reference so that the SkinInfo isnt deallocated



        DWORD maxVertInfluences = 0;

        DWORD numBoneComboEntries = 0;

        ID3DXBuffer* boneComboTable = 0;



        pSkinInfo->ConvertToIndexedBlendedMesh(pMeshData->pMesh,

                                                D3DXMESH_MANAGED | D3DXMESH_WRITEONLY,  

                                                30,

                                                0, // ignore adjacency in

                                                0, // ignore adjacency out

                                                0, // ignore face remap

                                                0, // ignore vertex remap

                                                &maxVertInfluences,

                                                &numBoneComboEntries,

                                                &boneComboTable,

                                                &pMeshContainer->MeshData.pMesh);



        if(boneComboTable != NULL)

            boneComboTable->Release();



        //Get Attribute Table

        pMeshContainer->MeshData.pMesh->GetAttributeTable(NULL, &pMeshContainer->NumAttributeGroups);

        pMeshContainer->attributeTable = new D3DXATTRIBUTERANGE[pMeshContainer->NumAttributeGroups];

        pMeshContainer->MeshData.pMesh->GetAttributeTable(pMeshContainer->attributeTable, NULL);



        //Create bone offset and current matrices

        int NumBones = pSkinInfo->GetNumBones();

        pMeshContainer->boneOffsetMatrices = new D3DXMATRIX[NumBones];        

        pMeshContainer->currentBoneMatrices = new D3DXMATRIX[NumBones];



        //Get bone offset matrices

        for(int i=0;i < NumBones;i++)

            pMeshContainer->boneOffsetMatrices[i] = *(pMeshContainer->pSkinInfo->GetBoneOffsetMatrix(i));

    }

    

    pMeshContainer->pNextMeshContainer = NULL;



    



    *ppNewMeshContainer = pMeshContainer;



    //delete pMeshContainer;

    pDevice->Release();



    return S_OK;

}



HRESULT CAllocateHierarchy::DestroyFrame(LPD3DXFRAME pFrameToFree)

{

    if(pFrameToFree)

    {

        //Free name

        if(pFrameToFree->Name != NULL) {

            delete [] pFrameToFree->Name;

            pFrameToFree->Name = NULL;



            

        }



        /*if (pFrameToFree->pFrameFirstChild != NULL) {

            delete pFrameToFree->pFrameFirstChild;

            pFrameToFree->pFrameFirstChild = NULL;



        }



        if (pFrameToFree->pFrameSibling != NULL) {

            delete pFrameToFree->pFrameSibling;

            pFrameToFree->pFrameSibling = NULL;

        }

        */



        /*if (pFrameToFree->pMeshContainer != NULL) {

            delete pFrameToFree->pMeshContainer;

            pFrameToFree->pMeshContainer = NULL;



        }

        */



        //Free bone

        delete pFrameToFree;

        

    }

    pFrameToFree = NULL;



    return S_OK;

}



HRESULT CAllocateHierarchy::DestroyMeshContainer(LPD3DXMESHCONTAINER pMeshContainerBase)

{

    /*

    */

    //Release textures

    

    

    

    D3DXMESHCONTAINER_DERIVED* pMeshContainer = (D3DXMESHCONTAINER_DERIVED*) pMeshContainerBase;



    int numTextures = (int)pMeshContainer->textures.size();



    for(int i=0;i < numTextures;i++)

    {

        if(pMeshContainer->textures[i] != NULL)

        {

            pMeshContainer->textures[i]->Release();

            pMeshContainer->textures[i] = NULL;

        }

    }

    

    if (pMeshContainer->Name != NULL)

    {

        delete [] pMeshContainer->Name;

        pMeshContainer->Name = NULL;

    }

    

    if (pMeshContainer->MeshData.pMesh != NULL)  

    {

        pMeshContainer->MeshData.pMesh->Release();

        pMeshContainer->MeshData.pMesh = NULL;

        

    }



    if (pMeshContainer->MeshData.pPatchMesh != NULL)

    {

        pMeshContainer->MeshData.pPatchMesh->Release();

        pMeshContainer->MeshData.pPatchMesh = NULL;

    }



    if (pMeshContainer->MeshData.pPMesh != NULL)

    {

        pMeshContainer->MeshData.pPMesh->Release();

        pMeshContainer->MeshData.pPMesh = NULL;

    }

    



    if (pMeshContainer->OriginalMesh != NULL)

    {

        pMeshContainer->OriginalMesh->Release();

        pMeshContainer->OriginalMesh = NULL;

    }

    

    

    if (pMeshContainer->boneMatrixPtrs != NULL)

    {

        delete [] pMeshContainer->boneMatrixPtrs;

        pMeshContainer->boneMatrixPtrs = NULL;

    }



    if (pMeshContainer->boneOffsetMatrices != NULL)

    {

        delete pMeshContainer->boneOffsetMatrices;

        pMeshContainer->boneOffsetMatrices = NULL;

    }



    if (pMeshContainer->currentBoneMatrices != NULL)

    {

        delete pMeshContainer->currentBoneMatrices;

        pMeshContainer->currentBoneMatrices = NULL;

    }



    if (pMeshContainer->attributeTable != NULL)

    {

        delete pMeshContainer->attributeTable;

        pMeshContainer->attributeTable = NULL;

    }



    if (pMeshContainer->pSkinInfo != NULL)

    {

        pMeshContainer->pSkinInfo->Release();

        pMeshContainer->pSkinInfo = NULL;

    }



    if (pMeshContainer->pAdjacency)

    {

        delete pMeshContainer->pAdjacency;

        pMeshContainer->pAdjacency = NULL;

    }

    



    

    if (pMeshContainer->pEffects)

    {

        delete pMeshContainer->pEffects;

        pMeshContainer->pEffects = NULL;



    }





    if (pMeshContainer->pMaterials)

    {

        delete pMeshContainer->pMaterials;

        pMeshContainer->pMaterials = NULL;

    }



    

    pMeshContainer->textures.clear();

    pMeshContainer->materials.clear();



    



    delete pMeshContainer;

    pMeshContainer = NULL;

    

    

    pMeshContainerBase = NULL;



    return S_OK;

}







SkinnedMesh::SkinnedMesh()

    : m_pRoot(0), m_pAnimCtrl(0)

{

}



SkinnedMesh::~SkinnedMesh()

{

    

    

    if (m_pRoot != NULL)

    {

        

        

        //Alloc.DestroyFrame(m_pRoot);

        D3DXFrameDestroy(m_pRoot, &Alloc);

        

        m_pRoot = NULL;

    }

    

    if (m_pAnimCtrl) { m_pAnimCtrl->Release(); m_pAnimCtrl = NULL; }

}







bool SkinnedMesh::Load(const std::wstring& filename)

{

    HRESULT hr = S_OK;

    



    

    

    hr = D3DXLoadMeshHierarchyFromXW(filename.c_str(), D3DXMESH_MANAGED,

                               g_pDevice, &Alloc ,

                               NULL, &m_pRoot, &m_pAnimCtrl);

                               



    /*SetupBoneMatrixPointers((D3DXFRAME_DERIVED*)m_pRoot);*/

    //Update all the bones

    /*

    D3DXMATRIX i;

    

    D3DXMatrixIdentity(&i);

    UpdateMatrices((D3DXFRAME_DERIVED*)m_pRoot, &i);

    */

    return true;

}



void SkinnedMesh::Update(const D3DXMATRIX& mat)

{

    D3DXMATRIX m(mat);

    this->UpdateMatrices((D3DXFRAME_DERIVED*)m_pRoot, &m);

}



void SkinnedMesh::Draw()

{

    Render(NULL);

}



void SkinnedMesh::UpdateMatrices(D3DXFRAME_DERIVED* f, D3DXMATRIX* parentMatrix)

{

    if(f == NULL)return;



    D3DXMatrixMultiply(&f->CombinedTransformationMatrix,

                       &f->TransformationMatrix,

                       parentMatrix);



    if(f->pFrameSibling != NULL)

    {

        UpdateMatrices((D3DXFRAME_DERIVED*)f->pFrameSibling, parentMatrix);

    }

    if(f->pFrameFirstChild != NULL)

    {

        UpdateMatrices((D3DXFRAME_DERIVED*)f->pFrameFirstChild, &f->CombinedTransformationMatrix);

    }

}



void SkinnedMesh::SetupBoneMatrixPointers(D3DXFRAME_DERIVED *bone)

{

    if(bone->pMeshContainer != NULL)

    {

        D3DXMESHCONTAINER_DERIVED *boneMesh = (D3DXMESHCONTAINER_DERIVED*)bone->pMeshContainer;



        if(boneMesh->pSkinInfo != NULL)

        {

            int NumBones = boneMesh->pSkinInfo->GetNumBones();

            boneMesh->boneMatrixPtrs = new D3DXMATRIX*[NumBones];



            for(int i=0;i < NumBones;i++)

            {

                D3DXFRAME_DERIVED *b = (D3DXFRAME_DERIVED*)D3DXFrameFind(m_pRoot, boneMesh->pSkinInfo->GetBoneName(i));

                if(b != NULL)boneMesh->boneMatrixPtrs[i] = &b->CombinedTransformationMatrix;

                else boneMesh->boneMatrixPtrs[i] = NULL;

            }

        }

    }



    if(bone->pFrameSibling != NULL)SetupBoneMatrixPointers((D3DXFRAME_DERIVED*)bone->pFrameSibling);

    if(bone->pFrameFirstChild != NULL)SetupBoneMatrixPointers((D3DXFRAME_DERIVED*)bone->pFrameFirstChild);

}





void SkinnedMesh::Render(D3DXFRAME_DERIVED *f)

{

    if(f == NULL)f = (D3DXFRAME_DERIVED*)m_pRoot;



    //If there is a mesh to render...

    if(f->pMeshContainer != NULL)

    {

        D3DXMESHCONTAINER_DERIVED *boneMesh = (D3DXMESHCONTAINER_DERIVED*)f->pMeshContainer;



        if (boneMesh->pSkinInfo != NULL)

        {        

            /*

            // set up bone transforms

            int numBones = boneMesh->pSkinInfo->GetNumBones();

            for(int i=0;i < numBones;i++)

            {

                D3DXMatrixMultiply(&boneMesh->currentBoneMatrices[i],

                                   &boneMesh->boneOffsetMatrices[i],

                                   boneMesh->boneMatrixPtrs[i]);

            }



            D3DXMATRIX view, proj, identity;                

            g_pEffect->SetMatrixArray("FinalTransforms", boneMesh->currentBoneMatrices, boneMesh->pSkinInfo->GetNumBones());

            



            //Render the mesh

            for(int i=0;i < (int)boneMesh->NumMaterials; i++)//>NumAttributeGroups;i++)

            {

                //int mtrlIndex = boneMesh->attributeTable[i].AttribId;

                //g_pDevice->SetMaterial(&(boneMesh->materials[mtrlIndex]));

                //g_pDevice->SetTexture(0, boneMesh->textures[mtrlIndex]);

                g_pDevice->SetMaterial(&(boneMesh->materials[i]));

                g_pDevice->SetTexture(0, boneMesh->textures[i]);

                g_pEffect->SetMatrix("matW", &identity);



                g_pEffect->SetVector( "materialColor",

                                         ( D3DXVECTOR4* )&(

                                         boneMesh->materials[i].Diffuse ) );



                g_pEffect->SetTexture("texDiffuse", boneMesh->textures[i]);

                D3DXHANDLE hTech = g_pEffect->GetTechniqueByName("Skinning");

                g_pEffect->SetTechnique(hTech);

                g_pEffect->Begin(NULL, NULL);

                g_pEffect->BeginPass(0);



                boneMesh->MeshData.pMesh->DrawSubset(i);



                g_pEffect->EndPass();

                g_pEffect->End();

            }

            */

            

            

        }

        else

        {

            try

            {

                //Normal Static Mesh

                g_pEffect->SetMatrix("matW", &f->CombinedTransformationMatrix);



                D3DXHANDLE hTech = g_pEffect->GetTechniqueByName("Lighting");

                g_pEffect->SetTechnique(hTech);



                //Render the mesh

                int numMaterials = (int)boneMesh->materials.size();



                for(int i=0;i < numMaterials;i++)

                {

                    g_pDevice->SetMaterial(&boneMesh->materials[i]);

                    g_pEffect->SetVector( "materialColor",

                                             ( D3DXVECTOR4* )&(

                                             boneMesh->materials[i].Diffuse ) );

                    g_pEffect->SetTexture("texDiffuse", boneMesh->textures[i]);



                    g_pEffect->Begin(NULL, NULL);

                    g_pEffect->BeginPass(0);



                    boneMesh->OriginalMesh->DrawSubset(i);



                    g_pEffect->EndPass();

                    g_pEffect->End();

                }

            }

            catch (std::bad_alloc e)

            {

                OutputDebugStringA(e.what());



            }

        }

    

    }

    if(f->pFrameSibling != NULL)

    {

        Render((D3DXFRAME_DERIVED*)f->pFrameSibling);

    }

    if(f->pFrameFirstChild != NULL)

    {

        Render((D3DXFRAME_DERIVED*)f->pFrameFirstChild);

    }

    

    

}
0

Share this post


Link to post
Share on other sites

Just open the memory output pane, look at said location, then look what variable is allocated at this location. Might take some time but it work. Also, using those macros would really decrease your code size:

 

#define SAFE_RELEASE(x)   if((x) != NULL){(x)->Release();(x) = NULL;} #define SAFE_DELETE_ARRAY(x) if((x) != NULL){delete[] (x);(x) = NULL;} #define SAFE_DELETE_OBJECT(x)  if((x) != NULL){delete (x);(x) = NULL;}  

I don't understand why anybody use them anymore, they used to come with the directx sdk and are really awsome at saving space (deleting an object, array or whatever take only 1 line of code), Most macros are bad, but those are really good.

Edited by jbadams
Restored post contents from history.
-3

Share this post


Link to post
Share on other sites

For once, it might be even easier if you used PIX (shipped with DirectX sdk) to debug your application. Create a new experiment, set your app as target. Pick second option (something with  "F12"), then press Start Experiment. Now press F12 any time, wait for the "dump" to being made, the app will freeze so long. Exit the app, then you should have a screen with one resource window in the middle. Look at which objects have their attribute "Destruction time" set to "never". Those will be your memory leaks. There will be a lot more of 3 since you most likely are not running DX in debug mode, and the memleaks-output of visual studio is imprecise in DX release mode.

 

As for the objects shown as never been released in PIX: Of course this will be the device, the backbuffer etc.. but you should also see which resources are never being actually freed.

 

 

Also, using those macros would really decrease your code size:



#define SAFE_RELEASE(x) if((x) != NULL){(x)->Release();(x) = NULL;}
#define SAFE_DELETE_ARRAY(x) if((x) != NULL){delete[] (x);(x) = NULL;}
#define SAFE_DELETE_OBJECT(x) if((x) != NULL){delete (x);(x) = NULL;}

 

Some comments on these too: for the SAFE_DELETE_ - macro, the check for x not being null is unnecessary. delete NULL is a valid operation, so if anything this adds more code size (than needed that is, as one can simply write delete/delete[] - but most people don't seem to know that). Plus, setting x to NULL afterwards is unnecessary for most circumstances, seing how in this example everything gets disposed in the destructor anyway, so there is no chance of them being used outside again afterwards. If anything, like in the case that one double-releases/deletes something in the destructor by accident, he might even rather be notified one time to fix the unecessary line. But well, if you REALLY want to be safe and spare that one additional line of typed code, I'd suggest to change those macros to:

 

//those macros are wide spread but not necessary. use them if you want to save yourself one line of code every now and then, //at the price of a lot of unecessary NULL sets
#define SAFE_RELEASE(x) if((x) != NULL){(x)->Release();(x) = NULL;} //makes the most sense
#define SAFE_DELETE_ARRAY(x) {delete[] (x);(x) = NULL;} //unnecessary, just call delete[]
#define SAFE_DELETE_OBJECT(x) if((x) != NULL){delete (x);(x) = NULL;} //unnecessary, just call delete

Those comments should be put there, too, really. Ok, its not that bad, but doesn't it always say we shouldn't be over-exessively using compiler macros anyway? That would make this a perfect case to not use these here, too. But of course you probably didn't know better, I used to use them until I found out some weeks ago that most of the null-checks weren't necessary, too.

Edited by The King2
0

Share this post


Link to post
Share on other sites

Why did i got downvoted for this?? The reason you give are a bit far fetch imo. First, for the code size, yea, you might lose 1-2 bytes each time you use it in you executable, big deal... calling delete [] might do the trick, but this alone won't protect you from trying to delete a null pointer, that's the point of the macros  , not the (x) = null (and save some source code lines too). Also, i don't see how setting a variable to null would make any difference in 99% of normal code usage... useless you spend you're entire time creating and deleting stuff, and even then...

Edited by jbadams
Restored post contents from history.
-2

Share this post


Link to post
Share on other sites

Why did i got downvoted for this?? The reason you give are a bit far fetch imo. First, for the code size, yea, you might lose 1-2 bytes each time you use it in you executable, big deal... calling delete [] might do the trick, but this alone won't protect you from trying to delete a null pointer, that's the point of the macros, not the (x) = null. Also, i don't see how setting a variable to null would make any difference in 99% of normal code usage... useless you spend you're entire time creating and deleting stuff, and even then...

 

I didn't downvote you, but I quess someone simply downvoted you because obviously these macros are, like I described, unnecessary, and you directly told someone to use it, so the -1 is an indication that your information is eigther not correct or not good practice. Talking about those macros again, its not like they are screwing up your code or anything, but they are, as I stated, unnecessary. I might as well write a "define safeplus(a, b, c) c = a + b; assert(c != a + b);" to make 1000% sure I correctly add two variables, but what for, if I can simply say c = a + b, and the code addition will be correct anyway. Same as those macros. If you code correctly, and thats what you ultimately should aim for, they do not give you any advantage (one line less "X = NULL;" where it is actually needed every 20 classes or so doesn't count) at the cost of some slight disadvantages. Take a look at the second anwser from above from here if you want to have a more elaborate answer.

 

Again, I don't blame you for using those macros, I think nearly everybode has come over them at some point. But for all the given reasons it should not be tought as good practice in any for or shape.

 

 

When did everyone stop using smart pointers?

 

Isn't the IResource* interface from DirectX already a smart pointer of a sort, given its features of reference counting etc...?

Edited by The King2
2

Share this post


Link to post
Share on other sites

Ok im done helping ppl here, if you can't stand a #define or goto, then switch to c# morons

 

I guess microsoft who wrote those macros are also noob and stupid programmers...

Edited by jbadams
Restored post contents from history.
-5

Share this post


Link to post
Share on other sites

If you can't stand getting downvoted for being wrong and told when you are wrong, you shouldn't be offering advice.

 

If you think goto and #define are perfect practice, you might considere, in your own logic, switching back to C.

 

If you don't want to improve your style of coding and accept that sometimes your way is not optimal, you should not code at all.

 

If this is your reaction to people who try to help you on correcting mistakes, I have no comment to that at all.

 

EDIT: And last but not least, if you don't have hard evidence for that what you do is the way to go, and have tons of articles/opinions/facts against you, you must never act like you are right.

 

Sorry for off-topic, that was necessary :/

Edited by The King2
1

Share this post


Link to post
Share on other sites


 

>When did everyone stop using smart pointers?

 

Isn't the IResource* interface from DirectX already a smart pointer of a sort, given its features of reference counting etc...?

 

The COM stuff has reference counting but it's not automatic. You can use a custom deleter with a smart pointer to manage DX objects automatically.

Edited by Khatharr
0

Share this post


Link to post
Share on other sites

#define SAFE_RELEASE(x)           if((x) != NULL){(x)->Release();(x) = NULL;}
#define SAFE_DELETE_ARRAY(x)   if((x) != NULL){delete[] (x);(x) = NULL;}
#define SAFE_DELETE_OBJECT(x)  if((x) != NULL){delete (x);(x) = NULL;}
 
I don't understand why anybody use them anymore, /.../

Because it is a horrible practice. Just try to forget MS put that crap in its SDK - never recommend it to anyone!

Using macros is fine, provided they are sane - thous are not because:
1. macros should be avoided when there is not a darn good reason to use them - this just pointlessly obscures what it does.
2. all macros should properly handle parameters with side effects.
3. old VC ignored the standard and needed a check for null in case of delete - that cruft is now useless.

Don't use it. And sure as hell do not recommend anyone else to use it.
2

Share this post


Link to post
Share on other sites

The COM stuff has reference counting but it's not automatic. You can use a custom deleter with a smart pointer to manage DX objects automatically.

Or use a smart pointer built for COM such as CComPtr.
0

Share this post


Link to post
Share on other sites

I was only sugesting those macros because that's exactly what's the op doing in is code exept without macro, and would have saved him about a hundred lines of code, but i don't see him gettings downrated for no good reason. I didn't put a gun on his head and said you MUST use them, i only sugessted them to reduce is code size.

 

But go ahead, continue to downrate me for no reason, and for trying to help, because it was the last time i spend time here helping someone, im getting tired of those "chest beating" game for stupidity like that, and had a very bad day on top of that.

 

Thank you.

Edited by jbadams
Restored post contents from history.
-2

Share this post


Link to post
Share on other sites

Hello,

I guess about 99% of the leaks are gone now. But when I turned on the DirectX debug runtime, the number of leaks did explode again. Do I have to bother with those leaks because they don't happen when I use the retail runtime?

Thanks

Jack

0

Share this post


Link to post
Share on other sites

Do I have to bother with those leaks because they don't happen when I use the retail runtime?

 

They do happen when you use runtime, they just won't be shown to you. Think about like how release mode compiling won't correctly let you step through your code - just as for directx, it won't be able to detect memory leaks properly when in release mode.

0

Share this post


Link to post
Share on other sites

The COM stuff has reference counting but it's not automatic. You can use a custom deleter with a smart pointer to manage DX objects automatically.

Or use a smart pointer built for COM such as CComPtr.

 

*googlygooglygoogly*

 

No MFC, no ATL...

 

_com_ptr_t

 

OMG, that could have been so useful when I was messing with COM.

Edited by Khatharr
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0