简体   繁体   中英

.NET Unhandled exception of type 'System.AccessViolationException' occurs when calling function from a C++ CLR DLL

I have a C# application that uses a managed C++ DLL (CLR) of a class (let's call the class ManagedProcessor ). When I am about to create an object of this class, I receive the following message from Visual Studio:

An unhandled exception of type 'System.AccessViolationException' occurred in Main.exe.

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

If I execute the application from the command line, I receive even less information, simply "Unhandled Exception."

Here is a simple example of how it is called:

class Program
{
        static void CallDLLFunction()
        {
            try
            {
                ManagedProcessor proc = new ManagedProcessor("C:/SomeFolder");                    
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine("Encountered exception: {0}...", e.Message);
            }
        }

        static void Main(string[] args)
        {
            System.Console.WriteLine("Before calling DLL Function");

            CallDLLFunction();

            System.Console.WriteLine("Before calling DLL Function");                
        }
    }
}

// ManagedProcessor.h

public ref class ManagedProcessor
{
public:
    ManagedProcessor(System::String ^folder);
    void doSomething();

private:
    bool mFlag;
};    

I have included all of the necessary dependencies of the DLL but it still gives me this error. FYI, the target .NET platform is 4.0 and the assemblies are x64. Also, the DLL also has references to some native C++ code.

This problem only occurred as we moved our code base from Visual Studio 2008 to 2010 and we used CMake to generate the VS 2010 projects (I ensured that the generate 2010 projects had the exact settings as their 2008 versions).

I would really like some help with this problem. Thanks in advance.

I was able to solve my problem by simply removing the Boost thread library from the linker input of my C++/CLI project. For some reason, that particular library was causing some sort of memory corruption / access violation at runtime (or mixing of runtimes, not sure). As far as I know, the library was built for VS 2010, but I didn't generate it so I can't confirm that with 100% surety. It was a transitive dependency, meaning that it was included by CMake because it was a reference included by one of the directly referenced libraries of my project (default CMake behavior). I changed the CMakeLists.txt file for the dependent project so that its dependencies weren't included in the C++/CLI project by adding the PRIVATE keyword to the target_link_libraries CMake command and voila:

target_link_libraries(DependentTarget PRIVATE ${REQUIRED_LIBS})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM