Re: [Urgent :-)] How you cancel the effect of upvar?

From: Schofield (bschofield_at_users.sf.net)
Date: 07/21/04


Date: Wed, 21 Jul 2004 04:25:13 GMT

George Petasis wrote:

> Hi all,
>
> My problem involves upvar & itcl.
> I have two itcl objects that belong to the same (or inherited class).
> In the class definition I have an array, that I want to be common
> between the two objects. As a result, I create the object A, but in the
> constructor of B I use an upvar to make the two tables equivalent:
>
> class test {
> variable _array
> constructor {mirror_obj} {
> if {[string length $mirror_obj]} {
> upvar "@itcl $mirror_obj _array" \
> [itcl::scope _array]
> } else {
> ## Initialise _array
> }
> }
> }

What are you trying to do? You should never exploit that "@itcl obj
variable" loop hole. From the looks of it, you seem to be wanting to
initialize an array, in the constructor. Maybe this is a copy
constructor? Actually, you seem to be trying to share an array from
multiple objects...

If you are trying to have data common to all instances, use "common"
instead of "variable".

class test {
     common _array
     constructor {} {
        if {![info exists _array]} { initArray }
     }
}

If you are wanting to do some sort of copy construction:
  variable _array
  constructor {obj} {
     array set _array [$obj getArray]
  }
  method getArray {} {return [array get _array]}

If you want to share the *same* data between to some instances but not
all, then consider some other scheme. Linking an member of one object to
appear be a member of another object has trouble written all over it.
Who actually owns it? Who will delete it, or otherwise maintain it. You
may need something like a data manager class that actually maintains
your arrays and registers objects that use it. So that the array can be
deleted when no object is referencing it. This of course would require
objects to register during construction and unregister during
destruction. But this will probably place the burdon of modifying the
array in the data manager object itself

   dataMgr setvalue $this key value
   dataMgr getvalue $this key

or something simular. Upcoming dictionaries might simplify this problem.

>
> However, this leads to random crashes when the objects get deleted,
> and the debugger points almost always to the deletion of the _array
> variable.
No doubt and no suprise. You have multple object deleting the same
variables.

  For now, I am using unset to unset the _array variable in the
> constructor, but I am not sure that this is the solution. (Itcl
> internals also try to unset the variable from C).

>
> How is supposed such a case to be handled? Can I undo the effect of
> upvar? Shouldn't itcl handle such a case automatically?
>
> George
>

-- bryan



Relevant Pages

  • Re: [Urgent :-)] How you cancel the effect of upvar?
    ... > My problem involves upvar & itcl. ... > I have two itcl objects that belong to the same. ... > In the class definition I have an array, that I want to be common ... I am using unset to unset the _array variable in the ...
    (comp.lang.tcl)
  • [Urgent :-)] How you cancel the effect of upvar?
    ... My problem involves upvar & itcl. ... I have two itcl objects that belong to the same. ... In the class definition I have an array, that I want to be common ... I am using unset to unset the _array variable in the ...
    (comp.lang.tcl)
  • [Urgent :-)] How to cancel the effect of upvar?
    ... My problem involves itcl & upvar. ... because I want the same array to be shared between two objects. ...
    (comp.lang.tcl)
  • Re: Bulk Array Element Allocation, is it faster?
    ... that Bla() gets a number. ... In the bulk initialization, bla.seqno is 43, always. ... If the constructor writes a message to System.out, ... What there is no way to make a "new" of an array ...
    (comp.lang.java.programmer)
  • Re: Sorting records using sort()
    ... > We have an array of n*m bytes. ... > proxy objects store a pointer to some master descriptor that stores the ... > need a default constructor, the value type needs to be assignable only ...
    (comp.lang.cpp)