Values in TMaps - Ref Counted?

BlitzMax Forums/BlitzMax Programming/Values in TMaps - Ref Counted?

Gabriel(Posted 2007) [#1]
I have recently rewritten my texture management system, and I hit a snag with reference counting, but decided that I would carry on and fix that later. However, when I ran it, it appears to be working, and it really shouldn't work unless Values in TMaps are not reference counted. I could just leave it alone since it works, but then it might turn out to be a little BMax bug that gets fixed, so I really ought to figure out what's wrong ( or right :/ ) and rewrite it properly. Values in TMaps really should be reference counted, right? Because otherwise, an object which only existed in a TMap would be collected and then everything would go wrong.


H&K(Posted 2007) [#2]
Yes, a link to an object should count as a refference, irrelivant of its a direct name or a list or map member.

So I wonder why it works as well.

(BAsicaly its refferanced somewhere, so it has a refference)
(But hahaha, I am just assuming as well, so lets wait and see what those clever people say)


Gabriel(Posted 2007) [#3]
And there are no known bugs with the GC and TMaps?


Mark Tiffany(Posted 2007) [#4]
As a TMap holds the key and value as objects, I too would expect them to be ref counted.

The only thing I can think of is that you might be replacing a given key value pair with a new version. i.e. you are storing object a with a key of 1, then you store object b with a key of 1. object a then loses its reference and is collected.


Gabriel(Posted 2007) [#5]
Hmm, good point, I'll check on that. Thanks Mark.


H&K(Posted 2007) [#6]
I havent started to use Tmaps yet, cos it was introduced too late in dev cycle to be worth it, but I dont recal anyone else claiming to have a problem with it. And I assume that most ppl who are using it are keeping thier only references within the map.

Ill go with Mark ATM, and say you are actualy unrefferenceing somewhere.


Dreamora(Posted 2007) [#7]
TMaps hold 2 object references.
So yes the raise the object reference count
There is nothing that should free anything mapped or my texture manager would be quite bogus as well :) (so far the pixmaps etc still were there when I looked for them)

The only thing that I could think of causing such stuff is when you believe in the myth that using a byte ptr to an object is a reference as well because it is not ...
In that case you could actually get into trouble.


Gabriel(Posted 2007) [#8]
The only thing that I could think of causing such stuff is when you believe in the myth that using a byte ptr to an object is a reference as well because it is not ...
In that case you could actually get into trouble.

Ah, you read ahead. That's my newly acquired plan to fake weak references. So no, I'm counting on that to NOT be a reference ;)

I'm definitely not explicitly dereferencing anything, so hopefully Mark is right and I'm re-inserting, which amounts to the same thing. Can't find it yet, but I'm hopeful that might be it.

EDIT: Nope, couldn't find it, so I'm going to rip it out and write it how I originally intended, but from scratch this time. Hopefully whatever went "wrongly-right" last time won't happen again and I won't have to worry about why.


bradford6(Posted 2007) [#9]
please excuse the unruly code here but when you null out the Object the Tmap seems to get cleared. not sure if htis answers your Q but hey, it's late & I'm tired :)


Type Tmemtest
	Field FooMap:Tmap = CreateMap()
	Method New()
		For x = 1 To 10
			Local k:Tkey = New Tkey
			k.n = x
			MapInsert(FooMap , k , New Tfoo )
		Next

	End Method
End Type


Type Tkey
	Field n:Int
End Type

Type Tfoo
	Field big:Int[100]	
	Method New()
		For x = 0 To Len(big)-1
			big[x] = x
		Next
	End Method
End Type

Local test:TList = CreateList()



Print "done creating"

Graphics 640,480
Repeat
	
	Local mem:Int = GCMemAlloced()
	SetColor 0,0,255
	DrawText "GCMemAlloced()="+mem, 0,0
	DrawText " UP to add, DOWN to remove",0,20
	DrawText CountList(test),0,40
	
	SetColor 255,255,0
	DrawRect (0,197,(mem*0.001)+6 ,46)

	SetColor 255,0,0
	DrawRect (3,200,mem*0.001 ,40)
	
	If KeyDown(KEY_UP)
		ListAddLast test, New Tmemtest
	EndIf
	
	If KeyDown(KEY_DOWN)
		If Not test.isEmpty()
			Local a:Tmemtest = Tmemtest(test.last())
			' ClearMap(a.FooMap)   ' <<<<<<<< <<<< < < <  UNREM this line to test
			ListRemove test , a
			a = Null
		EndIf
	EndIf
	Flip
	Cls
Until AppTerminate() Or KeyDown(KEY_ESCAPE)