Re: [Vala] use weak and var together

2009-01-11 Thread Jürg Billeter
On Sat, 2009-01-10 at 23:48 +0100, Hans Vercammen wrote:
 Why do this manually? Don't you need to chain the dispose handlers
 throughout the hierarchy to make it usable somehow?
 As far as I understand the GObject dispose mechanism, it doesn't prevent
 the cyclic references from not being cleaned up. But it allows you to
 manually break them if they exist.
 Perhaps it would be a good idea to dispose the member references in the
 GObject dispose handler by default instead of the finalize handler. In
 this case the user could build a custom object management system on top
 of vala and call the dispose handler explicitly when required. Of course
 this could cause various side-effects when not used carefully. On the
 other hand so do raw pointers and preventing these memory leaks is as
 far as know only possible by using weak references.

The issue is that freeing everything already in dispose is not always
correct, it could lead to crashes if referenced objects access other
objects while being disposed.

However, dispose is available as a virtual method in Vala and overriding
it should provide you with the means necessary to support the GObject
dispose mechanism. When you make sure that there are no cycles by using
weak references, this is not necessary.

Jürg

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Jürg Billeter
On Sat, 2009-01-10 at 01:04 -0500, Yu Feng wrote:
 On Fri, 2008-12-12 at 08:08 +0100, Jürg Billeter wrote:
  On Thu, 2008-12-11 at 23:56 -0500, Yu Feng wrote:
   Talking about circular references, is it possible to have a
 circular ref
   breaker mechanism like the one in GTK for vala fundamental classes?
  
  I don't think we should always add a dispose mechanism to fundamental
  classes. However, if someone wants to use that, it should still be
  possible to implement this with a bit of extra Vala code.
  
 
 I suddenly realized it is impossible to write dispose mechanism with
 extra vala code because one can not override the hidden unref method,
 therefore one can not do the cycle detection/breaking at the right time.

unref methods are never virtual in GObject. However, you can override
the dispose method in Vala. Is this what you meant or can you explain
what exactly you want to achieve?

Jürg

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Yu Feng

On Sat, 2009-01-10 at 09:24 +0100, Jürg Billeter wrote:
 On Sat, 2009-01-10 at 01:04 -0500, Yu Feng wrote:
  On Fri, 2008-12-12 at 08:08 +0100, Jürg Billeter wrote:
   On Thu, 2008-12-11 at 23:56 -0500, Yu Feng wrote:
Talking about circular references, is it possible to have a
  circular ref
breaker mechanism like the one in GTK for vala fundamental classes?
   
   I don't think we should always add a dispose mechanism to fundamental
   classes. However, if someone wants to use that, it should still be
   possible to implement this with a bit of extra Vala code.
   
  
  I suddenly realized it is impossible to write dispose mechanism with
  extra vala code because one can not override the hidden unref method,
  therefore one can not do the cycle detection/breaking at the right time.
 
 unref methods are never virtual in GObject. However, you can override
 the dispose method in Vala. Is this what you meant or can you explain
 what exactly you want to achieve?
 

No. We were talking about GTypeInstance fundamental classes in that
mail. They don't have a 'dispose' method to be overriden.


 Jürg
 

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Yu Feng
On Sat, 2009-01-10 at 19:36 +0100, Jürg Billeter wrote:
 On Sat, 2009-01-10 at 13:07 -0500, Yu Feng wrote:
  On Sat, 2009-01-10 at 18:35 +0100, Jürg Billeter wrote:
   Right, but I'm not sure that you need to change anything in the unref
   function to get dispose functionality working. Can you explain more
   specifically what you can't get working with the current possibilities?
   
  
  Yes I can do this:
  
  class Object {
 Object ref_to_there;
 Object ref_to_here;
 private disposed = false;
 public virtual void dispose() {
   /*release references here*/
ref_to_there = null;
ref_to_here = null;
 }
 private void run_dispose() {
if(disposed) return;
disposed = true;
dispose();
 }
 public void destroy() {
Object holder = this; 
/*needed if invoked from a weak reference of this*/
run_dispose();
holder = null;
 }
  }
  
  Then Object.destroy can be used to manifestly break any cycle
  references.
  
  I was thinking about auto-cycle-breaking but it is unfortunately beyond
  my knowledge.
 
 Unfortunately, there is no magic trick to break reference cycles
 automatically in a simple way. In general, this requires an additional
 garbage or cycle collector which scans stack and heap.
 
Is it basically to maintain a list of all pointers to the object? With
some extension to vala compiler at AssignmentBinding perhaps we can have
this list at a relatively cheap cost without scanning stacks and heaps.


 Jürg
 

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Noah Gibbs
  In addition to keeping a list of pointers to the object, you'll need to 
