Another memory question

BlitzMax Forums/BlitzMax Beginners Area/Another memory question

Brucey(Posted 2007) [#1]
I have a Type which at some point creates some memory using MemAlloc().

Without adding a special "FreeMyType()" kind of method, am I safe to put into a Delete() method something like this:

Method Delete()
    If myBytePointer Then
        MemFree(myBytePointer)
    End If
End Method

I realise that I don't know "when" the Object will be deleted, but am I right to assume that when the GC decides it is time for it to go, Delete() is called and I can free the allocated memory that way?

:o)

noob


Chris C(Posted 2007) [#2]
NooB my a**s !! ;)

I've done this myself and it seemed ok, is there any reason you think it might not be ok?

the only thing I can think is if you had some cyclic dependency between types that meant the GC never freed an instance...


QuietBloke(Posted 2007) [#3]
well... a few months ago I had an issue where I implemented Delete() and found it wasnt always getting called.

I cant seemt to find the thread but basically I was told that while a program is running then the garbage collector will call the Delete() method as it deletes objects.
When the programs ends then any remaining active objects and any that the garbage collector has not yet collected will just get deleted and the Delete() method for these objects is not called.

So... I guess in some cases when you program ends it might not actually free the memory you malloc'd.

Of course I could be wrong... I often am


Brucey(Posted 2007) [#4]
Hmmm... that's interesting.

But you'd think that *any* memory you malloc during the run will be freed when the program ends. (Can't speak for windows, but it should be like that on the *nix OSs)

I guess I'll just hope it all works... ;-)


tonyg(Posted 2007) [#5]
I remember Mark commenting that delete() is called when GC is run so, if the program ends, might not be actioned. Not an issue for memory and objects as they're freed at the end of the program anyway but if you put 'Bye bye' messages they might not get displayed.
Generally, I leave debug messages in my delete() methods to be sure they're run.

<EDIT> P.S. You *do* have to be careful that gccollect() is ever actually run. It runs, by default, every n thousand (5000?) object removals so you might want to throw a gccollect() in your program.
<edit2> A quick search and Budman reckons GC runs every 501st object is *created*.
<edit3> This *might* help
Type ttest
	Field x
	Function Create:ttest()
		Local temp:ttest = New ttest
		Return temp
	End Function
	Method New()
		Print "CREATED"
	End Method
	Method Delete()
		Print "DELETED"
		CHECKER=True
	End Method
End Type
Global checker:Int=False
Local mytest:ttest = ttest.create()
For Local x:Int = 0 To 1000
	Print x + " running"
	Local temptest:ttest=ttest.create()
	If x = 400 mytest = Null
Next



Brucey(Posted 2007) [#6]
Thanks tony.

Nice example :-)

Seems that it does get Deleted at some point, which is just what you want.
As for *when* it happens. I'm not too concerned. It'll be up to the user of the Type to release it when they are done (and call gccollect if they want it sooner) with it anyway, after which it looks like I can rely on Max calling the Objects Delete() method - and subsequently allowing the object to free the memory it allocated.

I'm usually not one to use MemAlloc/MemFree/etc, but I'm finding it easier than having to do more code in C.

Thanks for the info, everyone :-)