Writing C Modules with XS
# test some simple mime type recognitions
is(Gnome::MIME::file_type("foo.gif"),   image/gif ,  "recognizes .gif");
is(Gnome::MIME::file_type("foo.jpg"),   image/jpeg , "recognizes .jpg");
is(Gnome::MIME::file_type("foo.html"),  text/html ,  "recognizes .html");
Managing Memory Usage
As it turns out, the preceding XSUB does not leak memory. This came as a surprise 
to me I assumed that the gnome_mime_type() function returned a dynamically 
allocated string that I would need to clean up. If you look at the generated code in 
MIME.c (Listing 9 5), you'll see this line at the end of the function:
sv_setpv(TARG, RETVAL); XSprePUSH; PUSHTARG;
This line copies the string pointed to by RETVAL into TARG. TARG is the SV that will 
actually be returned by the subroutine. After that, TARG is pushed onto the stack 
and the function returns. My expectation was that this would result in a leak since 
the pointer stored in RETVAL wouldn't be freed before going out of scope. As it turns 
out, this pointer doesn't need to be freed because it comes from an internal pool 
managed by the Gnome API.
But, for the sake of the example, what would I need to do if the return value from 
gnome_mime_type() did need to be freed? My first draft might have been as follows:
const char *
file_type(filename)
     const gchar * filename
CODE:
     RETVAL = gnome_mime_type(filename);
OUTPUT:
     RETVAL
CLEANUP:
     Safefree(RETVAL);
The CLEANUP block specifies code to be run at the end of the generated function. 
This might work fine, but it's incorrect. The problem is that Gnome and Perl might 
be using different memory allocators. Thus, calling Perl's Safefree() function on 
memory allocated by Gnome is not guaranteed to yield the expected results. Instead, I 
would need to use the same call that Gnome uses, g_free():
CLEANUP:
     g_free(RETVAL);
21
219
9






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