On Sat, Mar 05, 2005 at 11:01:12AM -0500, muppet wrote: > > On Mar 5, 2005, at 9:29 AM, Steven N. Hirsch wrote:
> >I'm confused as to whether I get comprehensive management for "free" > >in the XS world, or whether the burden is on me to iterate into > >complex data structures. > > If you create your complex structures correctly, yes, it's free. "correctly" meaning that you get the reference counts correct. For example, if you make a hash of array (references), then if the hash owns the only reference on each SvRV it contains, and in turn each SvRV owns the only reference on the array it points to, and in turn the array owns the only reference on the scalars it contains, and nothing outside that structure retains a direct pointer to anything in it, then it's correct. Decreasing the reference count of the hash to zero will cause perl to iterate over all the values of the hash, decreasing their reference count by one. As just described, for each SvRV that drops its reference count to zero, so that causes their referent to have its reference count decreased by one. And for the structure described, that was the last reference count, so in turn perl will iterate over the array, decreasing the reference count of each value held. To get things correct the API documentation for the various functions (such as storing a scalar in a hash) explains whether passing them that scalar causes them to increment its reference count by one, or to leave the count the same, but take ownership of a references. The latter means that you, the caller, donate them a reference. IIRC all of the hash and array store functions leave the reference count unchanged, so you donate them a reference, with creating a scalar reference the odd one out, with the "default" version incrementing the reference count of the referent by one. As things are created with one reference, this means that if you do nothing but create scalars and then store them in hashes or arrays, you don't need to do anything further to get the reference counts correct. Nicholas Clark