Qbasicnews.com

Full Version: Does this leak?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I noticed a major slowdown in my computer after I ran this code:

Code:
dim stuff as any ptr
screen 13
stuff = imagecreate(100,100)
line stuff, (0,0)-(100,100),54,bf
put (0,0),stuff, pset
stuff = imagecreate(90, 90)
line stuff, (0,0)-(90, 90), 23, bf
put (100,0),stuff, pset
sleep

It may be a coincidence that it slowed down then, or it could be my imagination. I'm just wondering if his will leak memory. I obviously create an orphan in this program, I'm just wondering if FreeBasic's compiler will clean it up.
No, it will leak.. twice..

You must call IMAGEDESTROY

Code:
dim stuff as any ptr
screen 13
stuff = imagecreate(100,100)
line stuff, (0,0)-(100,100),54,bf
put (0,0),stuff, pset
imagedestroy stuff
stuff = imagecreate(90, 90)
line stuff, (0,0)-(90, 90), 23, bf
put (100,0),stuff, pset
imagedestroy stuff
sleep

Although I doubt this little program had anything to do with the slowdown, it doesent even use 100 kb memory
You mean once. Only the first one is orphaned, the other one still has a reference to it.
Technically it leaks twice, in agreement with The Z!re.
Ok, nevermid, the example above would leak twice, ut in my real program I destroy all images that still have references in a cleanup sub.
Quote:ut in my real program I destroy all images that still have references in a cleanup sub.
What about the images that dont have a reference anymore? You showed in your example that you're able to make images without reference... Tongue
Perhaps something like this is what you want?

Code:
Sub SetNewImage ByRef OldImage As Any Ptr, ByVal Width As Integer, Height As Intege
    If OldImage Then ImageDestroy OldImage
    OldImage = ImageCreate(Width, Height)
End Sub

ScreenRes 320, 240, 32

Dim OldImage As Any Ptr
SetNewImage OldImage, 32, 32
SetNewImage OldImage, 64, 64
That's what my original question was about: the ones without references. I just wondered if FreeBASIC had something like java's garbage collector.
Jofers just made it.
Quote:That's what my original question was about: the ones without references. I just wondered if FreeBASIC had something like java's garbage collector.
No. Without objects with destructors, you must still explicitly call ImageDestroy(...) to ultimately free any allocated memory, as Jofers code implies, ie. the code only prevents you from leaking memory when creating a new image with the same pointer. It's still up to you to manually free all allocated resources.
Pages: 1 2