Re: set_weak_flag() for objects?

Arne Goedeke <[email protected]>
Newsgroups gmane.comp.lang.pike.user
Message-ID <[email protected]>
Weak references are resolved (i.e. objects which only have weak
references are destructed) only if 1) the gc runs or 2) when a mapping
is rehashed. This means that in your example the object is only
destructed when the gc runs. In your situation I would recommend doing
something like this:

class FOO {
	private mixed _id;
	
	mixed `id() {
		return _id || this;
	}
}

Hope that helps.

Arne

On 09/21/16 12:14, Stephen R. van den Berg wrote:
> Tried some more, not even the workarounds work as expected:
> 
> class NoRef {
>   void create() {
>     werror("Created NoRef\n");
>   }
> 
>   void destroy() {
>     werror("Destructed NoRef\n");
>   }
> }
> 
> class Ref {
>   private mixed id;
> 
>   void create() {
>     id = this;
>     werror("Created Ref\n");
>   }
> 
>   void destroy() {
>     werror("Destructed Ref\n");
>   }
> }
> 
> class WRef {
>   private array id;
> 
>   void create() {
>     id = ({0});
>     set_weak_flag(id, Pike.WEAK);
>     id[0] = this;
>     set_weak_flag(id, Pike.WEAK);
>     werror("Created WRef %O\n", get_weak_flag(id));
>   }
> 
>   void destroy() {
>     werror("Destructed WRef\n");
>   }
> }
> 
> int main(int argc, array(string) argv) { 
>   object a;
>   a = WRef(); a = 0;
>   a = NoRef(); a = 0;
>   a = Ref(); a = 0;
>   werror("Finished\n");
>   return 0;
> }
> 
> Any ideas?
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.