Sign in to follow this  
Tordin

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

Recommended Posts

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[i] = (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

Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites
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[i] = meshBackupBuffer[i];
totalVerts += meshBackupBuffer[i].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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this