traverse them in some way to find cycles and free storage with cycles, but not 
pointed to by any non-cycles.  That pass should not be done every time you 
break a pointer link because it can be very expensive, just as garbage 
collection is.  You could do it periodically, maybe every so many allocations 
or so many seconds -- but if you do that, you've basically written a garbage 
collector.

  Remember that an object can be part of more than one cycle, and you have to 
know how many of those refs are cyclic refs, and how many (if any) aren't 
from cycles.  That's non-local knowledge.  What I'm saying is that it's not an 
easy think to calculate, and there's a reason that garbage collectors tend to 
be slow :-)

--- On Sat, 1/10/09, Yu Feng rainwood...@gmail.com wrote:

 From: Yu Feng rainwood...@gmail.com
 Subject: Re: [Vala] use weak and var together
 To: Jürg Billeter j...@bitron.ch
 Cc: vala-list@gnome.org
 Date: Saturday, January 10, 2009, 12:45 PM
 On Sat, 2009-01-10 at 19:36 +0100, Jürg Billeter wrote:
  On Sat, 2009-01-10 at 13:07 -0500, Yu Feng wrote:
   On Sat, 2009-01-10 at 18:35 +0100, Jürg Billeter
 wrote:
Right, but I'm not sure that you need to
 change anything in the unref
function to get dispose functionality
 working. Can you explain more
specifically what you can't get working
 with the current possibilities?

   
   Yes I can do this:
   
   class Object {
  Object ref_to_there;
  Object ref_to_here;
  private disposed = false;
  public virtual void dispose() {
/*release references here*/
 ref_to_there = null;
 ref_to_here = null;
  }
  private void run_dispose() {
 if(disposed) return;
 disposed = true;
 dispose();
  }
  public void destroy() {
 Object holder = this; 
 /*needed if invoked from a weak reference
 of this*/
 run_dispose();
 holder = null;
  }
   }
   
   Then Object.destroy can be used to manifestly
 break any cycle
   references.
   
   I was thinking about auto-cycle-breaking but it
 is unfortunately beyond
   my knowledge.
  
  Unfortunately, there is no magic trick to break
 reference cycles
  automatically in a simple way. In general, this
 requires an additional
  garbage or cycle collector which scans stack and heap.
  
 Is it basically to maintain a list of all pointers to the
 object? With
 some extension to vala compiler at AssignmentBinding
 perhaps we can have
 this list at a relatively cheap cost without scanning
 stacks and heaps.
 
 
  Jürg
  
 
 ___
 Vala-list mailing list
 Vala-list@gnome.org
 http://mail.gnome.org/mailman/listinfo/vala-list


  
___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Hans Vercammen
On Sat, 2009-01-10 at 13:07 -0500, Yu Feng wrote:
 On Sat, 2009-01-10 at 18:35 +0100, Jürg Billeter wrote:
  On Sat, 2009-01-10 at 12:19 -0500, Yu Feng wrote:
   On Sat, 2009-01-10 at 09:24 +0100, Jürg Billeter wrote:
On Sat, 2009-01-10 at 01:04 -0500, Yu Feng wrote:
 On Fri, 2008-12-12 at 08:08 +0100, Jürg Billeter wrote:
  On Thu, 2008-12-11 at 23:56 -0500, Yu Feng wrote:
   Talking about circular references, is it possible to have a
 circular ref
   breaker mechanism like the one in GTK for vala fundamental 
   classes?

I'm not very familiar with the inner workings. But don't they explicitly
destroy the objects instead of simply unreffing like vala does?

  
  I don't think we should always add a dispose mechanism to 
  fundamental
  classes. However, if someone wants to use that, it should still be
  possible to implement this with a bit of extra Vala code.
  
 
 I suddenly realized it is impossible to write dispose mechanism with
 extra vala code because one can not override the hidden unref method,
 therefore one can not do the cycle detection/breaking at the right 
 time.

unref methods are never virtual in GObject. However, you can override
the dispose method in Vala. Is this what you meant or can you explain
what exactly you want to achieve?

   
   No. We were talking about GTypeInstance fundamental classes in that
   mail. They don't have a 'dispose' method to be overriden.
  
  Right, but I'm not sure that you need to change anything in the unref
  function to get dispose functionality working. Can you explain more
  specifically what you can't get working with the current possibilities?
  
 
 Yes I can do this:
 
 class Object {
Object ref_to_there;
Object ref_to_here;
private disposed = false;
public virtual void dispose() {
  /*release references here*/
   ref_to_there = null;
   ref_to_here = null;
}
private void run_dispose() {
   if(disposed) return;
   disposed = true;
   dispose();
}
public void destroy() {
   Object holder = this; 
   /*needed if invoked from a weak reference of this*/
   run_dispose();
   holder = null;
}
 }
 
 Then Object.destroy can be used to manifestly break any cycle
 references.

