[Java-gnome-developer] Re: Use of destroy()
Brought to you by:
afcowie
From: 'Mark H. <mh...@ti...> - 2004-10-30 15:00:36
|
On Sat, Oct 30, 2004 at 02:23:34PM +0200, Laurent Martelli wrote: > Mark> - Java objects are GCd when they go out of scope, unless > Mark> events have been registered. Therefore, c objects can exist > Mark> without corresponding java objects. > > I believe this not a problem, and anyway there's probably nothing that > can be done against it since some gtk objects may create ither gtk > objects on their own. It's all right as long they destroy them when > they are destroyed themselves. I agree in this patricular case. But what about when we create a window, hide it and then the java object goes out of scope. The c objects are still three and we have no way of accessing them. This is a memory leak. > By the way, when we want to get a reference on such an object from > java, we seem to always get an instance of org.gnu.gtk.Widget. It > would be nice to get the actual instance so that more useful things > can be done with the widget. I'll fill a bug report against that. I thought there already was a bug about this. Not sure. I agree, libglade.getWidget should do an appropriate cast. I'm not sure how simple that would be though. > What about the finalize method which is called when a java object is > garbage collected ? I think should call g_object_ref() when a java > object is created so that we are pretty the C object won't be > destroyed as long as the java object exists, and g_object_unref() in > finalize(). This way, a C object would destroyed when no java objects > references it and it's not referenced by a any C object either. When we add event handlers, we create references from the c object to the java object. I think we would almost always get the unfortunate case where the c object and java object reference each other, but nothing else references them. i.e. a memory leak WeakReferences might seem the obvious answer, but it isn't -- in this case we would always have to keep references to any java objects which we have defined events in. What we really need is a weak reference which looks at both java references and g_object_refs. It might be possible for us to create this. > This only problem would be if a C object is manually destroyed while > there are still references on it (ref_count>0). This shouldn't happen. -- .''`. Mark Howard : :' : `. `' http://www.tildemh.com `- mh...@de... | mh...@ti... |