Programming Perl in C
Explicitly Freeing Memory
A simple way to make sure you don't leak memory is to explicitly decrement the ref 
erence counts of the variables you create. This is done using the SvREFCNT_dec macro:
SvREFCNT_dec(sv); // decrement sv s refcount
Perl's garbage collector works by freeing an object the moment its reference count 
reaches zero. After an SvREFCNT_dec that causes an object's refcount to reach zero, 
the object is no longer valid calls that attempt operate on it will usually yield 
crashes or other unpleasant behavior.
Using SvREFCNT and SvREFCNT_dec, you can write a function to unconditionally 
free any Perl object:
void free_it_now (SV *sv) {
   while(SvREFCNT(sv)) SvREFCNT_dec(sv);
}
But you shouldn't need to do something like this very often; in fact, if you do you 
should stop and consider what's wrong with the way you're managing the refer 
ence counts on your variables.
Implicitly Freeing Memory
The Perl API provides a way for you to hook into Perl's automatic garbage collection 
from C. The way this is done is by marking an SV, AV, or HV as mortal. Marking an 
object mortal is simply a way of deferring an SvREFCNT_dec until the end of the 
current scope. Here's an example that marks an SV as mortal using sv2_mortal():
SV *sv = newSVpv("foo",0); // sv contains "foo" and has a refcount of 1
sv_2mortal(sv);            // sv is now mortal.  At the next scope exit
                           // SvREFCNT_dec(sv) will be called and the sv
                           // will be freed.
This can be stated more succinctly, since sv_2mortal() returns the SV* passed 
to it:
SV *sv = sv_2mortal(newSVpv("foo",0)); // creates sv and mortalizes it
19
195
5






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