<div dir="ltr">Have you tried running your code within a debugger? It's pretty straightforward to do that, either by building a package and loading the resulting library or by using sourceCpp. Here's a simple standard session using the following file (named '/tmp/zero_ref.cc' on my system) as input:<div>
<pre style="color:rgb(0,0,0)">#include <Rcpp.h>
using namespace Rcpp;
// This next routine is guaranteed to cause an invalid pointer access
// error (e.g. an access violation in Windows, or a segmentation
// violation on a UNIX box.) Calling it will crash R.
//
// [[Rcpp::export]]</pre><pre style="color:rgb(0,0,0)">double CrashMe() {
return *((double *) NULL);
}
</pre></div><div>Here are the results, both without the debugger and inside the debugger.<div><pre style="color:rgb(0,0,0)">jwlm@d442:~$ R -q
> library(Rcpp)
> sourceCpp('/tmp/zero_ref.cc')
> CrashMe()
*** caught segfault ***
address (nil), cause 'memory not mapped'
Traceback:
1: .Primitive(".Call")(<pointer: 0x7fa7d5309e00>)
2: CrashMe()
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 3
jwlm@d442:~$ R -q -d gdb
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <<a href="http://gnu.org/licenses/gpl.html">http://gnu.org/licenses/gpl.html</a>>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<<a href="http://www.gnu.org/software/gdb/bugs/">http://www.gnu.org/software/gdb/bugs/</a>>...
Reading symbols from /usr/lib/R/bin/exec/R...(no debugging symbols found)...done.
(gdb) r
Starting program: /usr/lib/R/bin/exec/R -q
[Thread debugging using libthread_db enabled]
> library(Rcpp)
> sourceCpp('/tmp/zero_ref.cc')
> CrashMe()
Program received signal SIGSEGV, Segmentation fault.
CrashMe () at zero_ref.cc:12
12 }
(gdb) l
7 // violation on a UNIX box.) Calling it will crash R.
8 //
9 // [[Rcpp::export]]
10 double CrashMe() {
11 return *((double *) NULL);
12 }
13
14
15 #include <Rcpp.h>
16
(gdb) </pre></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jan 21, 2013 at 9:55 AM, 该走了 <span dir="ltr"><<a href="mailto:gaizoule@gmail.com" target="_blank">gaizoule@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I am tring to use Rcpp Module to export C++ class to R, but meet some problem.<div>Here is my code:</div><div>###########################################################################################</div>
<div>/* file PL modules.cpp */</div>
<div><div>using namespace Rcpp;</div><div>using namespace std;</div><div><br></div><div>class SymPL {</div><div>public:</div><div> SymPL(vector<double> close);</div><div> void update(int idx, </div><div><span style="white-space:pre-wrap"> </span> double txnqty, </div>
<div><span style="white-space:pre-wrap"> </span> double txnprc);</div><div> DataFrame toDF();</div><div> </div><div><br></div><div> int nrow;</div><div> vector<double> txn_qty, txn_prc, txn_fee;</div>
<div> vector<double> pos_qty, close_prc, PL;</div><div>};</div><div><br></div><div>SymPL::SymPL(vector<double> close) {</div><div> nrow = close.size();</div><div> txn_qty = vector<double>(nrow);</div>
<div> txn_prc = vector<double>(nrow);</div><div> txn_fee = vector<double>(nrow);</div><div> pos_qty = vector<double>(nrow);</div><div> close_prc = vector<double>(close);</div><div> PL = vector<double>(nrow);</div>
<div>}</div><div><br></div><div>void SymPL::update(int idx,</div><div><span style="white-space:pre-wrap"> </span> double txnqty, </div><div><span style="white-space:pre-wrap"> </span> double txnprc) {</div>
<div> txn_qty[idx] = txnqty;</div><div> txn_prc[idx] = txnprc;</div><div> txn_fee[idx] = 0; //calfee(txnqty*txnprc); todo: write a function to cal fee</div><div> if(idx == 0) { //index start with 0</div><div> pos_qty[idx] = txnqty;</div>
<div> PL[idx] = txnqty * (close_prc[idx] = txnprc) - txn_fee[idx];</div><div> }</div><div> else {</div><div> pos_qty[idx] = txnqty + pos_qty[idx-1];</div><div> PL[idx] = pos_qty[idx-1] * (close_prc[idx] - close_prc[idx-1]) + </div>
<div> txnqty * (close_prc[idx] = txnprc) - txn_fee[idx];</div><div> }</div><div>}</div><div><br></div><div>DataFrame SymPL::toDF() {</div><div> DataFrame PLrecord = DataFrame::create(_["txn.qty"] = txn_qty,</div>
<div><span style="white-space:pre-wrap"> </span> _["txn.prc"] = txn_prc,</div><div><span style="white-space:pre-wrap"> </span> _["txn.fee"] = txn_fee,</div><div>
<span style="white-space:pre-wrap"> </span> _["pos.qty"] = pos_qty,</div><div><span style="white-space:pre-wrap"> </span> _["close.prc"] = close_prc,</div><div>
<span style="white-space:pre-wrap"> </span> _["PL"] = PL);</div><div> return PLrecord;</div><div>}</div><div><span style="white-space:pre-wrap"> </span> </div><div>class PortPL {</div>
<div>public:</div><div> CharacterVector symnames;</div><div> int sym_num;</div><div> double initMoney;</div><div> int period_num;</div><div> vector<double> total_eq;</div><div> vector<double> total_PL;</div>
<div> vector<double> period_return;</div><div> vector<double> cum_return;</div><div> DatetimeVector timeindex;</div><div> </div><div> PortPL(SEXP times,</div><div><span style="white-space:pre-wrap"> </span> SEXP CLprc, double initM);</div>
<div> void update(int idx, NumericVector txnqty_vec, </div><div><span style="white-space:pre-wrap"> </span> NumericVector txnprc_vec);</div><div> void exit(int idx, NumericVector txnprc_vec);</div>
<div> void hold(int idx);</div><div> NumericVector getpos(int idx);</div><div> List getSymPLs();</div><div><br></div><div>private:</div><div> vector<SymPL> symbols;</div><div><br></div><div>};</div><div><br></div>
<div>PortPL::PortPL(SEXP times,</div><div><span style="white-space:pre-wrap"> </span> SEXP CL, </div><div><span style="white-space:pre-wrap"> </span> double initM):</div><div>
timeindex(times) {</div><div> List CLprc(CL);</div><div> sym_num = CLprc.size();</div><div> symnames = CLprc.names();</div><div> initMoney = initM;</div><div> //timeindex = DatetimeVector(times);</div><div> period_num = timeindex.size();</div>
<div> </div><div> //initialize total_eq, total_PL, period_return, cum_return</div><div> total_eq = vector<double>(period_num);</div><div> total_PL = vector<double>(period_num);</div><div> period_return = vector<double>(period_num);</div>
<div> cum_return = vector<double>(period_num);</div><div> </div><div> //initialize each SymPL</div><div> for(int i = 0; i < sym_num; i++) {</div><div> vector<double> close = as<vector<double> >(CLprc[i]);</div>
<div> symbols[i] = SymPL(close);</div><div> }</div><div>}</div><div><br></div><div>void PortPL::update(int idx, NumericVector txnqty_vec, </div><div><span style="white-space:pre-wrap"> </span> NumericVector txnprc_vec) {</div>
<div> double sumPL = 0;</div><div> for(int i = 0; i < sym_num; i++) {</div><div> symbols[i].update(idx, txnqty_vec[i], txnprc_vec[i]);</div><div> sumPL += symbols[i].PL[idx];</div><div> }</div><div> </div><div>
total_PL[idx] = sumPL;</div><div> if(idx == 0) {// index starts with 0</div><div> total_eq[idx] = initMoney + total_PL[idx];</div><div> period_return[idx] = total_PL[idx] / initMoney;</div><div> cum_return[idx] = total_PL[idx] / initMoney;</div>
<div> }</div><div> else {</div><div> total_eq[idx] = total_eq[idx-1] + total_PL[idx];</div><div> period_return[idx] = total_PL[idx] / total_eq[idx];</div><div> cum_return[idx] = total_eq[idx] / initMoney - 1;</div>
<div> }</div><div>}</div><div><br></div><div>void PortPL::hold(int idx) {</div><div> NumericVector tempv(sym_num, 0.0);</div><div> this->update(idx, tempv, tempv);</div><div>}</div><div><br></div><div>void PortPL::exit(int idx, NumericVector txnprc_vec) {</div>
<div> NumericVector curr_pos(sym_num);</div><div> for(int i = 0; i < sym_num; i++) {</div><div> curr_pos[i] = symbols[i].pos_qty[idx-1];</div><div> }</div><div> this->update(idx, curr_pos, txnprc_vec);</div>
<div>
}</div><div><br></div><div>NumericVector PortPL::getpos(int idx) {</div><div> NumericVector curr_pos(sym_num);</div><div> for(int i = 0; i < sym_num; i++) {</div><div> curr_pos[i] = symbols[i].pos_qty[idx-1];</div>
<div> }</div><div> return curr_pos;</div><div>}</div><div><br></div><div>List PortPL::getSymPLs() {</div><div> List allsym(sym_num);</div><div> for(int i = 0; i < sym_num; i++){</div><div> allsym[i] = symbols[i].toDF();</div>
<div> }</div><div> allsym.names() = symnames;</div><div> return allsym;</div><div>}</div><div><br></div><div>RCPP_MODULE(PortPL_module) {</div><div> class_<PortPL>("PortPL")</div><div> .constructor<SEXP, SEXP, double>()</div>
<div> .field("total.eq", &PortPL::total_eq)</div><div> .field("total.PL", &PortPL::total_PL)</div><div> .field("symnames", &PortPL::symnames)</div><div> .field("cum.return", &PortPL::cum_return)</div>
<div> .field("period.return", &PortPL::period_return)</div><div> .field("initMoney", &PortPL::initMoney)</div><div> </div><div> .method("update", &PortPL::update)</div>
<div> .method("exit", &PortPL::exit)</div><div> .method("hold", &PortPL::hold)</div><div> .method("getpos", &PortPL::getpos)</div><div> .method("getSymPLs", &PortPL::getSymPLs)</div>
<div> ;</div><div>}</div></div><div><br></div><div>#######################################################################</div><div><br></div><div>## R code</div><div><div>library(inline)</div><div>library(Rcpp)</div>
<div><br></div><div>fx <- cxxfunction(signature(), </div><div> includes = paste(readLines("PnL module.cpp"),</div><div> collapse = "\n"),</div>
<div>
plugin = "Rcpp")</div><div><br></div><div>port <- Module("PortPL_module", getDynLib(fx)) </div><div>PortPL <- port$PortPL</div><div><br></div><div>datel <- as.POSIXct(Sys.Date()+1:10)</div>
<div>colse <- 1:10</div><div>initM <- 1e5</div><div><br></div><div>testport <- new(PortPL, datel, list(hjs=colse), initM)</div></div><div><br></div><div>Then the R crash with error message:</div><div><br></div><div>
<div>*** caught segfault ***</div><div>address (nil), cause 'memory not mapped'</div><div><br></div><div>Traceback:</div><div> 1: .External(class__newInstance, module, pointer, ...)</div><div> 2: new_CppObject_xp(fields$.module, fields$.pointer, ...)</div>
<div> 3: Rcpp:::cpp_object_initializer(.self, .refClassDef, ...)</div><div> 4: .Object$initialize(...)</div><div> 5: initialize(value, ...)</div><div> 6: initialize(value, ...)</div><div> 7: new(PortPL, datel, list(hjs = colse), initM)</div>
<div><br></div><div>Possible actions:</div><div>1: abort (with core dump, if enabled)</div><div>2: normal R exit</div><div>3: exit R without saving workspace</div><div>4: exit R saving workspace</div><div>Selection:</div>
</div><div><br></div><div>######################################################################</div><div>I just can not figure out what is wrong, Is there anyone can help me check out? Thanks in advance!</div><div><br>
</div>
<div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>
<br>_______________________________________________<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" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>