Jump to content
  • Advertisement
Sign in to follow this  
Tordin

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

This topic is 3319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = (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
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];

Share this post


Link to post
Share on other sites
that "fix" do work good but the problem is still there.
any other hint on what it could be?

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 = 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;
}

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!