|
|
Warp wrote:
> However, what happens in a GC'd language where you basically *can't*
> implement a reference-counted system like this (which is the case eg.
> with Java)?
You can, of course. You're just reference counting whether the Listener
is "alive" rather than whether the memory is still allocated.
Do all the same stuff, but maintain the reference counter yourself.
(This can, of course, be difficult, if your language doesn't support
automatically doing something when a pointer to an object leaves its scope.)
> This will cause the Engine object to hold onto the references to all
> listener objects even if all the other references to those objects die.
> Thus the object will never by garbage-collected.
It's also the case the Engine will continue to send messages to the
Listener, so you'll see it still behaving after you thought it was gone.
Not a cure, but not hard to figure out what's going on, either.
> The only solution for this would be for the program to explicitly remove
> listening objects from the Engine object when they are not used anymore.
As pointed out, this is part of what "weak references" were created for.
If a GC runs and the only pointers to an object are "weak", then all the
"weak" pointers get set to null, and your Engine can notice that and not
try to use that pointer any more.
> If this is so, then what we have is a memory leak. In a GC'd system.
It's definitely a problem. Some would argue that it's not technically a
memory leak, but rather a failure to disconnect the Listener from the
Engine. It's easier to diagnose (if it's deterministic, at least),
because you see the Engine's list growing indefinitely. You don't lose
memory and not know what's in it.
But yes, if you don't have weak references, you could have this
situation. You can also have the situation where you think you're done
with the object, you tell it to "close" itself (for whatever that
means), and you fail to take it off the Engine's list, which means it
still gets messages after it's no longer wanting to handle them.
--
Darren New / San Diego, CA, USA (PST)
"That's pretty. Where's that?"
"It's the Age of Channelwood."
"We should go there on vacation some time."
Post a reply to this message
|
|