D3DX Memory leaks

Started by
14 comments, last by Khatharr 11 years ago

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 != NULL)

            {

                 textures->Release();

                 textures = 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, 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 = *(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 != NULL)

        {

            pMeshContainer->textures->Release();

            pMeshContainer->textures = 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 = &b->CombinedTransformationMatrix;

                else boneMesh->boneMatrixPtrs = 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,

                                   &boneMesh->boneOffsetMatrices,

                                   boneMesh->boneMatrixPtrs);

            }



            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.AttribId;

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

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

                g_pDevice->SetMaterial(&(boneMesh->materials));

                g_pDevice->SetTexture(0, boneMesh->textures);

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



                g_pEffect->SetVector( "materialColor",

                                         ( D3DXVECTOR4* )&(

                                         boneMesh->materials.Diffuse ) );



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

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

                    g_pEffect->SetVector( "materialColor",

                                             ( D3DXVECTOR4* )&(

                                             boneMesh->materials.Diffuse ) );

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



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

    }

    

    

}
Advertisement

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.

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.

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.

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

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

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

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 :/


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

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.

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

This topic is closed to new replies.

Advertisement