Memory access violation

Blitz3D Forums/Blitz3D Programming/Memory access violation

sswift(Posted 2004) [#1]
I'm getting a memory access violation in this function on the specified line at the end where I delete a type.

Function CLIP_ClipPoly(POLY_Clip.CLIP_Poly, Interpolate=True)	

	Local POLY_Input.CLIP_Poly, POLY_Output.CLIP_Poly, POLY_Temp.CLIP_Poly, POLY_Swap.CLIP_Poly   
	Local LOOP_Plane.CLIP_Plane
	Local LOOP_Vertex
	Local V1_Index, V2_Index, VI_Index
	Local V1_Inside, V2_Inside
	Local Vertices

	; Create a polygon to hold the new polygon that results each time we clip the input polygon.
		POLY_Temp = New CLIP_Poly
	
	; Set up the input/output buffer pointers.	
		POLY_Input  = POLY_Clip
		POLY_Output = POLY_Temp	
				
	; For each clipping plane...	
	For LOOP_Plane = Each CLIP_Plane
				
		; Clear all vertcies from the output polygon.
		POLY_Output\Vertices = 0
				
		; For each edge in the input polygon...
		For LOOP_Vertex = 0 To POLY_Input\Vertices-1

			; Caclulate the vertices that form this edge.

				V1_Index = LOOP_Vertex
				V2_Index = LOOP_Vertex+1
	
				If V2_Index = POLY_Input\Vertices Then V2_Index = 0
				
			; Are these vertices on the back side of the clipping plane?
			V1_Inside = CLIP_Vertex_Inside_Plane(POLY_Input, V1_Index, LOOP_Plane)
			V2_Inside = CLIP_Vertex_Inside_Plane(POLY_Input, V2_Index, LOOP_Plane)
		
			Select True

				; Output V2.			
				Case ((V1_Inside=True) And (V2_Inside=True))
				
					; Add V2 (endpoint of edge) to output polygon.
					CLIP_CopyVertex(POLY_Input, V2_Index, POLY_Output)


				; Output intersection point.	
				Case ((V1_Inside=True) And (V2_Inside=False))

					; Find the point at which the edge intersects this plane.
					CLIP_Edge_Intersect_Plane(POLY_Input, V1_Index, V2_Index, LOOP_Plane)
					
					; Add intersection point.
					Select Interpolate 
						Case True  CLIP_Add_Interpolated_Vertex(POLY_Output, V1_Index, V2_Index, CLIP_Intersect_U#)
						Case False CLIP_AddVertex(POLY_Output, CLIP_Intersect_X#, CLIP_Intersect_Y#, CLIP_Intersect_Z#)
					End Select


				; No output.	
				Case ((V1_Inside=False) And (V2_Inside=False))
				
					; Both vertcies are on the outside side of the plane. 
					; Don't add any vertices to the polygon.
			
				
				; Output Intersection point and V2.
				Case ((V1_Inside=False) And (V2_Inside=True))

					; Find the point at which the edge intersects this plane.
					CLIP_Edge_Intersect_Plane(POLY_Input, V1_Index, V2_Index, LOOP_Plane)

					; Add intersection point.
					Select Interpolate 
						Case True  CLIP_Add_Interpolated_Vertex(POLY_Output, V1_Index, V2_Index, CLIP_Intersect_U#)
						Case False CLIP_AddVertex(POLY_Output, CLIP_Intersect_X#, CLIP_Intersect_Y#, CLIP_Intersect_Z#)
					End Select

					; Add V2.
					CLIP_CopyVertex(POLY_Input, V2_Index, POLY_Output)
															
			End Select 		

		Next
		
		; If there are no vertices in the final polygon...
		If POLY_Output\Vertices = 0
			
			; The polygon has been completely clipped away.
						
			; Delete all vertices from the original polygon, delete the temporary storage buffer, and exit the function early.
			POLY_Clip\Vertices = 0
			Delete POLY_Temp  ; <- Memory access violation???
			Return
			
		EndIf	

		; Swap the pointers to the input polygon data and the output polygon data
		; This makes the output data from the last loop the input data to the next,
		; and uses the old input location to store the new output data.
		POLY_Swap   = POLY_Output
		POLY_Output = POLY_Input
		POLY_Input  = POLY_Swap
		
	Next	

	; If we've made it this far, we have a clipped polygon that lies within the frustum!

	; Copy the final output over the original polygon data, delete the temporary storage buffer, and exit the function.
	; (We swapped input and output at the end of the last loop, so input really points to the final output.)
	CLIP_CopyPoly(POLY_Input, POLY_Clip)
	Delete POLY_Temp
	Return 
	
		
End Function



Does anyone see an error in this code? I don't.

Poly_Temp is a local variable. It cannot be affected by outside functions.

It is created once at the start of the function, and the only times it is deleted are at two points. One is at the end of the function, and the other is one line before the function is exited early.

So how is this code causing a memory access violation?


sswift(Posted 2004) [#2]
Hm...

Cancel that. I just tried to recreate the memory access violation and now I got one in an entirely different area when trying to free a mesh. A portion of my code that I know works, and which has worked for months and is unchanged.

I suspect either a bug in Blitz, or this problem I've been having with my system crashing which might be due to bad ram might be causing the intermittent issue.


Sweenie(Posted 2004) [#3]
Bad ram can really play tricks with you.

A friend of mine couldn't for example play the game VietCong because the characters kept falling through the terrain. After running a memorydiagnostic tool we knew that one of the memorysticks were defect, replaced it and voila(or Viola as someone in this forum said), solid ground in Vietcong. :)
The bad ram also caused 75% of all installers to fail with an error stating that the cabs were corrupt.

So, I really recommend running a memorydiagnostic tool if your system behaves weird.
I can try to find the one we used, if you like.


poopla(Posted 2004) [#4]
I'd appreciate if you find that tool, I have to run a mem diag on my dads PC and need a tool for it.


sswift(Posted 2004) [#5]
I already ddi try running a memory diagnostic and it found nothing. And I replaced just ebout every other component in my system except the video card.

Shattered:
I hear this one is the best, and free.

But it did not find my error, if I have one. And I left it running for a good 12 hours.

http://www.memtest86.com/


Sweenie(Posted 2004) [#6]
Ok, here it is...

http://oca.microsoft.com/en/windiag.asp


Shambler(Posted 2004) [#7]
What happens if you replace that line 'Delete Poly_Temp' where the MAV occurs with 'Delete Poly_Output' ?