Chapter 8
Now that you understand reference counting, you can understand why cir 
cular references cause memory leaks. Consider this example:
{
  my $a;    # $a starts with a refcount of 1
  my $b;    # $b starts with a refcount of 1
  $a = \$b; # $b now has a refcount of 2
  $b = \$a; # $a now has a refcount of 2
}
# both $a and $b go out of scope and have their reference counts
# decremented.  Now they both have refcounts of 1.  Both are still live
# and the garbage collector cannot free them!
Inspecting Reference Counts
You can inspect the reference count of an object from C using the SvREFCNT macro:
SV *sv = newSV();
SV *rv;
printf("%d\n", SvREFCNT(sv)); // prints 1
rv = newRV_inc(sv);           // create a reference to sv
printf("%d\n", SvREFCNT(sv)); // prints 2
As you can see in this example, newRV_inc() increments the reference count of the 
target object as it creates the new RV. If this isn't what you want, then you can use 
newRV_noinc(), which creates a reference and doesn't increment the reference 
count. If you create a new SV and then attach a reference to it with newRV_noinc(),when 
the reference is freed the SV will be too. This is such a cozy setup that the sv_setref
functions use this process to introduce earlier work. The result is that you can forget 
about the original SV and only worry about freeing the RV.
The same procedure works with AVs and HVs they can even use the same 
macros and functions through the magic of polymorphism:
SV *hv = newHV();
SV *rv;
printf("%d\n", SvREFCNT((SV *) hv)); // prints 1
rv = newRV_inc(hv);                  // create a reference to hv
printf("%d\n", SvREFCNT((SV *) hv)); // prints 2
194






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

web hosting perl

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved