Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


D3DX Memory leaks


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 lucky6969b   Members   -  Reputation: 636

Like
0Likes
Like

Posted 03 April 2013 - 02:45 AM

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);

    }

    

    

}


Sponsor:

#2 Vortez   Crossbones+   -  Reputation: 2704

Like
-3Likes
Like

Posted 03 April 2013 - 09:54 AM

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, 04 April 2013 - 05:32 AM.
Restored post contents from history.


#3 Juliean   GDNet+   -  Reputation: 2748

Like
0Likes
Like

Posted 03 April 2013 - 10:10 AM

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, 03 April 2013 - 10:14 AM.


#4 Khatharr   Crossbones+   -  Reputation: 3070

Like
0Likes
Like

Posted 03 April 2013 - 12:55 PM

When did everyone stop using smart pointers?


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#5 Vortez   Crossbones+   -  Reputation: 2704

Like
-2Likes
Like

Posted 03 April 2013 - 03:25 PM

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, 04 April 2013 - 05:33 AM.
Restored post contents from history.


#6 Juliean   GDNet+   -  Reputation: 2748

Like
2Likes
Like

Posted 03 April 2013 - 03:37 PM

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, 03 April 2013 - 03:39 PM.


#7 Vortez   Crossbones+   -  Reputation: 2704

Like
-5Likes
Like

Posted 03 April 2013 - 03:47 PM

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, 04 April 2013 - 05:39 AM.
Restored post contents from history.


#8 Juliean   GDNet+   -  Reputation: 2748

Like
1Likes
Like

Posted 03 April 2013 - 03:55 PM

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, 03 April 2013 - 04:02 PM.


#9 Khatharr   Crossbones+   -  Reputation: 3070

Like
0Likes
Like

Posted 03 April 2013 - 04:13 PM


 

>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, 03 April 2013 - 04:14 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#10 tanzanite7   Members   -  Reputation: 1378

Like
2Likes
Like

Posted 03 April 2013 - 04:27 PM

#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.

#11 SiCrane   Moderators   -  Reputation: 9675

Like
0Likes
Like

Posted 03 April 2013 - 04:36 PM

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.

#12 Vortez   Crossbones+   -  Reputation: 2704

Like
-2Likes
Like

Posted 03 April 2013 - 07:52 PM

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, 04 April 2013 - 05:40 AM.
Restored post contents from history.


#13 lucky6969b   Members   -  Reputation: 636

Like
0Likes
Like

Posted 04 April 2013 - 01:34 AM

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



#14 Endurion   Crossbones+   -  Reputation: 3693

Like
0Likes
Like

Posted 04 April 2013 - 02:12 AM

It's good practise that you do remove them. Retail doesn't show them because it doesn't check for them.


Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#15 Juliean   GDNet+   -  Reputation: 2748

Like
0Likes
Like

Posted 04 April 2013 - 03:13 AM

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.



#16 Khatharr   Crossbones+   -  Reputation: 3070

Like
0Likes
Like

Posted 06 April 2013 - 09:41 PM

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, 06 April 2013 - 09:42 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS