From jmc at research.bell-labs.com  Mon Apr 26 23:19:03 2004
From: jmc at research.bell-labs.com (John Chambers)
Date: Mon Apr 26 23:19:21 2004
Subject: [Rd] Segfault: .Call and classes with logical slots
References: <Pine.LNX.4.51.0404261721500.13129@artemis.imbe.med.uni-erlangen.de>
Message-ID: <408D7CC7.3E753469@research.bell-labs.com>

I think you need to PROTECT the vector you're putting in the slot as
well as the overall object.  At any rate, the problem goes away for me
with the revised version of dummy.c below.  (Empirically, PROTECT'ing
the class definition didn't seem to be needed, but experience suggests
that too much protection  is better than too little.)

#include <Rdefines.h>

SEXP foo() {

    SEXP ans, cl, el;

    PROTECT(cl = MAKE_CLASS("test"));
    PROTECT(ans = NEW_OBJECT(cl));
    PROTECT(el = allocVector(LGLSXP, 1));
    SET_SLOT(ans, install("lgl"), el);
    LOGICAL(GET_SLOT(ans, install("lgl")))[0] = TRUE;
    UNPROTECT(3);
    return(ans);
}


Torsten Hothorn wrote:
> 
> Hi,
> 
> the following example aiming at a class containing a logical slot
> segfaults under R-1.9.0 when `gctorture(on = TRUE)' is used:
> 
> Code code (dummy.c):
> 
> #include <Rdefines.h>
> 
> SEXP foo() {
> 
>     SEXP ans;
> 
>     PROTECT(ans = NEW_OBJECT(MAKE_CLASS("test")));
>     SET_SLOT(ans, install("lgl"), allocVector(LGLSXP, 1));
>     LOGICAL(GET_SLOT(ans, install("lgl")))[0] = TRUE;
>     UNPROTECT(1);
>     return(ans);
> }
> 
> R code (dummy.R):
> 
> dyn.load("dummy.so")
> 
> setClass("test", representation = representation(lgl = "logical"))
> 
> a = .Call("foo")
> a # OK
> 
> gctorture(on = TRUE)
> a = .Call("foo")
> gctorture(on = FALSE)
> a # segfault
> 
> which gives
> 
> R>
> R>
> R> dyn.load("dummy.so")
> R>
> R> setClass("test", representation = representation(lgl = "logical"))
> [1] "test"
> R>
> R> a = .Call("foo")
> R> a
> An object of class "test"
> Slot "lgl":
> [1] TRUE
> 
> R>
> R> gctorture(on = TRUE)
> R> a = .Call("foo")
> R> gctorture(on = FALSE)
> Segmentation fault
> 
> Best,
> 
> Torsten
> 
> R> version
>          _
> platform i686-pc-linux-gnu
> arch     i686
> os       linux-gnu
> system   i686, linux-gnu
> status
> major    1
> minor    9.0
> year     2004
> month    04
> day      12
> language R
> R>
> 
>  _______________________________________________________________________
> |                                                                       |
> |       Dr. rer. nat. Torsten Hothorn                                   |
> |       Institut fuer Medizininformatik, Biometrie und Epidemiologie    |
> |       Waldstrasse 6, D-91054 Erlangen, Deutschland                    |
> |       Tel: ++49-9131-85-22707 (dienstl.)                              |
> |       Fax: ++49-9131-85-25740                                         |
> |       Email:  Torsten.Hothorn@rzmail.uni-erlangen.de                  |
> |               PLEASE send emails cc to torsten@hothorn.de             |
> |       Web: http://www.imbe.med.uni-erlangen.de/~hothorn               |
> |_______________________________________________________________________|
> 
> ______________________________________________
> R-devel@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel

-- 
John M. Chambers                  jmc@bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc


[Rd] Segfault: .Call and classes with logical slots
John Chambers