Memory Access Violations.. arggg.

Started by
4 comments, last by MatthewB 21 years, 4 months ago
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]
Advertisement
At some point you need to learn how to use a debugger, that would probably help you out of this situation.
Using a debugger in a fullscreen DirectX app is next to impossible for those of us without second monitors.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
I usually write my apps so I can run both in windowed and exlusive mode. It''s some extra pain, but it''s really useful being able to run under a debugger.

In any case, you can run outside the debugger and set DrWatson to create crash dumps, that you can load into VS.Net or windbg. DrWatson will see the crash, capture the process'' memory including calls stacks for all threads, and write a foo.dmp file for you.

If you have two computers, you could use remote debugging, that''s also very helpful and can be done with both visual studio and windbg.

http://www.microsoft.com/ddk/debugging/default.asp

http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/windowsnetserver/proddocs/datacenter/drwatson_setup.asp?frame=true

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/debugging_terminology.asp
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
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

This topic is closed to new replies.

Advertisement