Why do this manually? Don't you need to chain the dispose handlers
throughout the hierarchy to make it usable somehow?
As far as I understand the GObject dispose mechanism, it doesn't prevent
the cyclic references from not being cleaned up. But it allows you to
manually break them if they exist.
Perhaps it would be a good idea to dispose the member references in the
GObject dispose handler by default instead of the finalize handler. In
this case the user could build a custom object management system on top
of vala and call the dispose handler explicitly when required. Of course
this could cause various side-effects when not used carefully. On the
other hand so do raw pointers and preventing these memory leaks is as
far as know only possible by using weak references.

Hans


___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Hans Vercammen
On Sat, 2009-01-10 at 13:16 -0800, Noah Gibbs wrote:
 In addition to keeping a list of pointers to the object, you'll need
 to traverse them in some way to find cycles and free storage with
 cycles, but not pointed to by any non-cycles.  That pass should not be
 done every time you break a pointer link because it can be very
 expensive, just as garbage collection is.  You could do it
 periodically, maybe every so many allocations or so many seconds --
 but if you do that, you've basically written a garbage collector.

I agree. However, cyclic references are basically a trademark for
reference counting. It's relatively fast and easy to implement, but you
probably don't want use this in combination with a garbage collector as
the reference count other than zero is of no meaning to the collector.

   Remember that an object can be part of more than one cycle, and you
 have to know how many of those refs are cyclic refs, and how many
 (if any) aren't from cycles.  That's non-local knowledge.  What I'm
 saying is that it's not an easy think to calculate, and there's a
 reason that garbage collectors tend to be slow :-)

There are actually rather fast garbage collection algorithms out there.
The main reason I can think of why they still appear to be slow is that
in most cases they have to stop the world to do their processing.

Hans

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Hans Vercammen
On Sun, 2009-01-11 at 01:23 +0100, Thomas Chust wrote:
 Hans Vercammen wrote:
  On Sat, 2009-01-10 at 13:16 -0800, Noah Gibbs wrote:
  [...]
  Remember that an object can be part of more than one cycle, and you
  have to know how many of those refs are cyclic refs, and how many
  (if any) aren't from cycles.  That's non-local knowledge.  What I'm
  saying is that it's not an easy think to calculate, and there's a
  reason that garbage collectors tend to be slow :-)
  
  There are actually rather fast garbage collection algorithms out there.
  The main reason I can think of why they still appear to be slow is that
  in most cases they have to stop the world to do their processing.
  [...]
 
 Hello,
 
 there are also real-time garbage collectors that never need to stop the
 program for more than a few microseconds and can run concurrently with
 the main program on a separate thread.
 
 Garbage collection isn't necessarily slow, if it's implemented well. The
 trick is to make the extra work happen concurrently with the main
 program and / or at convenient points in time. Especially if you have a
 lot of memory and can afford to just allocate objects, deferring cleanup
 until the main program is idle, garbage collection is usually faster
 than manual memory management or reference counting which incurs more
 calls to the allocator at inconvenient points in time.

I'm not sure what the actual benefit would be that way. In general I
would agree, but if you mean running the garbage collector in a
different thread; it would imply more synchronization mechanisms, which
would also slow down the process significantly. Not all programs have a
main idle loop or are multi-threaded by default. The main idea of
reference counting is to take care of the object life-cycle immediately
without prosponing it to a different process/thread.

Hans

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Yu Feng
On Sat, 2009-01-10 at 23:48 +0100, Hans Vercammen wrote:
 On Sat, 2009-01-10 at 13:07 -0500, Yu Feng wrote:
  On Sat, 2009-01-10 at 18:35 +0100, Jürg Billeter wrote:
   On Sat, 2009-01-10 at 12:19 -0500, Yu Feng wrote:
On Sat, 2009-01-10 at 09:24 +0100, Jürg Billeter wrote:
 On Sat, 2009-01-10 at 01:04 -0500, Yu Feng wrote:
  On Fri, 2008-12-12 at 08:08 +0100, Jürg Billeter wrote:
   On Thu, 2008-12-11 at 23:56 -0500, Yu Feng wrote:
Talking about circular references, is it possible to have a
  circular ref
