为什么我可以得到一个未处理的异常访问冲突写在c ++ / CLI? [英] Why could i get an Unhandled exception Access violation writing in c++/CLI?

查看:746
本文介绍了为什么我可以得到一个未处理的异常访问冲突写在c ++ / CLI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力编写一个解决方案,从一个c ++ win32console和一个c ++ dll。我终于设法让他们说没有链接器的错误(所以我假设两个都是完全托管的c ++ / CLI项目),但当我运行控制台,我得到以下错误。

I have been struggeling writing a solution excisting out of an c++ win32console and a c++ dll. i finally managed to get them talking without linker errors (so i am assuming both are fully managed c++/CLI projects) but when i run the console i get the following error.



中的0x03f71849处的未处理异常Company.Pins.Bank.Win32Console.exe:
0xC0000005:访问冲突写
位置0x00000001。

Unhandled exception at 0x03f71849 in Company.Pins.Bank.Win32Console.exe: 0xC0000005: Access violation writing location 0x00000001.

控制台还显示以下


未处理的异常:
System.NullReferenceException:Object
引用未设置为
对象的实例。 at wmain in c:... \win32console.cpp:line
20在_wmainCRTStartup()

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at wmain in c:...\win32console.cpp:line 20 at _wmainCRTStartup()

假设这是因为未处理的异常。

but i am assuming this is because of the unhandled exception.

跟踪这个错误,以及我可以在下面的代码块中返回时发生错误。 (通过返回链接的方法似乎是一步一步的,只是当返回它似乎坏了。)为了防止你没有注意到,我没有写下面的代码自己,它是由视觉工作室

tracking down this error as well as i can the error occurs when doing the return in the below code block. (the method linked by the return seems to step through fine, just when returning it seems to go bad.) Just in case you hadn't noticed, i did not write the below code myself, it was generated by visual studio.

#ifdef WPRFLAG
int wmainCRTStartup(
#else  /* WPRFLAG */
int mainCRTStartup(
#endif  /* WPRFLAG */

#endif  /* _WINMAIN_ */
        void
        )
{
        /*
         * The /GS security cookie must be initialized before any exception
         * handling targetting the current image is registered.  No function
         * using exception handling can be called in the current image until
         * after __security_init_cookie has been called.
         */
        __security_init_cookie();

        return __tmainCRTStartup();
}

#include "stdafx.h"
#include "UInstruction.h"

#define DllExport  __declspec(dllexport)
#define DllImport  __declspec(dllimport)

using namespace System;

编辑:win32console.cpp代码如下。

edit: and the win32console.cpp code is below.

//int main(array<System::String ^> ^args)
int _tmain(int argc, _TCHAR* argv[])
{
    auto P2 = (TCHAR *)"3 Barrowstead";
    TCHAR* P3 = (TCHAR *)"3 Barrowstead";
    double* P1;
    P1[0] = 13;

    UserInstruction(P1, P2, P3);
}


推荐答案

不初始化它,所以它不指向一个对象(它包含一些垃圾地址):

You declare a pointer and do not initialize it so it doesn't point at an object (it contains some garbage address):

double* P1;     

然后尝试写入这个未初始化的指针指向的地方:

Then you try to write to wherever this uninitialized pointer points:

P1[0] = 13;

您不能使用未初始化的变量。您需要初始化 P1 以指向某个对象,然后才能解除引用。

You cannot use an uninitialized variable. You need to initialize P1 to point at some object before you dereference it.

这篇关于为什么我可以得到一个未处理的异常访问冲突写在c ++ / CLI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