Memory access violation

Blitz3D Forums/Blitz3D Programming/Memory access violation

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

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?