首页 > 解决方案 > 退出对 C++ FFI 的调用时出现 Haxe/Neko 异常,该 FFI 对抽象类型的项目进行迭代

问题描述

当我返回一个value包含来自一个 FFI 的向量的抽象类型,并在另一个 FFI 中对其进行迭代时,第二个 FFI 调用完成时会出现异常。我可以毫无问题地一一访问向量中的项目,但迭代这些值似乎会导致问题。如果我使用 for each 循环、迭代器或什至在索引上循环并不重要。

哈希码:

class VectorExample {
    public static function main() {
        var vec = createVec();
        // dumpForIntIter(vec); // fails on exit
        dumpEach(vec);          // works
    }

    private static var createVec = neko.Lib.load("vectorLib", "createVec", 0);
    private static var dumpForIntIter = neko.Lib.load("vectorLib", "dumpForIntIter", 1);
    private static var dumpEach = neko.Lib.load("vectorLib", "dumpEach", 1);
}

cp代码:

#include <iostream>
#include <string>
#include <vector>
#include <neko.h>

void free_vec( value handle ) {
    std::cout << "freeing vec" << std::endl;
    std::vector<std::string>* vec = (std::vector<std::string>*) val_data(handle);
    delete vec;
}

DEFINE_KIND(k_vector);

value createVec() {
    auto vec = new std::vector<std::string>();
    vec->push_back(std::string("one"));
    vec->push_back(std::string("two"));
    vec->push_back(std::string("three"));

    value handle = alloc_abstract(k_vector, vec);
    val_gc(handle, free_vec);

    return handle;
}
DEFINE_PRIM(createVec,0);

// fails on exit
void dumpForIntIter( value handle ) {
    auto vec = (std::vector<std::string>*) val_data(handle);
    std::cout << "size: " << vec->size() << std::endl;
    for (int ii=0; ii<vec->size(); ii++)
        std::cout << "  item: " << vec->at(ii) << std::endl;
}
DEFINE_PRIM(dumpForIntIter,1);

// works
void dumpEach( value handle ) {
    int ii = 0;
    auto vec = (std::vector<std::string>*) val_data(handle);
    std::cout << "size: " << vec->size() << std::endl;
    std::cout << "  item: " << vec->at(0) << std::endl;
    ii++;
    std::cout << "  item: " << vec->at(1) << std::endl;
    ii++;
    std::cout << "  item: " << vec->at(2) << std::endl;
    ii++;
}
DEFINE_PRIM(dumpEach,1);

构建 Neko 模块的命令:

g++ -o vectorLib.ndll -shared -fPIC -std=c++11 -I/usr/include/x86_64-linux-gnu \
    -L/usr/lib/x86_64-linux-gnu -lneko -ldl vectorLib.cpp

运行时输出dumpForIntIter

size: 3
  item: one
  item: two
  item: three
freeing vec
Called from ? line 1
Called from VectorExample.hx line 6
Uncaught exception - vectorLib@dumpForIntIter

请注意,虽然freeing vec是日志中的最后一件事,但即使free_vec为空或不存在,也会出现问题。

由于dumpForIntIteranddumpForEach似乎基本等同于我,我怀疑createVec.

文档:Neko FFI

更新:

我禁用了 nekovm 中的异常捕获,发现问题是段错误。这是 valgrind 的输出:

Jump to the invalid address stated on the next line
   at 0x2A50DA10C1C9AED6: ???
 Address 0x2a50da10c1c9aed6 is not stack'd, malloc'd or (recently) free'd

Can't extend stack to 0x2a50da10c1c99f88 during signal delivery for thread 1:
  no stack segment

Process terminating with default action of signal 11 (SIGSEGV)
 Access not within mapped region at address 0x2A50DA10C1C99F88
   at 0x2A50DA10C1C9AED6: ???

更新 2:

如果我更改vector<string>vector<int>问题仍然存在。如果我更改为char**(使用mallocnew)没有问题。如果我更改为只是string没有问题。好像alloc_abstract不喜欢vector

标签: c++haxeneko

解决方案


推荐阅读