Saikatranjan Parida wrote: > > I am trying to embed perl in my C++ program. My application needs to > evaluate different very simple perl programs. These perl programs will be > created on the fly. So they can't be in any file on the disk. I read up > perlembed and tried to execute the statements using eval_pv. This evaluation > works fine. My application is a Dialog box application with MFC. The exact > procedure that I am using is -- > > 1. Create the PerlInterpreter in the constructor of the dialog box using > code such as (my_perl is the PerlInterpreter) > char *embedding[] = { "", "-e", "0" }; > my_perl = perl_alloc(); > perl_construct( my_perl ); > perl_parse(my_perl, NULL, 3, embedding, NULL); > PL_exit_flags |= PERL_EXIT_DESTRUCT_END; > perl_run(my_perl); > > 2. When the user presses a button, create an appropriate perl program and > put it in a string. Then use eval_pv to evaluate and get the results > from it using sv* methods. strRead is the perl program string. This step can > be repeated many number of times. > SV *ret = eval_pv(strRead, TRUE); > BOOL bSuccess = SvTRUE(ret); > SvREFCNT_dec(ret); > ret = Nullsv; > > 3. When the dialog exits, the PerlInterpreter is freed using the following > code. > perl_destruct(my_perl); > perl_free(my_perl); > > The problem with this code is, after eval_pv has been called many times the > application leaks some amount of memory. From perlembed I understand that > this is because every time the perl parser sets up parsed code for the > evaluation string and does not free them. Is there any way to clean up the > data created for parsing and executing the perl program after the execution > is over. > > The other way of doing this is to create the PerlInterpreter every time and > execute the program and then free the PerlInterpreter. But this method is > way too slow. >
Hi. I can't see anywhere that perlembed says anything like: > every time the perl parser sets up parsed code for the evaluation string > and does not free them In place of your section 2 I have for (int i = 1; i <= 1000; i++) { char *perl = (char *) malloc(32); sprintf(perl, "%d / 7", i); SV *ret = eval_pv(perl, TRUE); free(perl); perl = (char *) 0; BOOL bSuccess = SvTRUE(ret); } Which just caculates the value of each number divided by seven and returns it. There is no memory loss at all for this. Are you sure you're not doing strRead = malloc(n); without a subsequent free(strRead) ? What I did get, though, is an Attempt to free unreferenced scalar. error if I included the SvREFCNT_dec(ret); line. I hope this helps in some way. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]