caffe 的分段错误
Segmentation error with caffe
我在 Windows 上使用 caffe,但遇到无法查明的分段错误。它发生在程序退出时,WinDbg 说 scalar deleting destructor
,不知道内存分配在哪里。我的完整代码(目前是一个试图缩小范围的虚拟代码,但它只是有时会发生):
#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;
}
我已经用 caffe_net 或 shared_ptr 进行了测试,但这根本没有区别。我不知道如何找到手头的问题。
"Happens sometimes" 是一种非常常见的未定义行为,这是您真正遇到的。分段错误是计算机可能做的理论上无限多的事情之一——行为实际上是未定义的。换句话说,正如他们在 USENET 上所说:"It is legal for the compiler to make demons fly out of your nose." 它可能会工作,它可能会做一些奇怪的事情,或者它可能会抛出一些像段错误这样的重大错误。
有专门用于跟踪分段错误和其他内存错误的工具。在 Linux 上,通常是 Valgrind,而在 Windows 上,您会使用 Dr. Memory。只要您使用包含的调试符号 (-g
) 进行编译,当您通过 Dr. Memory 运行 可执行文件时,它应该会为您提供分段错误的堆栈跟踪。
一旦获得堆栈跟踪,请检查它的顶部以查看代码正在抱怨哪个析构函数,或者至少,main.cpp
中的哪一行代码正在调用该函数( s) 对未定义的行为负责。
此外,根据您的编译器,您可能会遇到 known bug in VC。
您可以在 .
上找到有关分段错误、常见原因以及如何调试它们的更多一般信息。
我在 Windows 上使用 caffe,但遇到无法查明的分段错误。它发生在程序退出时,WinDbg 说 scalar deleting destructor
,不知道内存分配在哪里。我的完整代码(目前是一个试图缩小范围的虚拟代码,但它只是有时会发生):
#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;
}
我已经用 caffe_net 或 shared_ptr 进行了测试,但这根本没有区别。我不知道如何找到手头的问题。
"Happens sometimes" 是一种非常常见的未定义行为,这是您真正遇到的。分段错误是计算机可能做的理论上无限多的事情之一——行为实际上是未定义的。换句话说,正如他们在 USENET 上所说:"It is legal for the compiler to make demons fly out of your nose." 它可能会工作,它可能会做一些奇怪的事情,或者它可能会抛出一些像段错误这样的重大错误。
有专门用于跟踪分段错误和其他内存错误的工具。在 Linux 上,通常是 Valgrind,而在 Windows 上,您会使用 Dr. Memory。只要您使用包含的调试符号 (-g
) 进行编译,当您通过 Dr. Memory 运行 可执行文件时,它应该会为您提供分段错误的堆栈跟踪。
一旦获得堆栈跟踪,请检查它的顶部以查看代码正在抱怨哪个析构函数,或者至少,main.cpp
中的哪一行代码正在调用该函数( s) 对未定义的行为负责。
此外,根据您的编译器,您可能会遇到 known bug in VC。
您可以在