CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    How to initialize an array of DirectX9 Objects?

    Hello,
    I've got an error saying that there is an access violation at 0x0000040
    error no 0xC0000005
    I've searched the net, saying the error may be due to an uninitialzed variable.
    I debugged the code, and found out that an object created from directx D3DXMATRIXA16 is uninitialized.
    With values ??,??,?? in each element

    pMeshContainer->pBoneMatrices = new D3DXMATRIXA16[g_NumBoneMatricesMax];

    here, pMeshContainer is a variable passed into a function called GenerateSkinnedMesh by pointer
    GenerateSkinnedMesh(..., D3DXMESHCONTAINER_DERIVED *pMeshContainer)

    Then in turn, GenerateSkinnedMesh is called within a callback from a DirectX9 API called
    ID3DXAllocateHierarchy


    Code:
    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 );
    
        CAllocateHierarchy()
        {
        }
    };
    when the method CreateMeshContainer finished execution,
    the meshContainer is passed back to its parent like this

    *ppMeshContainer = pMeshContainer;

    The whole meshContainer stuff is stored persistently inside the frame root wrapped within a class called
    CMesh

    So in that process, I haven't initialized pBoneMatrices in anyways. But what and where is the best way to
    initialize an array of DirectX9 objects.

    There is a function called
    D3DXMatrixIdentity(&...);

    But how can I initialize each one of them with this call?
    Notice that pMeshContainer->pBoneMatrices does contain a valid address, despite the fact that
    the elements inside it are never initialized...
    Thanks
    Jack

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to initialize an array of DirectX9 Objects?

    1) Are you debugging a release build or a debug build?

    2) This:
    Code:
    pMeshContainer->pBoneMatrices = new D3DXMATRIXA16[g_NumBoneMatricesMax];
    does not default initialize the members of the struct. This does:
    Code:
    pMeshContainer->pBoneMatrices = new D3DXMATRIXA16[g_NumBoneMatricesMax]();
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: How to initialize an array of DirectX9 Objects?

    Is this
    Quote Originally Posted by Paul McKenzie View Post
    Code:
    pMeshContainer->pBoneMatrices = new D3DXMATRIXA16[g_NumBoneMatricesMax]();
    not called "value initialisation" as opposed to "default initialisation" which, for POD types, does nothing at all?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to initialize an array of DirectX9 Objects?

    Quote Originally Posted by Richard.J
    not called "value initialisation" as opposed to "default initialisation" which, for POD types, does nothing at all?
    Value initialisation means zero initialisation in such cases.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: How to initialize an array of DirectX9 Objects?

    I am not sure if I understand correctly.
    Say you have
    Code:
    int *p = new int[3];
    This leaves you with a default initialised array which may contain any value, depending on the compiler, doesn't it?
    On the other hand,
    Code:
    int *p = new(int[3])();
    gives you an array of three ints, all initialised to zero?
    At least this is what I think I understood using VC2010.
    Are there differences regarding the different C++ standards?

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to initialize an array of DirectX9 Objects?

    Yeah. I think that the standard has not changed in this respect: C++11 introduced standard layout types, but these initialisation rules were specified with respect to class types, array types and other types, not specfically POD types.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured