2016-10-13 5 views
2

CaffeのC++/CLIラッパーをC#に作成しようとしています。私は自分自身の分類プロジェクトをlibcaffeの上に作った。分類プロジェクトは、C++コンソールプロジェクトから呼び出されたときに完全に機能します。CaffeのC++/CLIラッパーを呼び出すときのAccessViolationException

は私がコンパイルして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 C

#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); 
} 

テストプログラムから呼び出すことができ、空のラッパープロジェクトを行っている

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 で発生しました追加情報:しようとしました保護されたメモリを読み書きする。これはしばしば、他のメモリが壊れていることを示します。

割り当てられていないメモリを読み書きするのがエラーではありませんか?どのようにこれを起動時に取得することができますか?この問題の解決策はありますか?

コールスタック:!

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()

EDIT: 私のラッパーで、.NETへの参照に問題があるようです。システムへの参照には赤い感嘆符がありました。私はそれを削除し、今私はそれを追加しようとするとエラーが発生します:エラーHRESULT E_FAILがCOMコンポーネントへの呼び出しから返されました。

+0

C#コードでネイティブC++を呼び出すには?またはその逆の詩? – Matt

+1

これは、C#コードの実行が開始される前であっても、*非常に*早く発生します。 Main()がスタックトレースにどのように含まれていないかに注意してください。トリガは、C++/CLIアセンブリをロードするジッタです。このアセンブリは、ネイティブコードを取得して初期化します。 C++の静的初期化バグは非常に診断が難しいので、C++の「静的初期化順序の失敗」という問題は、一週間の人生を壊す可能性があります。最初の重要なステップは、アンマネージドデバッグを有効にして、少なくとも*何かを見ることです。 –

+0

@Matt私はC++ –

答えて

0

通常、AccessViolationExceptionはnullのC++ポインタを使用しようとしています コールスタックはさらに、メインルーチンの実行とは対照的に静的なイニシャライザを使用していることを示しています - 私はcaffeについてはわかりませんが、 classification.h(またはその依存関係の1つ)で有効な初期化が実行されていることを確認してください

+0

を使用することはできますが、C++からテストすると同じクラッシュを取得しませんか? –

関連する問題