in reply to Working with multiple interpreters (my_perl is NULL when embed perl in C ; "dSP" segmentation faults)

Do I understand correctly that you creating new thread inside your library? If yes, then probably that's the source of the problem. When perl creates new thread it creates copy of interpreter, so every thread works with its own copy. After thread exits the memory is freed. If new thread is created in C library, the copy of the interpreter isn't created. I'm not too familiar with perl internals, but that's not good.

  • Comment on Re: my_perl is NULL when embed perl in C ; "dSP" segmentation faults

Replies are listed 'Best First'.
Re^2: my_perl is NULL when embed perl in C ; "dSP" segmentation faults
by perlmonk1729 (Acolyte) on Nov 04, 2009 at 04:15 UTC
    Thanks Zwon,

    I had not created any interpreter (perl_alloc) in the thread earlier. I now tried this and it still fails/seg faults inside call_sv(). I'm still trying to understand if I need perl_parse and if I should do a perl_clone rather than perl_alloc. Also, unclear if there is any "locking"/mutual exclusion between the two interpreters required before invoking call_sv

    Are there any other forums with more experience in this that I should post at?

    void mythread_fn(void) { static PerlInterpreter *my_perl=NULL; my_perl = perl_alloc(); PERL_SET_CONTEXT(my_perl); perl_construct(my_perl); perl_run(my_perl); while (1) { /* monitor for events */ if (event == xyz) callback_handler(event); } perl_destruct(my_perl); perl_free(my_perl); } void callback_handler(int event) { dTHX; dSP; ENTER; SAVETMPS; SV * sv = disc_callback; /* saved earlier in an API call */ if (sv == (SV*)NULL) croak("Internal error...\n"); PUSHMARK(SP); XPUSHs(sv_2mortal(newSViv(event))); PUTBACK; /* Call the Perl sub */ call_sv(sv, G_DISCARD); /* crashes as shown below in gdb */ FREETMPS; LEAVE; } Part of gdb stacktrace. #0 Perl_pp_entersub (my_perl=0x6587b0) at pp_hot.c:2945 #1 0x00000000004224f3 in S_call_body (my_perl=0x6587b0, myop=0x65a6e0 +, is_eval=1 '\001') at perl.c:2728 #2 0x00000000004223c4 in Perl_call_sv (my_perl=0x6587b0, sv=0x635420, + flags=2) at perl.c:2607