Thread: I got an unhandled exception!!

  1. #1
    Registered User
    Join Date
    Apr 2005
    Location
    New Brunswick, Canada
    Posts
    8

    I got an unhandled exception!!

    Hi,
    I am trying to implent a graphic script into the script I already got... it's with DirectX 9.0 and C++... The error is "Unhandled exception at 0x004a5a94 in Code Name RLS.exe: 0xC0000005: Access violation reading location 0x00000000."

    And the error is near that code:

    Code:
    HRESULT CMyD3DApplication::InitDeviceObjects()
    {
    
        m_pFont->InitDeviceObjects( m_pd3dDevice );
        m_pFontSmall->InitDeviceObjects( m_pd3dDevice );
        m_pFontLoad->InitDeviceObjects( m_pd3dDevice );
    
        Vertex* Vertices;
    	if(FAILED(Vertex_Buffer->Lock(0, sizeof(Polygon_Data), (void**)&Vertices, 0)))
          return false;
    
        memcpy(Vertices, Polygon_Data, sizeof(Polygon_Data));
    
        Vertex_Buffer->Unlock();
       
    		if(FAILED(m_pd3dDevice->CreateVertexBuffer(sizeof(TexCoords_Data), 0, 0,
                                                D3DPOOL_DEFAULT, &TexCoord_Buffer, NULL)))
          return false;
    	
        TexCoords* TexC;
    
        if(FAILED(TexCoord_Buffer->Lock(0, sizeof(TexCoords_Data), (void**)&TexC, 0)))
          return false;
    
        memcpy(TexC, TexCoords_Data, sizeof(TexCoords_Data));
    
        TexCoord_Buffer->Unlock();
    
        D3DVERTEXELEMENT9 Declaration[] = 
    	   {
    	      {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
    		   {1, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
    		   D3DDECL_END()
    	   };
    	   m_pd3dDevice->CreateVertexDeclaration(Declaration, &VertexDeclaration);
    
        if(D3DXCreateTextureFromFile(m_pd3dDevice, "ugp.tga", &Texture) != D3D_OK)
          return false;
    
    	return S_OK;
    }
    I'm not getting it... can someone help me? There is no warnings at build time either errors codes...

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    The only thing I can see right now that I think looks suspicious are these lines:
    Code:
    Vertex* Vertices;    // Remember, this is a pointer, no memory allocated
    memcpy(Vertices, Polygon_Data, sizeof(Polygon_Data));    // This is what I think is suspicious, 
    //I am not very familiar with memcpy but I dont think it will allocate memory (might be wrong).
    
    // same with TexCoords* TexC;
    These are the things I can see right away.

  3. #3
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Shakti: In general cases you are right Unfortunately, in this case it's slightly different. the Lock() function on a vertex buffer object actually returns a pointer (through the parameter list) which is used to access the area of the vertex buffer that you want to work with. So after the Lock() call, Vertices will not be null (assuming of course that the function succeeds ).

    LegendBreath: Helping you solve this problem is going to be tough with the small snippet of code you've posted, along with the small amount of information given. Could you perhaps let us know which line of code the error is occurring on? Or could you post your entire source file (as an attachment of course ).

    As a quick thought, just make sure your Vertex_Buffer object has been initialised, because it's not actually initialised in the function you posted.

    Cheers.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ah ok, thanks for clearing that up.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Location
    New Brunswick, Canada
    Posts
    8
    Here, I'm posting all the source code... thanks for your help :P

    Rename ".bmp" to ".tga" and, it will maybe ask for mainmenu.manifest if so, here it is:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity
        version="1.0.0.0"
        processorArchitecture="X86"
        name="The Legend"
        type="win32"
    />
    <description>Legend</description>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity
                type="win32"
                name="Microsoft.Windows.Common-Controls"
                version="6.0.0.0"
                processorArchitecture="X86"
                publicKeyToken="6595b64144ccf1df"
                language="*"
            />
        </dependentAssembly>
    </dependency>
    </assembly>

  6. #6
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    As i suspected.

    Vertex_Buffer is never created. ie. it's declared, and you attempt to lock it before you create it! Make sure that you create the vertex buffer in your InitDeviceObjects() function.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Location
    New Brunswick, Canada
    Posts
    8
    May I ask a noob question... how do I create the vertex buffer???

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by LegendBreath
    May I ask a noob question... how do I create the vertex buffer???
    Using the method you call here:
    Code:
    if(FAILED(m_pd3dDevice->CreateVertexBuffer(sizeof(TexCoords_Data), 0, 0,
                                               D3DPOOL_DEFAULT, &TexCoord_Buffer, NULL)))
    Do you create one for your texcoords and positions? you probably could do with just having one vertex buffer that contains your position, normal, texcoords, etc.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unhandled exception
    By JOCAAN in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 10:18 AM
  2. Replies: 3
    Last Post: 11-11-2006, 06:46 PM
  3. unhandled exception error
    By trends in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 06:54 PM
  4. Unhandled exception
    By pdstatha in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2002, 06:03 PM
  5. Unhandled Exception
    By StringQuartet13 in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2002, 05:18 PM