C++ caffe分割误差

C++ caffe分割误差,c++,segmentation-fault,caffe,C++,Segmentation Fault,Caffe,我在Windows上使用caffe,并且得到了无法精确定位的分割错误。它发生在程序退出时,WinDbg说标量删除析构函数,不知道内存分配在哪里。我的完整代码(目前是一个试图缩小范围的伪代码,但它只是偶尔发生): #包括 #包括 #包括“boost/algorithm/string.hpp” #包括“google/protobuf/text_format.h” #包括 #包括 #包括 #包括 #包括“caffe/blob.hpp” #包括“caffe/common.hpp” #包括“caffe/n

我在Windows上使用caffe,并且得到了无法精确定位的分割错误。它发生在程序退出时,WinDbg说
标量删除析构函数
,不知道内存分配在哪里。我的完整代码(目前是一个试图缩小范围的伪代码,但它只是偶尔发生):

#包括
#包括
#包括“boost/algorithm/string.hpp”
#包括“google/protobuf/text_format.h”
#包括
#包括
#包括
#包括
#包括“caffe/blob.hpp”
#包括“caffe/common.hpp”
#包括“caffe/net.hpp”
#包括“caffe/proto/caffe.pb.h”
#包括“caffe/util/db.hpp”
#包括“caffe/util/format.hpp”
#包括“caffe/util/io.hpp”
使用caffe::Blob;
使用caffe::caffe;
使用caffe::数据;
使用caffe::Net;
使用std::string;
名称空间db=caffe::db;
int main(int argc,字符**argv){
//用程序调用初始化日志记录。首先需要做的是
::google::InitGoogleLogging(argv[0]);
cv::Mat平均值;
//将Caffe设置为仅在CPU模式下运行
Caffe::设置_模式(Caffe::Caffe::CPU);
std::向量标签;
/*std::共享测试网络*/
网络*caffe\U测试网络;
caffe_test_net=new net(“D:\\Development\\caffe windows\\models\\bvlc_reference\u caffenet\\deploy.prototxt”,caffe::Phase::test);
caffe_test_net->copytrained layers from(“D:\\Development\\caffe windows\\models\\bvlc_reference_caffenet\\bvlc_reference_caffenet.caffemodel”);
删除caffe_测试网;
返回1;
}
我已经用caffe_net在一个独特的或共享的ptr中进行了测试,但这没有任何区别。我不知道如何找到手头的问题。

对于未定义的行为来说,“有时会发生”是一件非常常见的事情,而这正是您真正遇到的问题。从理论上讲,分段错误是计算机可能做的无数事情中的一个,其行为实际上是未定义的。换句话说,正如他们在USENET上所说:“编译器让恶魔飞出你的鼻子是合法的。”它可能会工作,它可能会做一些奇怪的事情,或者它可能会抛出一些重大错误,比如segfault

有专门用于跟踪分段错误和其他内存错误的工具。在Linux上,这通常是Valgrind,而在Windows上,则使用。只要编译时包含调试符号(
-g
),当您通过Dr.Memory运行可执行文件时,它就会为您提供分段错误的堆栈跟踪

一旦获得堆栈跟踪,检查堆栈顶部,查看代码抱怨的是哪一个析构函数,或者至少是
main.cpp
中的哪一行代码正在调用负责未定义行为的函数

此外,根据编译器的不同,您可能会遇到错误


您可以找到有关分段错误、常见原因以及如何调试它们的更多一般信息。

好的,我现在已经这样做了,我得到的主要错误是
无效堆参数:分配了运算符new,释放了free replace\u free
,但是,我认为这是棺材中的最后一根钉子,即堆边界之外的
不可寻址访问:写入4个字节
,但创建不可寻址访问的代码在我看来很好。好的,修复了它。感谢您的输入,这使修复得以实现:)您能分享问题是什么吗?我也看到了同样的事情。谢谢
#include <string>
#include <vector>

#include "boost/algorithm/string.hpp"
#include "google/protobuf/text_format.h"
#include <stdio.h>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp"
#include "caffe/util/format.hpp"
#include "caffe/util/io.hpp"

using caffe::Blob;
using caffe::Caffe;
using caffe::Datum;
using caffe::Net;
using std::string;
namespace db = caffe::db;

int main(int argc, char** argv) {
    // Initialize logging with program call. First thing that needs to be done
    ::google::InitGoogleLogging(argv[0]);

    cv::Mat mean_;

    // Set Caffe to run in CPU only mode
    Caffe::set_mode(caffe::Caffe::CPU);

    std::vector<std::string> labels_;

    /*std::shared_ptr<Net<float>> caffe_test_net;*/
    Net<float>* caffe_test_net;
    caffe_test_net = new Net<float>("D:\\Development\\caffe-windows\\models\\bvlc_reference_caffenet\\deploy.prototxt", caffe::Phase::TEST);
    caffe_test_net->CopyTrainedLayersFrom("D:\\Development\\caffe-windows\\models\\bvlc_reference_caffenet\\bvlc_reference_caffenet.caffemodel");

    delete caffe_test_net;
    return 1;
}