Self reference (this)

Anything that doesn't fit elsewhere.
Post Reply
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Groningen, The Netherlands
Contact:

Self reference (this)

Post by Raj »

Dear Forum,

C++ style coding in hoc would suggest the following possibility for an unref procedure in a template, using the this keyword:

Code: Select all

proc unref(){
				refcount=$1
				if(refcount==1 && context==this){     
					objref context
					mycontextset=0
				}
			}
Is something like this possible? I seem to need something like this to remove self references and I like to keep the responsability with the objects, i.e. preferably not introducing a higher level managing layer.

Regards,
Ronald
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Groningen, The Netherlands
Contact:

Self reference (this)

Post by Raj »

The answer was under my eyes almost while writing my question, a sad thing.

Defining an object reference this, makes the pointer to the object itself available. In code:

Code: Select all

 
begintemplate MyClass
...
objref this
...
endtemplate MyClass
The GUI code is larded with this construction.

If you like to make objects of unknown type on the fly and use them within your class, just do

Code: Select all

strdef tstr1
tstr1="TheFinalClass"
objref tobj

sprint(tstr1, "tobj = new %s()", tstr1)
		execute(tstr1, this)
and your tobj contains a reference to a TheFinalClass object.

Just talking to myself,
Raj
hines
Site Admin
Posts: 1711
Joined: Wed May 18, 2005 3:32 pm

Post by hines »

Getting rid of a group of mutually referencing objects can be complicated since there is no automatic garbage collection. In that case it is useful to check reference counts with allobjects() or allobjectvars(). If those lists are too long or
you wish to see more detailed reference information and know the object, use
StringFunctions.references(object). However
those do not solve the problem they just diagnose it and help one come up with strategies to overcome it. In general one may have to use the undocumented contingent destructor procedure
proc unref() { // arg is the refcount...} in a
template. This special name procedure gets
called whenever the objects refcount is decremented. An example of its use is in
nrn/lib/hoc/celbild/celbild1.hoc where the
CellBuild object is supposed to be destroyed when its refcount is 5. However, in most cases, even the CellBuild case above, the group of mutually referencing objects is not a democratic community but master-worker set where the master references its many workers and the workers reference the master but not each other. In this case it has proved convenient to create a
proc destroy(){...} in the master which
explicitly unreferences (ie. object = nil or
objref object) all the objects it has references
to. This causes the worker object reference counts to go to 0, be destroyed and thus unreference the master. Thus destroying the
entire group is accompished by calling destroy()
on the master and then having the caller ureference the master. An example of the use of this idiom is in nrn/share/lib/hoc/mview .
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Groningen, The Netherlands
Contact:

Post by Raj »

To monitor the references I added some monitoring printf statements to the unref procedure.

In my template object selfreferences can only be created from within the object itself. I kept track of them in the selfrefcount variable at creation time. Myname contains the name of the object obtained using sprint(myname,"%s",this). The list test is a list of objects that can hold a reference to the object in whose context we work and the cause of my unreferencing problem.

Code: Select all

proc unref(){local selfrefcount 
			refcount=$1
							
			if(refcount==selfrefcount && selfrefcount > 0 && unreferencing==0){  
				unreferencing=1
				test.remove_all() 
				printf( "Remove All")
				printf( "ObjectName: %s, Refcount=%d, selfrefcount=%d \n",myname,refcount, selfrefcount)
			}else{
				printf( "ObjectName: %s, Refcount=%d, selfrefcount=%d \n",myname,refcount, selfrefcount)
			}			
		}
When convinced that the references are managed correctly simply remove the printf statements. When things are not working correctly look both at the object and the code you use for testing, because it is so easy to oversee a reference lingering around.
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Groningen, The Netherlands
Contact:

What are the exact usage rules for nil ,this?

Post by Raj »

What are the exact rules for the usage of nil and this?

It seems they accept normal assignments, so in fact nil is not different then any other defined objref reference which has not been assigned an object reference.

this however becomes, once declared, a reference to the object in which it was declared, but after that can also be used as a normal objref.

So apart from initialization of this, nil and this are normal object references?
Post Reply