Maptitude GISDK Help

Weak References

In GISDK, arrays and objects can hold references to other arrays or objects. In order to manage the lifetime of these values, the platform maintains a reference count for each value which is incremented while other object that references it in this way.

 

This can lead to a problem known as "circular references" where 2 values each hold a reference to each other, and so neither will ever be released causing a memory leak. For example:

 

class "Node" (parent)

   init do

      self.Parent = parent

      endItem

 

   Macro "AddChild" do

      child = CreateObject("Node", self)

      self.Children = self.Children + {child}

      endItem

endClass

 

parent = CreateObject("Node")

parent.AddChild()

 

Now parent holds a reference to a child node and the child node holds a reference to parent. Neither will ever be freed unless some code intervenes to null out at least one of the references

 

In order to make it easier to avoid this problem, GISDK now has the ability to declare a "weak reference" to other values. This means that the reference count on the value being referred to is not incremented. Once all other references to that value are released then the value of any variable still holding a weak reference will be set to null.

 

A weak reference is declared by using the operator "<=" for assignment instead of the standard "=" operator. So, to update the above example:

 

class "Node" (parent)

   init do

      self.Parent <= parent     //a weak reference to the parent

      endItem

 

   Macro "AddChild" do

      child = CreateObject("Node", self)

      self.Children = self.Children + {child}

      endItem

endClass

 

parent = CreateObject("Node")

parent.AddChild()

 

Now there is no memory leak. Once the parent variable goes out of scope it has a reference count of zero and so it will be released, which then releases all its child nodes as well.

 

 

©2025 Caliper Corporation www.caliper.com