Object handle and reference

BlitzMax Forums/BlitzMax Programming/Object handle and reference

zambani(Posted 2009) [#1]
Hi everyone. 2 Questions
1)What's the difference between an Object handle and reference.

2)What's the best way to remove an image(Timage/pixmap) or sound from memory.

It seems like nulling the image is not the way to do it.

Example:
pix = loadimage("picture.jpg")
pix2=pix

How do i totally remove picture.jpg from memory?

Thanks for all your help.


GfK(Posted 2009) [#2]
Don't know the first answer.

2)What's the best way to remove an image(Timage/pixmap) or sound from memory.

It seems like nulling the image is not the way to do it.

Example:
pix = loadimage("picture.jpg")
pix2=pix

How do i totally remove picture.jpg from memory?
Nulling is exactly the way to do it.

In the example above, you have two references to the same image. You must null them both, after which the garbage collector will clean up and the image will be no more.


zambani(Posted 2009) [#3]
Oh, ok
I thought you had to do some additional work to really get rid of it from memory. Does that also work for Tsound.


Gabriel(Posted 2009) [#4]
1. Semantics. People use them interchangeably around here, so I would urge caution when assuming what people mean but generally speaking, a handle means an integer index number which refers to an object, whereas a reference would be an actual object, passed by reference.

Therefore:

Pix=LoadImage("picture.jpg")


Is an integer handle, because you don't specify a datatype, so you must not be using the Strict or SuperStrict compiler directives. Tip: Use SuperStrict

A reference would be

Local Pix:TImage=TImage.Load("picture.jpg")


2. I don't really understand question 2. You're just declaring a new variable and storing the handle there too. That's not nulling anything - quite the opposite - so I'm not sure why you say that nulling the variable is not the way to go.

In general, if you haven't stored the image anywhere else, you don't need to do anything. When pix and pix2 go out of scope the image will be collected. You can null if you want to, but there's no need. If you store objects in lists or whatever then you need to remove them from that location.


zambani(Posted 2009) [#5]
@Gabriel. Agree about superstrict. I only use superstrict. Was just lazy to type the type.
I may have gotten confused by the use of RELEASE that I thought is required to remove integer handles.

@Gabriel and @GfK
Thanks a lot for clarify this up for me.


N(Posted 2009) [#6]
I may have gotten confused by the use of RELEASE that I thought is required to remove integer handles.
Release is required to remove integer handles. Otherwise, any object that has an integer handle will exist forever.