Skip to main content
gamedev.net
🔒 Locked

Heap Memory problem

Started by dawidjoubert 2 replies 1k views
Original Post
dawidjoubert
dawidjoubert
Hi, I am trying to load the .obj file format.

		case 'f':
			int t,format;
			long currentStart;
			currentStart = ftell(f);
			readln(f,buffer);
			// Format can be one of three
			// "f vertexIndex1 vertexIndex2 vertexIndex3 .."
			// "f vertexIndex1/coordIndex1 vertexIndex2/coordIndex2 vertexIndex3/coordIndex3 .."
			// "f vertexIndex1/coordIndex1/normalIndex1 vertexIndex2/coordIndex2/normalIndex2 vertexIndex3/coordIndex3/normalIndex3 .."
			/*if (sscanf(buffer," %d/%d/%d",&t,&t,&t) > 0) format = 2;
			else if (sscanf(buffer," %d/%d %d/%d",&t,&t,&t,&t) > 0) format = 1;
			else if (sscanf(buffer," %d %d",&t,&t) > 0) format = 0;*/
			format = 1;
			// we must count how many Vertices there are on this face
			switch(format)
			{
				case 0:
					t = strcount(buffer,' ');
					assert(1);
					break;
				case 1:
					t = strcount(buffer,'/');
					break;
				case 2:
					t = strcount(buffer,'/')/2;
					break;
			}

			face[faceInd].vert = new CVector3[t]; $$$ ERROR HERE
			face[faceInd].uv = new CVector3[t];
			face[faceInd].sides = t;

			fseek(f,currentStart,SEEK_SET);

			int vi,ti,ni;
			if (format == 1)
			{  
				// Parse 
				for (int l=0;l < t;l++)
				{
					fscanf(f," %d/%d",&vi,&ti);
					face[faceInd].vert[l] = tmpVert[vi];
					face[faceInd].uv[l] = tmpUV[vi];
				}
			}
			faceInd++;
			break;

I get memory errors here. physics.exe!_CrtIsValidHeapPointer(const void * pUserData=0x00e52d08) Line 1807 C physics.exe!_msize_dbg(void * pUserData=0x00e52d08, int nBlockUse=2) Line 1291 + 0x9 C physics.exe!_onexit(int (void)* func=0x0045ea90) Line 119 + 0xd C physics.exe!atexit(void (void)* func=0x0045ea90) Line 158 + 0x9 C physics.exe!std::_Nomemory() Line 8 + 0x32 C++ physics.exe!operator new(unsigned int size=36) Line 15 C++ physics.exe!operator new[](unsigned int count=36) Line 7 + 0x9 C++ As you can see CrtIsValidHeapPointer returns false where it should return true... So What exactly is the problem. I mean my system has enough memory but it could run out of heap because in the same function i create 6000 CVector3 s. Thanks
----------------------------http://djoubert.co.uk
SiCrane
SiCrane
The most likely problem is that you have a buffer overrun somewhere in your program that's corrupting the heap and making the program die an unhappy death.
Zahlman
Zahlman
The problem, unfortunately, could be anywhere else in your program. Something else invokes undefined behaviour, and corrupts a 'hidden' piece of data that the system was using for its memory allocation/deallocation bookkeeping. This usually happens due to writing past the bounds of an array (or indexing a std::vector beyond its .capacity(); note that you actually shouldn't index it beyond its .size(), but that will usually not mung up the heap but instead just doesn't do what you want).

If you were really running out of memory, it would instead throw an exception of type std::bad_alloc.
dawidjoubert
dawidjoubert
Quote:
Original post by Zahlman
The problem, unfortunately, could be anywhere else in your program. Something else invokes undefined behaviour, and corrupts a 'hidden' piece of data that the system was using for its memory allocation/deallocation bookkeeping. This usually happens due to writing past the bounds of an array (or indexing a std::vector beyond its .capacity(); note that you actually shouldn't index it beyond its .size(), but that will usually not mung up the heap but instead just doesn't do what you want).

If you were really running out of memory, it would instead throw an exception of type std::bad_alloc.



Thanks, yeah I found it. Was using two different integers, so i created a structure with sizeof(0) but expected it to be size 1000+
----------------------------http://djoubert.co.uk

Topic Locked

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

Sign in to reply to this topic.