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

Memory Access Violations.. arggg.

Started by MatthewB Dec 25, 2002 at 8:23 AM 4 replies 300+ views
Original Post
MatthewB
MatthewB
Following is some code from a "live" mesh exporter I am writing to take the vertex information from objects in my 3D modelling software and copy that into my DX8.1 scene. Everything compiles with no warnings or errors, but upon running it, I get Windows memory access violations at seemingly random places. I know it has to be mis-allocated pointers somewhere in it, but I have looked through it for several hours and tried changing a little here and a little there with no success. If some of you experienced C++ programmers would not mind looking through this, perhaps it will be easy to spot for someone who does not have his head buried in this project.
        
// ***** From the top of the file ******

//

// A structure for our custom vertex type

struct CUSTOMVERT
{
    FLOAT x, y, z;      // The untransformed, 3D position for the vertex

    DWORD color;        // The vertex color

};

// Our custom FVF, which describes our custom vertex structure

#define MYFVF (D3DFVF_XYZ|D3DFVF_DIFFUSE)

LPDIRECT3DVERTEXBUFFER8	 g_vertBufferArray;
LPDIRECT3DDEVICE8	 OurDXDevice;
LPDIRECT3D8		 OurDXInterface;

// **** Ok, now the "meat" of the class ****

//

HRESULT MyDXInterface::ImportGeometry()
{
	CUSTOMVERT* imp_verts;
	int icount = 0, iinnercount, numVerts;
	HRESULT retval = E_FAIL;
	CtsxVector3f *myVertices;
	char message[255];
	
	imp_verts = new CUSTOMVERT[];
	numModels = 0;

	tsxGNODE *pGNode = tsxSceneGetFirstNode();
	
	//Loop through all the objects in the scene

	for(; pGNode != NULL; pGNode = tsxGNodeGetNext(pGNode))
	{
		sprintf(message, "numModels = %d",numModels);
		AfxMessageBox(message);

		switch(tsxSobjGetType((tsxSOBJ*)pGNode))
		{
			case e_tsxPOLYHEDRON:
				AfxMessageBox("Inside Case e_tsxPolyhedron");
				break;
			case e_tsxSUBDIV:
				//maybe someday

				continue;
				break;

			case e_tsxGROUP:
				continue;
				break;

			default:
				continue;
				break;
		}

		
		//tsxPOLYHEDRON* myPoly = (tsxPOLYHEDRON*) pGNode;


		//Get the number of vertices in this model.

		numVerts = tsxPolyhGetNbrVxs((tsxPOLYHEDRON*)pGNode);
				
		// Get the array of vertices 

		myVertices = tsxPolyhGetVxArray((tsxPOLYHEDRON*)pGNode);
		
		//Loop through these vertices and assign the members

		//of the imp_verts structure to the appropriate values.

		for(iinnercount=0;iinnercount<numVerts;iinnercount++)
		{
			sprintf(message, "Inner loop # %d and newX = %6.3f",iinnercount, myVertices[iinnercount].x);
			AfxMessageBox(message);
			
			imp_verts[iinnercount].x = (float)myVertices[iinnercount].x;
			imp_verts[iinnercount].y = (float)myVertices[iinnercount].y;
			imp_verts[iinnercount].z = (float)myVertices[iinnercount].z;
			//imp_verts[iinnercount].color = 0xBDF021;//Greenish-Yellow maybe?

			
		}
		

		//Place the completed vertex buffer into the g_vertBufferArray

		/*if( FAILED( OurDXDevice->CreateVertexBuffer( numVerts*sizeof(CUSTOMVERT),
                                                  0, MYFVF,
                                                  D3DPOOL_DEFAULT, &g_vertBufferArray[numModels] ) ) )
		{
			return E_FAIL; 
		}*/
		numModels++;
	}
	delete imp_verts;
	return S_OK;
}
//

// **** Class Definition ****

//

class MyDXInterface  
{
public:
	
	HRESULT ImportGeometry();
	void PaintDX();
	void MainDX(HWND);
	MyDXInterface();
	virtual ~MyDXInterface();
	void UninitDX();
	int numModels;

private:	
	int InitDX();
	HWND wHandle;
	RECT OurDXClip;
}; 
       
[edited by - MatthewB on December 25, 2002 9:34:24 AM]
AndyOxfeld
AndyOxfeld
Here''s how I do debugging when I''m lazy... (in a DX app)

Basically, if I want to check if the program is getting to a certain section of code, I just put an exit(0) there, and if the game exits, then I know it got there

Similary, I can check the values of variables and stuff by doing:

if (variable == 5) exit(0);

Or such. When I''m feeling less lazy, rather than exitting, I''ll fopen a log file, and fprintf values of variables, etc. to it. And in a non-DX app, I usually take the time to write a full-fledged error system.

- Andy Oxfeld
MatthewB
MatthewB
I finally found the problem.... And it turned out to be fairly simple. With the imp_vert pointer I was incorrectly using the "new" and "delete" keywords. Now I am doing the "new" in the inner loop with a number in the square brackets and the "delete [] imp_vert" after I am done with it to free it up for the next outer loop. Good grief.. why did it take me nearly 24 hours to find it? Anyway, I''m glad it''s working error free now

Topic Locked

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

Sign in to reply to this topic.