[Java-gnome-developer] Re: Use of destroy()
Brought to you by:
afcowie
From: Laurent M. <la...@ao...> - 2004-10-30 15:33:59
|
>>>>> "'Mark" == 'Mark Howard' <mh...@ti...> writes: Mark> On Sat, Oct 30, 2004 at 02:23:34PM +0200, Laurent Martelli Mark> 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. Mark> I agree in this patricular case. But what about when we Mark> create a window, hide it and then the java object goes out of Mark> scope. The c objects are still three and we have no way of Mark> accessing them. This is a memory leak. Unless we unref in finalize(). >> 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. Mark> I thought there already was a bug about this. Is it http://bugzilla.gnome.org/show_bug.cgi?id=155075 ? It's not only a glade problem. Bin.getChild() is also affected. Mark> Not sure. I agree, libglade.getWidget should do an appropriate Mark> 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. Mark> When we add event handlers, we create references from the c Mark> object to the java object. I think we would almost always get Mark> the unfortunate case where the c object and java object Mark> reference each other, but nothing else references Mark> them. i.e. a memory leak I had not thought of that. But the GC is supposed to also work in the case of circular references. Unless the the reference create in C is a root object for the GC. But it would seem normal that the programmer must unregister from events when a widget is not used anymore and is meant to be garbaged. Mark> WeakReferences might seem the obvious answer, That's what I was about to say :-) Mark> but it isn't :-( Mark> -- in this case we would always have to keep references to any Mark> java objects which we have defined events in. I must admit I don't see the point. To me, weak refs are really a solution. I guess I do not see the whole picture. Mark> What we really need is a weak reference which looks at both Mark> java references and g_object_refs. It might be possible for us Mark> 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). Mark> This shouldn't happen. If gtk does not do this, then we can assume it can't happen if we remove destroy() from the java API. -- Laurent Martelli la...@ao... Java Aspect Components http://www.aopsys.com/ http://jac.objectweb.org |