Skip to main content
GameDev.net gamedev.net
🔒 Locked

access violation when moving code to different file

Started by Distec Jan 5, 2002 at 2:42 PM 2 replies 200+ views
Original Post
Distec
Distec
ok, I can''t figure this out at all. All I want to do is make a simple object(ie square) using a class. I''m using the basic.cpp framework included with dx8. It works perfectly when kept in that file. So, this is the object i''m making:
  
HRESULT CMyD3DApplication::InitSquare()
{
	
  // Initialize to render a quad

    CUSTOMVERTEX cvVertices[] =
    {
        {   0.0f,    0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color

		{ 100.0f,    0.0f, 0.5f, 1.0f, 0xffff0000, },
        { 100.0f,  100.0f, 0.5f, 1.0f, 0xff0000ff, },
        {   0.0f,  100.0f, 0.5f, 1.0f, 0xff00ff00, },
    };

	m_dwSizeofVertices = sizeof(cvVertices);

    // Create the vertex buffer

    if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_MANAGED, &m_pVB ) ) )
        return E_FAIL;

    BYTE* pVertices;
    if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, &pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, cvVertices, m_dwSizeofVertices);
    m_pVB->Unlock();
  
	
	return S_OK;
	
}

HRESULT CMyD3DApplication::DrawSquare()
{
	m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
	m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
	m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );


	return S_OK;
}
[source]

Now, this works perfectly and i get a nice square on the screen. However, its not very usefull in making multiple square without a lot of code, so I want to make my own square class to make it easier.

I make my square.h and square.cpp files and put in the following code:
square.h
[source]
#ifndef Square_H
#define Square_H


#include <tchar.h>
#include <math.h>
#include <stdio.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "resource.h"

struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw; // The transformed position for the vertex

    DWORD color;        // The vertex color

};


#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
// -------------Square CLASS-----------------

class Square : public CD3DApplication
{
protected:
	LPDIRECT3DVERTEXBUFFER8 m_pVB; // Buffer to hold vertices

    DWORD m_dwSizeofVertices;
public:
	Square();
	HRESULT Init();
	HRESULT Draw();
	~Square();

};

#endif
  
square.cpp
  

#include "Square.h"

Square::Square()
{
	m_pVB = NULL;

}

HRESULT Square::Init()
{

	// Initialize to render a quad

    

    CUSTOMVERTEX cvVertices[] =
    {
        {   0.0f,    0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color

		{ 100.0f,    0.0f, 0.5f, 1.0f, 0xffff0000, },
        { 100.0f,  100.0f, 0.5f, 1.0f, 0xff0000ff, },
        {   0.0f,  100.0f, 0.5f, 1.0f, 0xff00ff00, },
    };

	m_dwSizeofVertices = sizeof(cvVertices);
	

    // Create the vertex buffer

    if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_MANAGED, &m_pVB ) ) )
        return E_FAIL;

    BYTE* pVertices;
    if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, &pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, cvVertices, m_dwSizeofVertices);
    m_pVB->Unlock();
  
	
	return S_OK;
}

HRESULT Square::Draw()
{
	m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
	m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
	m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );

	return S_OK;
}

Square::~Square()
{
	SAFE_RELEASE( m_pVB );
}
  
When i run this code, i get an error message that says this: Unhandled exception in basic.exe: 0xC0000005: Access Violation pointing at &m_pVB in if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices, 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_pVB ) ) ) Anyone know how to fix this?
S1CA
S1CA
1. Install the debug version of the SDK/D3D runtime, ramp up the debug level in the control panel and see what your debug output says. This is ALWAYS the first thing to do - about 90% of D3D crash problems can be quickly diagnosed this way.

2. If the m_pd3dDevice creation had failed you''d get an access violation - make sure the CreateDevice call is succeeding. This would also happen if the m_pd3dDevice in the instance of the Square object wasn''t initialised (that depends on what your higher level code is doing).

3. If the device was created for software vertex processing, the vertex buffer must also be created for software vertex processing (look at the docs).



--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
Distec
Distec
I turned debug to the max and this is what I got:

Direct3D8: :This driver exposes an unrecognized DX7- style texture format
Direct3D8: (INFO) :======================= Hal SWVP device selected

Direct3D8: :DoneExclusiveMode
Direct3D8: (INFO) :HalDevice Driver Style 4

Direct3D8: (INFO) :Using P3 PSGP

Doesn''t make sense to me, since i''m not using textures or dx7.

quote:

2. If the m_pd3dDevice creation had failed you''d get an access violation - make sure the CreateDevice call is succeeding. This would also happen if the m_pd3dDevice in the instance of the Square object wasn''t initialised (that depends on what your higher level code is doing).


I tried calling another method of the 3d device and it gave me the same error, so that looks like the problem. But, how would I initialize the 3dDevice in my square class?
Distec
Distec
Thanks a lot, it was a device problem. I fixed it by passing the device through the square constructor so it uses that one.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.