Hi i get a bus error

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Hi i get a bus error

Post by Ghoti »

Hi i try to do something as simple as this:

Code: Select all

tmpObj->ObjMeshPart[iNumberOfGroups].iFaces = fc-Correction;
fc = an int, Correction also.

Code: Select all

typedef struct {		unsigned int	iFaces;
							ObjVertexB		*Vertices;
							Image*			texture; } ObjMeshPart;
starting, they are 0 and the fc is the total of facecount and the correction is the value for the current face number.

At this line i get an bus error. what exactly is a bus error and what am i doing wrong here? I have read on this forum that bus error mostly indicates that there is an alignment error however codes like:

Code: Select all

tmpObj->iNumberOfParts = iNumberOfGroups;
works perfectly and it seems the some kind of code.
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

A bus-error could also be that you access a part of the memory that you shouldn't, you might end up with a null-address (or some other that isn't valid memory) when you dereference the pointers in the code you pasted for example.
GE Dominator
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Sorry for beeing suchs a noob but,

what is the difference between the wrong code and the correct code i gave? And what can i do to fix this?

PS i have looked at dereferencing but i don't see what i am doing wrong exactly
memon
Posts: 63
Joined: Mon Oct 03, 2005 10:51 pm

Post by memon »

My best guess is that you use iNumberOfGroups the wrong way. Say you have iNumberOfGroups = 1, and allocate 1 object, then tmpObj->ObjMeshPart[iNumberOfGroups] will access out of bounds. Off by one...

If you want to access the last object and iNumberOfGroups is the number of objects, then it is tmpObj->ObjMeshPart[iNumberOfGroups-1], or I would use: ObjMeshPart* part = &tmpObj->ObjMeshPart[iNumberOfGroups-1]; part-> ...
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

No that is not the case at all (well i guess if the following holds:)

this is a piece of my code:

Code: Select all

if ((fp = fopen(filename, "rb")) == NULL) { iNumberOfGroups = -1; return; }
		while(!feof(fp))
		{
			fgets(ReadBuffer, 256, fp);
			if (strncmp("EndGroup", ReadBuffer, 8) == 0 )
			{
			
				tmpObj->ObjMeshParts[iNumberOfGroups].Vertices = (ObjVertexB*)malloc((fc-Correction) * 3 * sizeof(ObjVertexB));
				//tmpObj->ObjMeshPart[iNumberOfGroups].iNumberOfFaces = fc - Correction;
				tmpObj->ObjMeshParts[iNumberOfGroups].iFaces = (fc-Correction);
				l = 0;

				for &#40;j=Correction;j<fc;j++&#41;
				&#123;
					for &#40;k=0; k<3; k++&#41;
					&#123;
						tmpObj->ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.u = TexCoords&#91;Faces&#91;j&#93;.textc&#91;k&#93;&#93;.u;
						tmpObj->ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.v = -&#40;TexCoords&#91;Faces&#91;j&#93;.textc&#91;k&#93;&#93;.v&#41;;
						tmpObj->ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.color = 0xffffff00;
						tmpObj->ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.x = Vertices&#91;Faces&#91;j&#93;.vertices&#91;k&#93;&#93;.x;
						tmpObj->ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.y = Vertices&#91;Faces&#91;j&#93;.vertices&#91;k&#93;&#93;.y;
						tmpObj->ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.z = Vertices&#91;Faces&#91;j&#93;.vertices&#91;k&#93;&#93;.z;
						l++;
					&#125;
				&#125;
				
				// the texture for this part.

				//sprintf&#40;sBuffer, "Textures/UVtexturesHousePlatDak1024type3.png"&#41;;
				tmpObj->ObjMeshParts&#91;iNumberOfGroups&#93;.texture = loadImage&#40;Textures&#91;iNumberOfGroups&#93;&#41;;

				// increment the group number.
				Correction = fc;
				iNumberOfGroups++;
			&#125;
			else if &#40;strncmp&#40;"v ", ReadBuffer, 2&#41; == 0 &#41;
			&#123;
				sscanf&#40;&#40;ReadBuffer+2&#41;, "%f%f%f",&Vertices&#91; vc &#93;.x, &Vertices&#91; vc &#93;.y, &Vertices&#91; vc &#93;.z&#41;; 
				vc++;
			&#125;
			else if &#40;strncmp&#40;"vt ", ReadBuffer, 3&#41; == 0 &#41;
			&#123;
				sscanf&#40;&#40;ReadBuffer+3&#41;, "%f%f",&TexCoords&#91; tc &#93;.u, &TexCoords&#91; tc &#93;.v&#41;; 
				tc++;
			&#125;

			else if &#40;strncmp&#40;"f ", ReadBuffer, 2&#41; == 0 &#41;
			&#123;
				
				char *pSplitString = NULL;
				int Waste;
				i=0;
				pSplitString = strtok&#40;&#40;ReadBuffer+2&#41;," \t\n"&#41;;

				do &#123;

					sscanf&#40;&#40;pSplitString&#41;, "%d/%d/%d",&Faces&#91; fc &#93;.vertices&#91; i &#93;, &Faces&#91; fc &#93;.textc&#91; i &#93;,&Waste&#41;; 
					Faces&#91; fc &#93;.textc&#91; i &#93; -= 1;		// 1 down because the obj file objects start at 1 and arrays start at 0
					Faces&#91; fc &#93;.vertices&#91; i &#93; -= 1;
					pSplitString = strtok&#40;NULL," \t\n"&#41;;
					i += 1;
				&#125;
				while&#40; pSplitString &#41;;
				
				
				
				
				fc++;
			&#125;
		&#125;
		fclose&#40;fp&#41;;
the only part that crashes is the part i mentioned. All the other part where i use the number of group do not crash. as you can see i reuse that variable (i know it is not good code, but i clean up afterwards, thats my way of coding) so it looks like that for some reason the problem lies in the part after the "="

i hope you can lead more out of my code now

greets ghoti
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Hi,

I have solved the problem,

the problem was indeed the index of the array but it origin of the problem lay in the reading of the object file.
every last line of the file was empty therefor the program save the previous line as the last line. This was EndGroup and therefor adding 1 to many with the index.

thanks for the replies.
Post Reply