<div dir="ltr">Thank you for the quick and kind reply, Kevin! You are right.<div><br></div><div>I confirmed that the program option object is defined as a local variable in mylib::Simulation constructor, and is destructed correctly. But I found that the object is initialized with other objects from several source files, and they were defined as static variables. Changing them into local variables has solved the problem. What I didn't know was, therefore, that static variables do survive after Rcpp function calling. Now I will have to take a careful look at other variables too.</div><div><br></div><div><span style="font-size:12.8px">Thanks a lot!!</span></div><div><span style="font-size:12.8px">Watal</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 6, 2016 at 3:13 PM, Kevin Ushey <span dir="ltr"><<a href="mailto:kevinushey@gmail.com" target="_blank">kevinushey@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If I had to guess, maybe there is some initialization that occurs when<br>
your mylib::Simulation object is constructed, and you need to ensure<br>
that code is only run once? (Perhaps the code that defines the program<br>
options needs to be hidden behind an initialization flag, or something<br>
like that)<br>
<br>
Nonetheless, this isn't really an Rcpp question, and it's hard to know<br>
for sure without seeing how your Simulation object is defined.<br>
<div><div class="h5"><br>
On Tue, Jul 5, 2016 at 10:56 PM, Watal M. Iwasaki <<a href="mailto:heavy.watal@gmail.com">heavy.watal@gmail.com</a>> wrote:<br>
> Hi all,<br>
><br>
> I have been trying to make an R function to call my C++ simulation program<br>
> in this way:<br>
> ```c++<br>
> // [[Rcpp::plugins(cpp14)]]<br>
> #include <Rcpp.h><br>
> #include <mylib.hpp><br>
> // [[Rcpp::export]]<br>
> std::string run_simulation(Rcpp::CharacterVector<br>
> args=Rcpp::CharacterVector::create()) {<br>
>     auto vs_args = Rcpp::as<std::vector<std::string>>(args);<br>
>     mylib::Simulation sim(vs_args);<br>
>     sim.run();<br>
>     return sim.results();<br>
> }<br>
> ```<br>
><br>
> The class `Simulation` parses arguments with `boost::program_options` and is<br>
> implemented in `libmylib.dylib`, which can be linked with `PKG_LIBS =<br>
> -lmylib`. Both `devtools::check()` and independent `Rcpp::sourceCpp()` can<br>
> successfully build the function `run_simulation()`. It runs perfectly as I<br>
> intended **only if it is the first call**. The second time I call<br>
> `run_simulation('-v')`, I get the following error:<br>
> ```<br>
> Error: option '-v' is ambiguous and matches different versions of<br>
> '--verbose'<br>
> ```<br>
><br>
> Other options like `-h` or `--mutation=0` raise the same error. It indicates<br>
> that every option is re-defined after the first call (maybe?). Calling<br>
> without any argument is always OK, and an executable `a.out` built with<br>
> `main.cpp -lmylib` has no problem. I guess the key is how C++ objects are<br>
> cached in R environments. Is this a bug in Rcpp? Or am I missing something?<br>
><br>
> Thanks,<br>
><br>
> --<br>
> Watal M. Iwasaki / 岩嵜 航<br>
> SOKENDAI, Graduate University for Advanced Studies,<br>
> Hayama, Kanagawa 240-0193, Japan<br>
> +81-46-858-1576<br>
> <a href="https://heavywatal.github.io/" rel="noreferrer" target="_blank">https://heavywatal.github.io/</a><br>
><br>
</div></div>> _______________________________________________<br>
> Rcpp-devel mailing list<br>
> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Watal M. Iwasaki / 岩嵜 航<br>SOKENDAI, Graduate University for Advanced Studies,<br>Hayama, Kanagawa 240-0193, Japan<br>+81-46-858-1576<br><a href="https://heavywatal.github.io/" target="_blank">https://heavywatal.github.io/</a></div></div>
</div>