Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Dear Monks,
I'm currently working on glueing C and perl together (maybe there will be no more fopen() and fscanf() *grin*)

The problem is that a) the bindings are hard to understand and b) my code leaks memory as hell. But I took almost everything more or less directly from man perlembed and man perlguts, thus it can't be _that_ wrong...
BTW: this is a cooked down version for you, fellow Monks, that doesn't leak as much as the original which has some SVs and stuff, but still ... it leaks :(

I post the code right below, but a quick word of warning: yes, it's C ;-)

/* compile with */ /* gcc -o monk monk.c `perl -MExtUtils::Embed -e ccopts -e ldopts` */ /* I tried this under Perl 5.6.0 and perl 5.005_3 */ # include <stdio.h> # include <EXTERN.h> # include <perl.h> # include <unistd.h> void call_perl(); int main(int argc, char **argv, char **env) { /* That's OK: */ printf("Testing perl embedding\n"); call_perl(); /* But in a loop we leak as hell */ while (1) { call_perl(); sleep(1); } } void call_perl(void) { static PerlInterpreter *my_perl; char *embedding[] = { "", "-e", "0" }; my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); eval_pv("print \"Perl Version: $]\\n\"",TRUE); perl_destruct(my_perl); perl_free(my_perl); }

Regards Stefan K


In reply to MemLeak in Perl from C Calls by stefan k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found