[R-pkg-devel] New package with C++ code causes R abort in RStudio, not in R console.

Luc De Wilde Luc@DeW||de @end|ng |rom UGent@be
Thu Nov 14 11:41:22 CET 2024


After some corrections (initialization of pointer vars in some structs/classes) I still get the problem in Rstudio, aborting the R process.

This is the dump in Dr.Mingw:
rsession-utf8.exe caused an Unknown [0xC0000374] Exception at location 00007FF96FD68215 in module ntdll.dll.

AddrPC           Params
00007FF96FD68215 0000004BAF5FF9B0 0000000000000000 0000000000000000  ntdll.dll!RtlReportFatalFailure+0x9
00007FF96FD66B09 0000004BAF5F8749 00007FF96FEAB0E0 0000000000000003  ntdll.dll!RtlReportCriticalFailure+0xa9
00007FF96FD0EAE2 0000000000000003 0000004BAF5F8749 00000204D0AC0000  ntdll.dll!RtlpHeapHandleError+0x12
00007FF96FDFF9EA 0000000000000000 00000204D7B6D230 0000004BAF5F8840  ntdll.dll!RtlpHpHeapHandleError+0x7a
00007FF96FD0977B 2AAAAAAAAAAAAAAB 0000004BAF5F8940 0000004BAF5F8940  ntdll.dll!RtlpLogHeapFailure+0x4b
00007FF96FD12486 00000204D0AC0000 0000020400000000 00000204D7B6D240  ntdll.dll!RtlFreeHeap+0x266
00007FF96D96DDAB 0000000000000000 0000000000000000 0000000000000000  ucrtbase.dll!_free_base+0x1b
00007FF65019AB89 0000004BAF5F8940 0000004B00000005 0000004B0000FDE9  rsession-utf8.exe!rd_traceback+0x72a449
00007FF650369484 0000004BAF5F89E0 0000000000000000 0000000000000000  rsession-utf8.exe!rd_traceback+0x8f8d44
00007FF650370672 0000000000000000 0000000000000000 0000000000000000  rsession-utf8.exe!rd_traceback+0x8fff32
00007FF8D0D95328 00000204D0BB44E8 0000000000000000 00007FF800000000  R.dll!Rf_EncodeChar+0x68
00007FF8D0D953E4 00000204D404F240 00000204D0B41060 0000000000000000  R.dll!Rvprintf+0x44
00007FF8D0D95452 0000004BAF5FAC50 00000204DC46BF90 0000000000000000  R.dll!Rprintf+0x22
00007FF8D0C80F74 00000204DBABDC10 00000204D2A62188 00000204D2A594F8  R.dll!R_do_slot_assign+0x3504
00007FF8D0D05200 0000000000000001 00007FF6503BF8E8 00000204D0AC0000  R.dll!R_ParseEvalString+0x59e0
00007FF8D0D1BE74 202020200029226E 0000000000000000 00000204D7457FF0  R.dll!R_ParseEvalString+0x1c654
00007FF8D0D1C17B 00000204D09D2280 0000000000000000 00000204D09D02AC  R.dll!R_ParseEvalString+0x1c95b
00007FF8D0D20320 00000204D38747B0 00000204D3874CF0 0000004BAF5FC330  R.dll!Rf_eval+0x3a20
00007FF8D0D216EF 003100330061002D 00330034002D0066 00000204D38749E0  R.dll!Rf_eval+0x4def
00007FF8D0D1C3DE 0000004B00000007 0000000000800000 0000004BAF5FEBA0  R.dll!R_ParseEvalString+0x1cbbe
00007FF8D0D478FD 0000004BAF5FEBA0 00007FF8D0D1DEDA 0000000000000000  R.dll!Rf_ReplIteration+0x22d
00007FF8D0D47C88 00007FF8D0D45EE1 00000204D0B5E690 0000000000000000  R.dll!Rf_ReplIteration+0x5b8
00007FF8D0D47D22 0000004BAF5FD768 0000004B00000000 0000004BAF5FD701  R.dll!run_Rmainloop+0x52
00007FF65035D0F0 00007FF650354D40 0000000000000000 0000004BAF5FDBB0  rsession-utf8.exe!rd_traceback+0x8ec9b0
00007FF650356E0E 0000000000000000 0000004BAF5FF7C0 0000000000000000  rsession-utf8.exe!rd_traceback+0x8e66ce
00007FF64F8ABDBE 00007FF650BED878 0000000000000000 00000204D0ACEBD0  rsession-utf8.exe!0x2bbdbe
00007FF6507D1BC8 0000000000000000 0000000000000000 0000000000000000  rsession-utf8.exe!rd_traceback+0xd61488
00007FF96E37DBE7 0000000000000000 0000000000000000 000004F0FFFFFB30  KERNEL32.DLL!BaseThreadInitThunk+0x17
00007FF96FDBFBEC 0000000000000000 0000000000000000 0000000000000000  ntdll.dll!RtlUserThreadStart+0x2c


