Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pointer Bug?
#1
The following indexing code crashes (can't read memory error):
Code:
Type Vert
     x As Single
     y As Single
     z As Single
End Type

Type Tri
     verts(2) As Vert
End Type

Type Object
     numoftris As Integer
     tris As Tri Ptr
End Type

Dim obj As Object

With obj
     .numoftris = 7
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris
        For v = 0 To 2
            .tris[i].verts(v).x = Rnd * 23
            .tris[i].verts(v).y = Rnd * 75
            .tris[i].verts(v).z = Rnd * 51
        Next v
     Next i
End With

Print obj.numoftris, obj.tris[0].verts(0).y '<- Error here


Sleep

But doesn't with a minor change to print the values as they're being set:

Code:
Type Vert
     x As Single
     y As Single
     z As Single
End Type

Type Tri
     verts(2) As Vert
End Type

Type Object
     numoftris As Integer
     tris As Tri Ptr
End Type

Dim obj As Object

With obj
     .numoftris = 7
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris
        For v = 0 To 2
            .tris[i].verts(v).x = Rnd * 23
            .tris[i].verts(v).y = Rnd * 75
            .tris[i].verts(v).z = Rnd * 51
            Print .tris[i].verts(v).x  '<- Change
        Next v
     Next i
End With

Print obj.numoftris, obj.tris[0].verts(0).y '<- Reads fine with print in loop


Sleep

What's wrong? :o
Reply
#2
I don't get an error on either one.

WinXP Pro SP2
ature has its way of warning a person away from danger: The distinct black and white coloration on a skunk, the chilling buzz of a rattlesanke, a redneck handing you his beer and saying "Watch this!"
Reply
#3
Yeah, there was a bug with 0.11 when calculating the len of types that have array fields (reported by Dr_D), it was fixed 2 weeks ago, the patch will be on the next release.

That code worked fine with version 0.12.

Btw, it should be: for i = 0 To .numoftris-1
Reply
#4
Got it. Thanks.
Reply
#5
One more thing, there also seems to be another bug in assigning variables to pointer indexes. This time the compiler crashes.:

Code:
Type Vert
     x As Single
     y As Single
     z As Single
End Type

Type Tri
     verts(2) As Vert
End Type

Type Object
     numoftris As Integer
     tris As Tri Ptr
End Type

Dim obj As Object

With obj
     .numoftris = 7
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris - 1
        For v = 0 To 2
            .tris[i].verts(v).x = Rnd * 23
            .tris[i].verts(v).y = Rnd * 75
            .tris[i].verts(v).z = Rnd * 51
        Next v
     Next i
End With

newfile = FreeFile

Open "data.obj" For Binary As #newfile

Put #newfile,,obj.numoftris

For i = 0 To obj.numoftris - 1
    Put #newfile,,obj.tris[i].verts()
Next i

Close #newfile

Deallocate(obj.tris)

Dim newobj As Object, temptri As Tri

readfile = FreeFile
Open "data.obj" For Binary As #readfile

Get #readfile,,newobj.numoftris
Print newobj.numoftris
newobj.tris = Allocate(newobj.numoftris * Len(Tri))
For i = 0 To newobj.numoftris - 1
    Get #readfile,,newobj.tris[i].verts() '<- Crashes here, compiles when commented out.
    Print "X: ", newobj.tris[i].verts(0), "Y: ", newobj.tris[i].verts(1), "Z: ", newobj.tris[i].verts(2)
Next i
Close #readfile

Sleep

Even using a temp variable to read in a triangle (temptri), assigning it to the pointer index fails.
Reply
#6
Problem is at Print "X: ", newobj.tris[i].verts(0), "Y: ", newobj.tris[i].verts(1), "Z: ", newobj.tris

TYPE's can't be printed, but PRINT and WRITE are not checking for that, new check added..
Reply
#7
LOL, are you serious? :oops: I really, really need to proofread my code more, Thanks.

The test works now:

Code:
With newobj
     Get #readfile,,.numoftris
     Print "Number of triangles: "; .numoftris
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris - 1
         Get #readfile,,.tris[i].verts()
         Color i + 10
         Print "Triangle: ";i
         Print "X: "; .tris[i].verts(0).x; " Y: "; .tris[i].verts(0).y; " Z: "; .tris[i].verts(0).z
         Print "X: "; .tris[i].verts(1).x; " Y: "; .tris[i].verts(1).y; " Z: "; .tris[i].verts(1).z
         Print "X: "; .tris[i].verts(2).x; " Y: "; .tris[i].verts(2).y; " Z: "; .tris[i].verts(2).z
     Next i
     Close #readfile
End With
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)