Skip to content

[Rcpp-devel] Segmentation Fault on Exit of RInside-using Programs

2 messages · Mario Bourgoin, Dirk Eddelbuettel

#
Dear Sir or Madam,

I first noticed this problem yesterday.  Every C++ program that
instantiates RInside has a segmentation violation upon exit.  (That's after
doing everything it was supposed to do the way it was expected to do it.)
What am I doing wrong?

A minimal example is

#include <RInside.h>
int main(int argc, char *argv[]) {
    RInside R(argc, argv);
    exit(0);
}

On

Ubuntu 10.04 LTS
R version 2.15.2 (2012-10-26) -- "Trick or Treat"
Rcpp version 0.10.1
RInside version 0.2.10

To clear up the problem this morning, I nuked all R system code (sudo
apt-get remove r-base) and packages from my machine (sudo rm -rf
/usr/local/lib/R and other places).  I freshly installed R (sudo apt-get
install r-base) and the RInside package (> install.packages( 'RInside' )).
I copied the rinside_sample0.cpp and its Makefile into a fresh directory,
added ``CXXFLAGS:=$(filter-out -O3,$(CXXFLAGS))'' to the Makefile to
facilitate debugging, compiled

$ make rinside_sample0
g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include
-I/usr/local/lib/R/site-library/RInside/include -pipe -g -Wall
rinside_sample0.cpp  -L/usr/lib64/R/lib -lR  -lblas -llapack
-L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp
-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib
-L/usr/local/lib/R/site-library/RInside/lib -lRInside
-Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o rinside_sample0

and ran the code to get

$ ./rinside_sample0
Segmentation fault

In gdb I get

$ gdb ./rinside_sample0
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
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:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/mob/src/example/rinside_sample0...done.
(gdb) run
Starting program: /home/mob/src/example/rinside_sample0
[Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff603e7af in ~Rostream (this=0x7ffff625cba0, __in_chrg=<value
optimized out>, __vtt_parm=<value optimized out>) at
../inst/include/Rcpp/iostream/Rostream.h:41
41    ../inst/include/Rcpp/iostream/Rostream.h: No such file or directory.
    in ../inst/include/Rcpp/iostream/Rostream.h

I have downloaded the RInside and Rcpp sources and will reinstall them,
hopefully without -O3 and with -g so I can get more informative results.

Sincerely,
Mario
#
Mario,
On 7 December 2012 at 12:56, Mario Bourgoin wrote:
| Dear Sir or Madam,
| 
| I first noticed this problem yesterday.? Every C++ program that instantiates
| RInside has a segmentation violation upon exit.? (That's after doing
| everything it was supposed to do the way it was expected to do it.)? What am I
| doing wrong?

Nothing, it's Rcpp 0.10.1 and the issue got fixed in the first commit _after_
finalising 0.10.1.

This was also already discussed on the very list this very week. It is also
completely harmless.

You can any one of 

  i)   revert Rcpp to an older release 

  ii)  upgrade Rcpp to an SVN snapshot version post 0.10.1

  iii) apply the patch below.

That code has by now been rewritten by Romain anyway....

Modified: pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h	2012-11-27 01:34:59 UTC (rev 4046)
+++ pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h	2012-11-27 14:21:42 UTC (rev 4047)
@@ -38,7 +38,12 @@
             std::ostream( new Rstreambuf(output) ), 
             buf( static_cast<Rstreambuf*>( rdbuf() ) )
         {}
-        ~Rostream(){ delete buf ; }
+        ~Rostream() { 
+            if (buf != NULL) {
+                delete buf; 
+                buf = NULL;
+            }
+        }
     };
     
     // declare global variable


[...]
 

| 
| A minimal example is
| 
| #include <RInside.h>
| int main(int argc, char *argv[]) {
| ??? RInside R(argc, argv);
| ??? exit(0);
| }

[..]

| I have downloaded the RInside and Rcpp sources and will reinstall them,
| hopefully without -O3 and with -g so I can get more informative results.

Just apply the patch. That will make the (scary-looking but harmless) notice
of a segfault go away.

Sorry that you had to go through all of this.  Sometimes it pays to simply be
subscribed here and lurk...

Dirk