and, although the package dll is not in the stack, I suppose the problem is "connected" to the code that evaluates an expression in R:

SEXP lav_eval(const char* expression, int& error, int varpos)
{
  if (Debug) Rprintf("Eval %s :", expression);
  string strexpr("tryCatch(");
  strexpr+=expression;
  strexpr+=", error = function(e)  NaN)";
  error = 0;
  SEXP cmdSexp, cmdexpr, ans = R_NilValue;
  ParseStatus status;
  cmdSexp = PROTECT(Rf_allocVector(STRSXP, 1));
  SET_STRING_ELT(cmdSexp, 0, Rf_mkChar(strexpr.c_str()));
  cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &status, R_NilValue));
  if (status != PARSE_OK) {
    UNPROTECT(2);
    error = (int)(spe_invalidexpr << 24) + varpos;
    return ans;
  }
  /* Loop is needed here as EXPSEXP will be of length > 1 */
  ans = PROTECT(Rf_allocVector(VECSXP, Rf_length(cmdexpr)));
  for(int i = 0; i < Rf_length(cmdexpr); i++)
    SET_VECTOR_ELT(ans, i, Rf_eval(VECTOR_ELT(cmdexpr, i), R_GlobalEnv));
  double* dval;
  if (Rf_isNumeric(VECTOR_ELT(ans, 0))) {
    dval = REAL(VECTOR_ELT(ans, 0));
    if (ISNAN(dval[0]) && !ISNA(dval[0])) {
      error = (int)(spe_invalidexpr << 24) + varpos;
      UNPROTECT(3);
      return ans;
    }
  }
  UNPROTECT(3);
  if (Debug) Rf_PrintValue(ans);
  return VECTOR_ELT(ans, 0);
}


The package was installed with environment variables DEBUG="on" and R_C_BOUNDS_CHECK="yes".

The exception reported is a heap corruption problem.

I hope you, or someone else,  can help me with this problem.

Luc
________________________________________
Van: Ivan Krylov <ikrylov using disroot.org>
Verzonden: dinsdag 12 november 2024 22:33
Aan: Luc De Wilde <Luc.DeWilde using UGent.be>
CC: r-package-devel using r-project.org <r-package-devel using r-project.org>
Onderwerp: Re: [R-pkg-devel] New package with C++ code causes R abort in RStudio, not in R console.

В Tue, 12 Nov 2024 21:09:54 +0000
Luc De Wilde <Luc.DeWilde using UGent.be> пишет:

> I have attached a debugger (from Visual Studio) to the R process,
> which gives me the message "A breakpoint instruction (__debugbreak()
> statement or similar call) was executed in rsession-utf8.exe.
>
> When I go to the dll module in the Call Stack I get the message
> "Binary was not built with debug information".

So the crash does happen inside lavaanC.dll, did I get this right?

If you set the environment variable DEBUG to a non-empty string before
compiling the package, the resulting *.dll will have debug information
in the DWARF format (this is set in file.path(R.home('etc'), 'x64',
'Makeconf')). Visual Studio might not work with it, so you could
install gdb into Rtools using pacman, or you could try to give the
executable to Dr. Mingw <https://github.com/jrfonseca/drmingw>.

--
Best regards,
Ivan



More information about the R-package-devel mailing list