The case of the missing a

BlitzMax Forums/BlitzMax Programming/The case of the missing a

Czar Flavius(Posted 2010) [#1]
Strict

Type a
	Method New()
		DebugLog "im here"
	End Method

	Method Delete()
		DebugLog "im gone"
	End Method
End Type

For Local i = 0 To 1
	DebugLog i
	gah()
Next

GCCollect()

Function gah()
	Local test:a = New a
	test = Null
End Function


DebugLog:0
DebugLog:im here
DebugLog:1
DebugLog:im here
DebugLog:im gone



Floyd(Posted 2010) [#2]
If you add another field which identifies the object you will see that final "im gone" is really the first object, the one called 0.

After setting test = Null the garbage collector can reclaim that memory whenever it wants to. It got around to doing the first one. But apparently the second one is not needed because the program ends and all memory is returned to the OS.

At least that's what I would guess is the explanation.


TaskMaster(Posted 2010) [#3]
You can never be sure delete will get called on any objects after you end your program. No guarantees.


plash(Posted 2010) [#4]
If you need to have something happen every time an object is to be considered dead, it is best to do that with a manual call - obviously relying on the GC is not a good idea.


Czar Flavius(Posted 2010) [#5]
But I read in another post that GCCollect before End will still collect everything.


markcw(Posted 2010) [#6]
I always get 2 'gones'. Similarly whatever the loop count the 'gones' are always the right amount. I'm in Linux btw, are you in Windows?


plash(Posted 2010) [#7]
I always get 2 'gones'. Similarly whatever the loop count the 'gones' are always the right amount. I'm in Linux btw, are you in Windows?
I don't get two 'gones' here on Linux.

The last one is never destroyed for me:



Czar Flavius(Posted 2010) [#8]
Two GCCollect() at the end of the program will destroy the last object.


plash(Posted 2010) [#9]
Two GCCollect() at the end of the program will destroy the last object.
Not always.

EDIT: Again, if you want something done when an object is to be considered dead, you must do it yourself.


Czar Flavius(Posted 2010) [#10]
I'm doing it to make sure destroyed objects are being deleted and not retained, due to circular reference etc.


markcw(Posted 2010) [#11]
Plash: how many 'destroyed' do you get now? When I remove MakeObject's parameter it goes from 2 to 3. Strange.




plash(Posted 2010) [#12]
Plash: how many 'destroyed' do you get now? When I remove MakeObject's parameter it goes from 2 to 3. Strange.
Still two.


Zeke(Posted 2010) [#13]
add small delay before GCCollect:

and all 3 are destroyed.

but in threaded build this works without delay...


markcw(Posted 2010) [#14]
Could be my system is slower and I don't need a delay. I'm using 1.40 on a dell 1525.


Floyd(Posted 2010) [#15]
The lesson is still that abandoned objects may or may not get garbage collected before the program ends. If there is any important clean-up that must be done then you should do it yourself. Don't rely on the garbage collector to do it automatically via Delete.


Kryzon(Posted 2010) [#16]
Local test:A = New A.Create()

Isn't this wrong? shouldn't it be either:

Local test:A = A.Create() 'With the Create returning a new instance and printing stuff.
or...
Local test:A = new A  'Using the New constructor to print the stuff.



Yan(Posted 2010) [#17]
Local test:A = New A.Create()

Isn't this wrong?...
No. In this case, Create() is a method not a function.


H&K(Posted 2010) [#18]
Isnt the Debug log just being turned off before the last object is deleted?


plash(Posted 2010) [#19]
Isnt the Debug log just being turned off before the last object is deleted?
I don't think the debug log can be turned off.

This thread is turning towards silly drivel - the point has been made already people.


markcw(Posted 2010) [#20]
Isnt the Debug log just being turned off before the last object is deleted?

No. Czar and Plash have supercomputers so the GC doesn't get enough time to delete the last object.


Czar Flavius(Posted 2010) [#21]
Enough time from where? Even with massive delays before and after the last one doesn't get deleted.


markcw(Posted 2010) [#22]
My mistake then. See post #15.


Czar Flavius(Posted 2010) [#23]
Interesting! Putting
OnEnd GCCollect
solves the problem, even with only a single GCCollect added.