breaker mechanism like the one in GTK for vala fundamental 
classes?
 
 I'm not very familiar with the inner workings. But don't they explicitly
 destroy the objects instead of simply unreffing like vala does?
Neither am I. GTKObject has a destroy signal to do the magic with
g_ojbect_run_dispose. Cycle references are automatically resolved. the
relavent code are in gtk/gtkobject.c but I didn't have time to study it
carefully.



 
   
   I don't think we should always add a dispose mechanism to 
   fundamental
   classes. However, if someone wants to use that, it should still be
   possible to implement this with a bit of extra Vala code.
   
  
  I suddenly realized it is impossible to write dispose mechanism with
  extra vala code because one can not override the hidden unref 
  method,
  therefore one can not do the cycle detection/breaking at the right 
  time.
 
 unref methods are never virtual in GObject. However, you can override
 the dispose method in Vala. Is this what you meant or can you explain
 what exactly you want to achieve?
 

No. We were talking about GTypeInstance fundamental classes in that
mail. They don't have a 'dispose' method to be overriden.
   
   Right, but I'm not sure that you need to change anything in the unref
   function to get dispose functionality working. Can you explain more
   specifically what you can't get working with the current possibilities?
   
  
  Yes I can do this:
  
  class Object {
 Object ref_to_there;
 Object ref_to_here;
 private disposed = false;
 public virtual void dispose() {
   /*release references here*/
ref_to_there = null;
ref_to_here = null;
 }
 private void run_dispose() {
if(disposed) return;
disposed = true;
dispose();
 }
 public void destroy() {
Object holder = this; 
/*needed if invoked from a weak reference of this*/
run_dispose();
holder = null;
 }
  }
  
  Then Object.destroy can be used to manifestly break any cycle
  references.
 
 Why do this manually? Don't you need to chain the dispose handlers
 throughout the hierarchy to make it usable somehow?
But this is not a GObject. It's about implementing two stage disposal
and ref breaking with GTypeInstance classes.

 As far as I understand the GObject dispose mechanism, it doesn't prevent
 the cyclic references from not being cleaned up. But it allows you to
 manually break them if they exist.
 Perhaps it would be a good idea to dispose the member references in the
 GObject dispose handler by default instead of the finalize handler. In
 this case the user could build a custom object management system on top
 of vala and call the dispose handler explicitly when required. Of course
 this could cause various side-effects when not used carefully. On the
 other hand so do raw pointers and preventing these memory leaks is as
 far as know only possible by using weak references.
 
 Hans
 
 

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-10 Thread Hans Vercammen
On Sat, 2009-01-10 at 21:12 -0500, Yu Feng wrote:
 On Sat, 2009-01-10 at 23:48 +0100, Hans Vercammen wrote:
  On Sat, 2009-01-10 at 13:07 -0500, Yu Feng wrote:
   On Sat, 2009-01-10 at 18:35 +0100, Jürg Billeter wrote:
On Sat, 2009-01-10 at 12:19 -0500, Yu Feng wrote:
 On Sat, 2009-01-10 at 09:24 +0100, Jürg Billeter wrote:
  On Sat, 2009-01-10 at 01:04 -0500, Yu Feng wrote:
   On Fri, 2008-12-12 at 08:08 +0100, Jürg Billeter wrote:
On Thu, 2008-12-11 at 23:56 -0500, Yu Feng wrote:
 Talking about circular references, is it possible to have a
   circular ref
 breaker mechanism like the one in GTK for vala fundamental 
 classes?
  
  I'm not very familiar with the inner workings. But don't they explicitly
  destroy the objects instead of simply unreffing like vala does?
 Neither am I. GTKObject has a destroy signal to do the magic with
 g_ojbect_run_dispose. Cycle references are automatically resolved. the
 relavent code are in gtk/gtkobject.c but I didn't have time to study it
 carefully.

As far as I can tell they use the gtk_widget_destroy function. Which in
turn explicitly calls g_object_run_dispose.

  

I don't think we should always add a dispose mechanism to 
fundamental
classes. However, if someone wants to use that, it should still 
be
possible to implement this with a bit of extra Vala code.

   
   I suddenly realized it is impossible to write dispose mechanism 
   with
   extra vala code because one can not override the hidden unref 
   method,
   therefore one can not do the cycle detection/breaking at the 
   right time.
  
  unref methods are never virtual in GObject. However, you can 
  override
  the dispose method in Vala. Is this what you meant or can you 
  explain
  what exactly you want to achieve?
  
 
 No. We were talking about GTypeInstance fundamental classes in that
 mail. They don't have a 'dispose' method to be overriden.

