为std :: string分配内存异常(试图在UE4中使用YOLO / Darknet)

问题描述 投票:0回答:1

我正在尝试让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的第132行-c:\ Program Files( x86)\ 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,UE4项目,YOLO cpp DLL。我已经尝试对所有内容进行完全全新的Visual Studio 2015和2017安装,并且每次都遇到相同的问题。

有人知道这到底是怎么回事吗?以及我如何解决它或解决它?

c++ memory dll compiler-errors unreal-engine4
1个回答
0
投票

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

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

硬方式:使用相同的编译器/选项编译所有模块(对于UE4,则特别困难)。>>

© www.soinside.com 2019 - 2024. All rights reserved.