我在 Windows 上使用 caffe,并且遇到了我无法查明的段错误。它发生在程序退出时,WinDbg 说标量删除析构函数
,不知道内存分配在哪里。我的完整代码(目前是一个试图缩小范围的虚拟代码,但它只是有时发生):
#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;
}
我已经在 unique 或 shared_ptr 中使用 caffe_net 进行了测试,但这根本没有区别。我不知道如何找到手头的问题。
最佳答案
“有时会发生”是一种非常常见的未定义行为的情况,这正是您真正遇到的情况。段错误是理论上计算机可能执行的无数操作之一 - 行为实际上是未定义的。换句话说,正如他们在 USENET 上所说:“编译器让恶魔从你的 Nose 里飞出来是合法的。”它可能会起作用,可能会做一些奇怪的事情,或者可能会抛出一些重大错误,例如段错误。
有一些工具专门用于跟踪段错误和其他内存错误。在 Linux 上,通常是 Valgrind,而在 Windows 上,您可以使用 Dr. Memory 。只要您使用包含的调试符号(-g
)进行编译,当您通过 Dr. Memory 运行可执行文件时,它应该为您提供段错误的堆栈跟踪。
一旦获得堆栈跟踪,请检查其顶部以查看代码正在提示哪个析构函数,或者至少看看 main.cpp
中的哪一行代码正在调用负责未定义行为的函数。
此外,根据您的编译器,您可能会遇到 known bug in VC .
您可以在this answer上找到有关段错误、常见原因以及如何调试它们的更多一般信息。 .
关于c++ - caffe 的分割错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39455124/