Right, but I'm not sure that you need to change anything in the unref
function to get dispose functionality working. Can you explain more
specifically what you can't get working with the current possibilities?

   
   Yes I can do this:
   
   class Object {
  Object ref_to_there;
  Object ref_to_here;
  private disposed = false;
  public virtual void dispose() {
/*release references here*/
 ref_to_there = null;
 ref_to_here = null;
  }
  private void run_dispose() {
 if(disposed) return;
 disposed = true;
 dispose();
  }
  public void destroy() {
 Object holder = this; 
 /*needed if invoked from a weak reference of this*/
 run_dispose();
 holder = null;
  }
   }
   
   Then Object.destroy can be used to manifestly break any cycle
   references.
  
  Why do this manually? Don't you need to chain the dispose handlers
  throughout the hierarchy to make it usable somehow?
 But this is not a GObject. It's about implementing two stage disposal
 and ref breaking with GTypeInstance classes.

Yes, I understand. But if you can't free/break the possible cyclic
references of the private members from the base class, there is
basically little use in providing a virtual/override system for a
dispose method (either GObject or non-GObject). So think providing a
class dispose method for the fundamental classes is a good idea :)
(considering member references would be freed in the dispose method in
both cases).

Hans

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2009-01-09 Thread Yu Feng
Hi Jurg,

Sorry for digging such an old post but I discovered something. See the
end of the message.

On Fri, 2008-12-12 at 08:08 +0100, Jürg Billeter wrote:
 On Thu, 2008-12-11 at 23:56 -0500, Yu Feng wrote:
  On Thu, 2008-12-11 at 22:11 +0100, Jürg Billeter wrote:
   On Wed, 2008-12-03 at 14:05 +, Frédéric Gaudy wrote:
I don't know if it' a bug or a feature. When I use weak and var
keywords, I obtain a error:
error: The type name `var' could not be found

Code : 
weak var thread = Thread.create( this.process_thread, true);
   
   I haven't decided yet whether we should support that and if yes, what
   exact syntax to use. In addition to your example, just
   
 weak thread =...
   
   might also be a possibility. I tend to not support it at all at the
   moment and rather try to minimize the number of situations where 'weak'
   is needed.
  
  I am wondering why you dislike weak so much. It does usually mess stuff
  up but it is also good if one knows what she is doing.
  It not only saves a ref and unref, but also breaks circular references. 
 
 var is only used for local variables and you never need to break
 circular references with local variables. I only dislike it if it's used
 to workaround issues with not reference-counted objects. weak is
 perfectly fine to break circular references, that's the main reason it
 exists.
 
  Talking about circular references, is it possible to have a circular ref
  breaker mechanism like the one in GTK for vala fundamental classes?
 
 I don't think we should always add a dispose mechanism to fundamental
 classes. However, if someone wants to use that, it should still be
 possible to implement this with a bit of extra Vala code.
 

I suddenly realized it is impossible to write dispose mechanism with
extra vala code because one can not override the hidden unref method,
therefore one can not do the cycle detection/breaking at the right time.

Yu
 Jürg
 

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] use weak and var together

2008-12-11 Thread Jürg Billeter
On Thu, 2008-12-11 at 23:56 -0500, Yu Feng wrote:
 On Thu, 2008-12-11 at 22:11 +0100, Jürg Billeter wrote:
  On Wed, 2008-12-03 at 14:05 +, Frédéric Gaudy wrote:
   I don't know if it' a bug or a feature. When I use weak and var
   keywords, I obtain a error:
   error: The type name `var' could not be found
   
   Code : 
   weak var thread = Thread.create( this.process_thread, true);
  
  I haven't decided yet whether we should support that and if yes, what
  exact syntax to use. In addition to your example, just
  
  weak thread =...
  
  might also be a possibility. I tend to not support it at all at the
  moment and rather try to minimize the number of situations where 'weak'
  is needed.
 
 I am wondering why you dislike weak so much. It does usually mess stuff
 up but it is also good if one knows what she is doing.
 It not only saves a ref and unref, but also breaks circular references. 

var is only used for local variables and you never need to break
circular references with local variables. I only dislike it if it's used
to workaround issues with not reference-counted objects. weak is
perfectly fine to break circular references, that's the main reason it
exists.

 Talking about circular references, is it possible to have a circular ref
 breaker mechanism like the one in GTK for vala fundamental classes?

I don't think we should always add a dispose mechanism to fundamental
classes. However, if someone wants to use that, it should still be
possible to implement this with a bit of extra Vala code.

Jürg

___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list