I have it isolated in this small code example.
In the destructor after img1 gets imagedestroy, I cast the any ptr to an integer ptr and make its contents 0.
Doing this seems to knock out the img2 ptr.
When stepping through the code with Sarg's debugger an error occurs on the next line (something about heap).
Commenting out the *cast line removes the error.
Code: Select all
Type test
As Any Ptr img1
As Any Ptr img2
Declare Destructor()
End Type
Destructor test()
ImageDestroy(img1)
*Cast(Integer Ptr, img1) = 0 ' comment to remove error
img1 = 0
Imagedestroy(img2) ' crashes here when img1 is casted to integer ptr above
*Cast(Integer Ptr, img2) = 0
img2 = 0
End Destructor
ScreenRes 800,600
Dim As test t
t.img1 = ImageCreate(110,110)
t.img2 = ImageCreate(110,110)
Code: Select all
function createimage(w as long,h as long,clr as ulong=rgba(255,0,255,255)) as any ptr
#define pad(n) (n)+(n) mod 16
dim as ulong ptr p=callocate(w*h,sizeof(ulong))
p[0]=7 'always
p[1]=4 'pixelsize
p[2]=w 'width
p[3]=h 'height
p[4]=pad(w*p[1]) 'pitch -- padded to a multiple of 16
for a as long=5 to 7
p[a]=0 'reserved
next
for a as long=8 to h*w
p[a]=clr 'colour
next
return p
end function
#define destroyimage(i) deallocate i ':i=0
Type test
As Any Ptr img1
As Any Ptr img2
Declare Destructor()
End Type
Destructor test()
destroyimage(this.img1)
*Cast(Integer Ptr, img1) = 0
img1 = 0
destroyimage(this.img2)
'*Cast(Integer Ptr, img2) = 0
img2 = 0
End Destructor
Screenres 800,600, 32
Dim As test t
t.img1 = createImage(110,110)
t.img2 = CreateImage(110,110)
The pointer is deallocated and although the variable still points to a memory address, that address is not safe. It could be used by other processes at any time.
But why is this affecting the img2 variable and memory address? At this point that memory address is not deallocated.