gpt4 book ai didi

c# - 调用 Caffe 的 C++/CLI 包装器时出现 AccessViolationException

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:17 24 4
gpt4 key购买 nike

我正在尝试为 Caffe 制作一个 C++/CLI 包装器到 c#。我在 libcaffe 上做了自己的分类项目。从 C++ 控制台项目调用时,分类项目完美运行。

我做了一个空的包装器项目,它可以编译并可以从 c# 调用

CaffeWrapper.h

#pragma once
//#include "classification.h"
namespace CaffeWrapper {

public ref class Detector
{
public:
Detector(System::String^ model_file,
System::String^ trained_file,
System::String^ label_file);

private:
//Classifier * c;

CaffeWrapper.cpp

#include "CaffeWrapper.h"
#include <msclr\marshal_cppstd.h>
namespace CaffeWrapper {
Detector::Detector(System::String^ model_file,
System::String^ trained_file,
System::String^ label_file)
{
std::string unmanaged_model =
msclr::interop::marshal_as<std::string>(model_file);
std::string unmanaged_train =
msclr::interop::marshal_as<std::string> (trained_file);
std::string unmanaged_label =
msclr::interop::marshal_as<std::string>(label_file);

//c = new Classifier(unmanaged_model, unmanaged_train, unmanaged_label);
}

C#中的测试程序

using System;
using System.IO;
using CaffeWrapper;
namespace TestDetect
{
class Program
{
static void Main(string[] args)
{
string basePath = "C:/Caffe/TestClassify/";
string model_file = basePath + "net.prototxt";
string trained_file = basePath + "lenet_iter_20000.caffemodel";
string label_file = basePath + "labels.txt";
string imgfile = basePath + "imageTest.pgm";
Console.WriteLine("test");
var detecotr = new Detector(model_file, trained_file, label_file);
}
}
}

当我在包装器中包含我的分类项目时

#include "classification.h"

启动时出现以下错误:

“System.AccessViolationException”类型的第一次机会异常发生在 mscorlib.dll 中附加信息:试图读取或写入 protected 内存。这通常表明其他内存已损坏。

这不是读取或写入未分配内存的错误吗?如何在启动时得到这个?你有解决这个问题的办法吗?

调用堆栈:

mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)

mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()

编辑:我的包装器中对 .NET 的引用似乎存在一些问题。对系统的引用有一个红色的感叹号。我删除了它,现在当我尝试添加它时出现错误:调用 COM 组件返回错误 HRESULT E_FAIL。

最佳答案

AccessViolationException 通常建议尝试使用空 C++ 指针调用堆栈进一步表明问题出在静态初始化程序而不是执行主例程 - 我不知道 caffe 但我会查看 classification.h 中的静态(或其依赖项之一)并检查有效初始化正在进行中

关于c# - 调用 Caffe 的 C++/CLI 包装器时出现 AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40023425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com