Debug problem

This is a discussion on Debug problem within the C++ Programming forums, part of the General Programming Boards category; Hi! I have a DirectX program which works correctly when I launch it, but when I debug it, it gives ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    Debug problem

    Hi! I have a DirectX program which works correctly when I launch it, but when I debug it, it gives me error in the middle of the DirectX initalyzing function: Unhandled exception at 0x004119f8 in MyProgram.exe: 0xC0000005: Access violation reading location reading 0xcccccccc.

    I think that D3DXMtrl_Buffer gives problems, but I'm not absolutely sure and I don't know how to fix it.

    Here's my problematic function for initalyzing DirectX:


    Code:
    //Globals
    LPDIRECT3D9             DirX_3D            = NULL;
    LPDIRECT3DDEVICE9       DirX_Device        = NULL;
    LPD3DXMESH              tiiger_mesh     = NULL;
    LPDIRECT3DTEXTURE9*        Tekstuur        = NULL;
    D3DMATERIAL9*            D3D_Material    = NULL;
    DWORD mat_nr=0L;
    HRESULT hr;
    
    void InitDirX(HWND hWnd)
    {
    //----------------------------------------------
    //                InitDirX
    //----------------------------------------------
    
        D3DPRESENT_PARAMETERS    pp;
        LPD3DXBUFFER            D3DXMtrl_Buffer;
    
        //Creating Direct3D object
        if(NULL==(DirX_3D=Direct3DCreate9(D3D_SDK_VERSION)))
            {
                 //error
            }
        
        ZeroMemory(&pp, sizeof(pp));
    
    
        pp.Windowed=                TRUE;
        pp.SwapEffect=                D3DSWAPEFFECT_DISCARD;
        pp.BackBufferFormat=        D3DFMT_UNKNOWN;
        pp.EnableAutoDepthStencil = TRUE;
        pp.AutoDepthStencilFormat = D3DFMT_D16;
    
        //Creating DirectX device
        hr=DirX_3D->CreateDevice(D3DADAPTER_DEFAULT,
                                    D3DDEVTYPE_HAL,
                                    hWnd,
                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                    &pp,
                                    &DirX_Device);
            if(FAILED(hr))
                {
                    //error
                }
    
        DirX_Device->SetRenderState( D3DRS_ZENABLE, TRUE );
        DirX_Device->SetRenderState( D3DRS_AMBIENT, 0xffffffff );
    
        //Loading Mesh
        hr=D3DXLoadMeshFromX("Tiger.x", D3DXMESH_SYSTEMMEM,
                                       DirX_Device, NULL,
                                       &D3DXMtrl_Buffer, NULL, &mat_nr,
                                       &tiiger_mesh);
        if(FAILED(hr))
        {
              //error
        }
    
        D3DXMATERIAL* D3DX_Materials = (D3DXMATERIAL*)D3DXMtrl_Buffer->GetBufferPointer(); //In this line it gives me error!!!
        D3D_Material    =    new D3DMATERIAL9[mat_nr];
        Tekstuur        =    new LPDIRECT3DTEXTURE9[mat_nr];
    
        for( DWORD i=0; i<mat_nr; i++ )
        {
    
            D3D_MaterialIdea = D3DX_MaterialsIdea.MatD3D;
    
            D3D_MaterialIdea.Ambient = D3D_MaterialIdea.Diffuse;
    
            TekstuurIdea = NULL;
    
            //Creating texture
            hr=D3DXCreateTextureFromFile(DirX_Device,
                                      D3DX_MaterialsIdea.pTextureFilename,
                                      &TekstuurIdea);
            if(FAILED(hr))
            {
                 //error
            }
        }
    
        D3DXMtrl_Buffer->Release();
    
    }
    What's wrong with it?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    all you //error comment
    Do you exit or continue excution as in the sample?

    Then probably hr=D3DXLoadMeshFromX failed - but you still go father and get your crash

    You should fix this...

    And the reason for failure - Check the Current Directory set for the Debug session, probably it is different from the one use during run, and some files located in the directory together with the exe are not found in the debug run
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    Some function has thrown an exception and since it has not been handled, the program has ended. To debug, try to find where this exception has been thrown from (ie, whic function). As if anyone can tell from a small and confusing segment of your code.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Quote Originally Posted by vart View Post
    all you //error comment
    Do you exit or continue excution as in the sample?

    Then probably hr=D3DXLoadMeshFromX failed - but you still go father and get your crash

    You should fix this...

    And the reason for failure - Check the Current Directory set for the Debug session, probably it is different from the one use during run, and some files located in the directory together with the exe are not found in the debug run
    You're right. It was my mistake. I didn't know, that during debugging it tries to load files from a different folder, so it didn't find the mesh file.

    How can I change that directory?

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    If you are using VS - Open Project properties/Debug
    Set the Working directory to the correct path
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Help, Debug Problem
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2002, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21