c++ - 为 std::string 释放内存的异常(尝试在 UE4 中使用 YOLO/Darknet)

标签 c++ memory dll compiler-errors unreal-engine4

我试图让 YOLO/Darknet 在 UE4 项目中工作。我已将 YOLO 构建为 C++ DLL,并将所有依赖项添加到项目中。它构建得很好,并且代码的某些部分正在运行,但是我在某个时候遇到了异常,经过几天的努力试图弄清楚,我现在不知所措,需要一些帮助。

下面是我调用的从 UE4 类中创建 YOLO Detector 类的代码:

YOLO_DataPath = FPaths::ProjectDir() + "Plugins/Stereolabs/Source/YOLO/Data/";
std::string YOLO_DataPathC(TCHAR_TO_UTF8(*YOLO_DataPath));

std::string  NamesFile = YOLO_DataPathC + "obj.names";
std::string  CFGFile = YOLO_DataPathC + "yolo-obj.cfg";
std::string  WeightsFile = YOLO_DataPathC + "yolo-obj.weights";

Detector YOLODetector(CFGFile, WeightsFile);

这是被调用的构造函数(来自 'yolo_v2_class.cpp',第 130 行):
LIB_API Detector::Detector(std::string cfg_filename, std::string weight_filename, int gpu_id) : cur_gpu_id(gpu_id)
{
    wait_stream = 0;
#ifdef GPU
    int old_gpu_index;
    check_cuda( cudaGetDevice(&old_gpu_index) );
#endif

    detector_gpu_ptr = std::make_shared<detector_gpu_t>();
    detector_gpu_t &detector_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());

#ifdef GPU
    //check_cuda( cudaSetDevice(cur_gpu_id) );
    cuda_set_device(cur_gpu_id);
    printf(" Used GPU %d \n", cur_gpu_id);
#endif
    network &net = detector_gpu.net;
    net.gpu_index = cur_gpu_id;
    //gpu_index = i;

    _cfg_filename = cfg_filename;
    _weight_filename = weight_filename;

    char *cfgfile = const_cast<char *>(_cfg_filename.c_str());
    char *weightfile = const_cast<char *>(_weight_filename.c_str());

    net = parse_network_cfg_custom(cfgfile, 1, 1);
    if (weightfile) {
        load_weights(&net, weightfile);
    }
    set_batch_network(&net, 1);
    net.gpu_index = cur_gpu_id;
    fuse_conv_batchnorm(net);

    layer l = net.layers[net.n - 1];
    int j;

    detector_gpu.avg = (float *)calloc(l.outputs, sizeof(float));
    for (j = 0; j < NFRAMES; ++j) detector_gpu.predictions[j] = (float*)calloc(l.outputs, sizeof(float));
    for (j = 0; j < NFRAMES; ++j) detector_gpu.images[j] = make_image(1, 1, 3);

    detector_gpu.track_id = (unsigned int *)calloc(l.classes, sizeof(unsigned int));
    for (j = 0; j < l.classes; ++j) detector_gpu.track_id[j] = 1;

#ifdef GPU
    check_cuda( cudaSetDevice(old_gpu_index) );
#endif
}

所有这些代码似乎运行良好,但是当它到达构造函数的末尾时,它遇到了一个异常,它似乎试图删除一个字符串,在这一行(xmemory - c:\Program Files (x86) 的第 132 行)\Microsoft Visual Studio 14.0\VC\include\xmemory0):
::operator delete(_Ptr);

完整的调用栈是这样的:
[Inline Frame] yolo_cpp_dll.dll!std::_Deallocate(void * _Ptr, unsigned __int64) Line 132
[Inline Frame] yolo_cpp_dll.dll!std::allocator<char>::deallocate(char *) Line 720
[Inline Frame] yolo_cpp_dll.dll!std::_Wrap_alloc<std::allocator<char> >::deallocate(char * _Count, unsigned __int64) Line 987
[Inline Frame] yolo_cpp_dll.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Tidy(bool) Line 2258
[Inline Frame] yolo_cpp_dll.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::{dtor}() Line 1017
yolo_cpp_dll.dll!Detector::Detector(std::basic_string<char,std::char_traits<char>,std::allocator<char> > cfg_filename, std::basic_string<char,std::char_traits<char>,std::allocator<char> > weight_filename, int gpu_id) Line 177

从我可以找到的有关此错误的有限信息中,似乎这可能是使用不同编译器编译 DLL 的问题。我现在花了几天时间尝试从头开始编译所有东西的不同版本 - UE4 from source、UE4 项目、YOLO cpp DLL。我已经尝试过完全干净安装 Visual Studio 2015 和 2017 的所有内容,但每次都遇到同样的问题。

有谁知道这里到底发生了什么?以及我如何修复它或解决它?

最佳答案

简单的方法:永远不要在不同模块之间传递 std::xxx 对象。使用原始 C 类型。内存应该在分配它的模块中释放。

检测器(const char* cfg_filename,const char* weight_filename,int gpu_id)

困难的方式:用相同的编译器/选项编译所有模块(在 UE4 的情况下特别困难)。

关于c++ - 为 std::string 释放内存的异常(尝试在 UE4 中使用 YOLO/Darknet),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60623053/

相关文章:

c++ - 删除 vector 类成员

CUDA:使用网格跨步循环减少共享内存

java - 使用 dll 和 java jni4net 时出现 UnsatisfiedLinkError 异常

c++ - 两个瓦片之间的瓦片 map 冲突解决

python - 如何使用 PyKCS11 库验证签名数据

c++ - getline() 读取额外的一行

memory - H2o:迭代大于内存的数据而不将所有数据加载到内存中

c++ - 从另一个线程中删除对齐的内存

java - 启动 DLL 进程不工作

c# - 用于 x86/x64 架构的 SQLite dll