C++/dx10 - Cant fill vertexbuffer could be a heap problem or a dx problem.

Started by
6 comments, last by Tordin 14 years, 10 months ago
Hello! i am trying to fill a vertex buffer, but i only end up in a runtime break from msvs. "Cant read memory location" or "std::bad_aloc". here is the code where the problem lies (have found it with the exclude-your-code-until-you-find-it) i did exclude som of the code here beucs i dont whant to take up an houer just put it up! if anything else is needed il put it up on demand!


VTX* vertexData = NULL;
	VTXN* vertexDataN = NULL;

if(sh.g_Norm)
		vertexDataN = new VTXN[numVert]; 
	else if(!sh.g_Norm)
		vertexData = new VTX[numVert];

myfile.clear();
	myfile.seekg(0);
	numVert = vtxPos;
	index = new int[indexes*3];
	vtxPos = 0;		

float copyx[3] = {0.0f,0.0f,0.0f};
				for(int i = 0; i<3; i++) //läser in värdet.
				{
					char read[10];
					myfile>>read;
					for(int r = 0; r<10; r++)
						line += read[r];
					copyx = (atof(line.c_str())); //copy pasta!
					line = "";
				}
				if(sh.g_Norm) //tildelar om vi har normaler
				{
					vertexDataN[vtxPos].vtxPos = D3DXVECTOR3(copyx[0],copyx[1],copyx[2]);
					vertexDataN[vtxPos].color = D3DXVECTOR4(1.0f,1.0f,1.0f,1.0f);
				}
				else if(!sh.g_Norm) //tildelar om vi inte har normaler
				{
					vertexData[vtxPos].vtxPos = D3DXVECTOR3(copyx[0],copyx[1],copyx[2]);
					vertexData[vtxPos].color = D3DXVECTOR4(1.0f,1.0f,1.0f,1.0f);
				}
				line = ""; //tömsi!
				vtxPos++;


so the problem is that i cant find out what i have done wrong is this code, that results in a runtime break from msvs. cheers o7
"There will be major features. none to be thought of yet"
Advertisement
I don't understand how you read from the file, but it looks complicated and wrong to me. IMO, that should be simple like that, assuming myfile is a valid ifstream :
myfile >> copyx[0] >> copyx[1] >> copyx[2];
that "fix" do work good but the problem is still there.
any other hint on what it could be?
"There will be major features. none to be thought of yet"
On which line do you get exception or error message?
I do get it alitle of every where, but i most of the time get it at this line in ANOTHER function :/

void MeshHolder::addMesh(MeshInstance &in_Mesh,Mesh *ing_Mesh,Texture *in_Tex){		totalVerts = 0;	if(meshBuffer != NULL)		meshBackupBuffer = meshBuffer;	length++;	meshBuffer = new MeshInstance[length]; <--- here! <!"!"!******<---- 	for(int i = 0; i<(length-1); i++)	{		meshBuffer = meshBackupBuffer;		totalVerts += meshBackupBuffer.g_Mesh->numVert;	}	in_Mesh.g_Mesh = ing_Mesh;	in_Mesh.tex = in_Tex;	in_Mesh.memPos = length-1;	meshBuffer[(length-1)] = in_Mesh;	totalVerts += in_Mesh.g_Mesh->numVert;	delete [] meshBackupBuffer;}
"There will be major features. none to be thought of yet"
Does length variable contains sane number?
What does MeshInstance constructor look like? Maybe it does something bad?

And instead of new [] better use std::vector - then you will not need to worry about calling delete [], it delete memory it allocated automatically. Also you will not need to copy manuall all memory to add new mesh - std::vector has push_back method that adds new element at the end of array.
oh thanks, i gona try the vector out right away.
the length variabel starts at 0 and adds one every time this fuction is called, and the constructor of the MeshInstance is only setting the variabels to Zero (pos,rot,scale and mempos)

MeshInstance::MeshInstance(){	pos.x = 0.0f;	pos.y = 0.0f;	pos.z = 0.0f;	rot.x = 0.0f;	rot.y = 0.0f;	rot.z = 0.0f;	scale.x = 1.0f;	scale.y = 1.0f;	scale.z = 1.0f;	memPos = 0; }
"There will be major features. none to be thought of yet"
I still get the same breakerror at the same location.
even when i do it with a vector, and this is the result code of it.
void MeshHolder::addMesh(MeshInstance &in_Mesh,Mesh *ing_Mesh,Texture *in_Tex){			in_Mesh.g_Mesh = ing_Mesh;	in_Mesh.tex = in_Tex;	in_Mesh.memPos = length;	totalVerts += in_Mesh.g_Mesh->numVert;	meshBuff.push_back(&in_Mesh);}
"There will be major features. none to be thought of yet"

This topic is closed to new replies.

Advertisement