ID:1981273
 
(See the best response by Konlet.)
So, if an atom has no references, it gets garbage collected, which is fine.

I'm creating an object pooling library. If the pool is bigger than the poolTrimSize, it starts removing from the end of the pool, then calls
spawn(poolDeleteTimer)del object
. Will that line count as a reference and prevent garbage collecting, assuming there are no other references to the object?
No, but after the spawn and if the proc/verb isn't attached to the mob it could cause problems.
In response to Kozuma3
Kozuma3 wrote:
No, but after the spawn and if the proc/verb isn't attached to the mob it could cause problems.

So I should be doing spawn(poolDeleteTimer)if(object)del object
Also, to get even more in-depth

var/list/pools = new/list()
pools["A"] = new/list()
pools["A"] += object

pools -= "A"
//Does the list "A" stop existing? What happens to everything inside of "A"?
//Does it lose the list as a reference?
//Do the elements within the list count as references to the list?
The reason why I'm asking this is because I'm wondering how lists interact with garbage collection. If an object is in a list, but the list has no references to it, does the object count as a reference to the list?

If I removed all the references to a list filled with objects, then did spawn(time)if(object)del object on each object, would the list be garbage collected, and then assuming there are no references to the objects, the objects get garbage collected?

If the objects count as a reference to the list, the list won't get GC'd, so the object's won't get GC'd, so therefor I'd have to Del them, which I don't want, meaning I'd have to for(object in list)list-=object, which is a solid workaround, but isn't particularly what I desire to do since the list could in theory be huge.
In response to Rushnut
Best response
Both "A" and the value associated with it are gc'd in this case, unless the associated value is referenced elsewhere. The length of the list shortens. Elements within the list do not count as references to the list. If a value in the list is an atom referenced somewhere else, and the list is deleted, the atom will not be deleted; however, this case also will not bring the list "back to life".
The spawn() will create a new reference to src, and also will create new references to any local vars in the proc, which get copied. If object is a local var or an argument to the proc, it'll get a new reference.

I wouldn't call del() in this case, as it's nowhere near as clean as letting all the refs go. Having a spawn() for each object in the pool (as the above code suggests) is also highly inefficient. I'd have to see the full object pool code to really understand what you're doing, though.
In response to Lummox JR
It's intended for a library, this one to be precise.

The issue is that I can't rely on users not having references to something they've pooled. If they've pooled it, they're effectively saying "I no longer need this", which is akin to saying "I have no references to this", but I can't rely on that being the case.

My current workaround (Haven't updated the lib to reflect this) is to return the pool's object list when you delete a pool, with which you can then manually handle, although ideally it'll just fizzle out when the stack ends and get garbage collected. At least this way users can make sure it is deleted if for some reason it isn't garbage collected.
To work around this, use \ref and locate

You should also mark the object so that if it get's gc'ed and the ref is reused, you know.

/datum/var/gcchecktime = 0

/proc/gcbutcheck(var/datum/item)
if (!istype(item))
del(item)
return
item.gcchecktime = world.time
var/timecheck = world.time
var/itemref = "\ref[item]"
var/item = null
spawn (DeleteTimer)
var/thing = locate(itemref)
if (!thing || thing.gcchecktime != timecheck)
return
del(thing)


To see a more fleshed out version of this, checkout https://github.com/tgstation/-tg-station/blob/ bea187ef8d7bac55640d08fbb55dda8feda0bd04/code/controllers/ subsystem/garbage.dm

All of our deletes at /TG/station13 use such a system to allow for GCs normally but ensure the item gets deleted if something has a hanging reference.
The downside of using \ref this way of course is that you end up with more strings in the string tree. But there aren't a lot of perfect solutions there.