Rcpp对象似乎被R的垃圾收集器破坏了

时间:2019-07-19 02:02:28

标签: r garbage-collection rcpp

我有一个R脚本,可以创建由不同基础数据构造的同一类的Rcpp对象。然后lapply()用于调用其方法,并将数据放入列表中。

但是运行一段时间后,它崩溃并显示以下错误。对我来说,这似乎是垃圾收集器的问题。

no function to return from, jumping to top level
1: foo(items)
 2: lapply(items) {
 3: FUN(X[[1]], ...)
 4: lapply(1:5, function(i) {
    myRcppModule::bar(item)
 5: FUN(1:4[[2]], ...)
 6: myRcppModule::bar(item)
 7: new(Rcpp::Module("myRcppMod", PACKAGE = "MyRCPP_MOD")$MyRcppModWrapper, list(item)
 8: getClass(Class, where = topenv(parent.frame()))
 9: .getClassFromCache(Class, where)
 10: Rcpp::Module("myRcppMod", PACKAGE = "MyRCPP_MOD")$MyRcppModWrapper
11: .getModulePointer(x)
12: Module(module, mustStart = TRUE)
13: methods::setRefClass(clname, fields = fields, contains = "C++Object", methods = methods, where = where)
14: setClass(Class, contains = superClasses, where = where, ...)
15: is(try(setIs(Class, class2, classDef = classDef, where = where)), "try-error")
16: try(setIs(Class, class2, classDef = classDef, where = where))
17: tryCatch(expr, error = function(e) {
    call <- conditionCall(e)
    if (!is.null(call)) {
        if (identical(call[[1]], quote(doTryCatch)))
            c
18: tryCatchList(expr, classes, parentenv, handlers)
19: tryCatchOne(expr, names, parentenv, handlers[[1]])
20: doTryCatch(return(expr), name, parentenv, handler)
21: setIs(Class, class2, classDef = classDef, where = where)
22: completeExtends(classDef, class2, obj, where = where)
23: .walkClassGraph(ClassDef, "contains", where, attr(ext, "conflicts"))
24: .transitiveExtends(className, by, ext[[i]], exti, strictBy)
25: .combineExtends(byExt, toExt, by, to, strictBy)
26: (function (x)
x$.self$finalize())(<environment>)
27: x$.self$finalize
28: envRefInferField(x, what, getClass(class(x)), selfEnv)

摘要如下所示

foo <- function(items) {
    lapply(
        items,
        function(item) {
            data <- lapply(
                1:5,
                function(i) {
                    obj <- myRcppModule::bar(item)
                    obj$getData()
            })
    })
}

while(TRUE) {
    items <- some_vector
    foo(items)
}

如果我在迭代后调用gc(),脚本可以运行更长的时间而不会崩溃。这使我认为我在记忆中做错了什么。可以在调用obj之前,gc释放getData()吗?

请帮助。预先感谢。

foo <- function(items) {
    lapply(
        items,
        function(item) {
            data <- lapply(
                1:5,
                function(i) {
                    obj <- myRcppModule::bar(item)
                    obj$getData()
            })
            gc(TRUE)
    })
}

while(TRUE) {
    items <- some_vector
    foo(items)
}

以下是对象创建的Rcpp代码,其中MyRcppModWrapper是普通的C ++类。

MyRcppModWrapper* createMyRcppMod(SEXP argsexp)
{
    try {
        Rcpp::List args = Rcpp::as<Rcpp::List>(argsexp);
        const auto item = Rcpp::as<std::string>(args["item"]);

        auto obj = new MyRcppModWrapper(item);

        return obj;
    } catch(const std::exception& e) {
        forward_exception_to_r(e);
    }

    return nullptr;
}

std::array<double> getData(MyRcppModWrapper* self)
{
        try {
            return self->getData();
        } catch (const std::exception& e) {

0 个答案:

没有答案