2

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

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

控制台还显示以下内容

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

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

跟踪此错误以及在下面的代码块中执行返回时会发生错误。(返回链接的方法似乎可以通过,只是返回时似乎变坏了。)以防万一您没有注意到,我自己没有编写以下代码,它是由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 代码如下。

//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);
}
4

5 回答 5

8

您声明一个指针并且不初始化它,因此它不指向一个对象(它包含一些垃圾地址):

double* P1;     

然后,您尝试写入此未初始化指针指向的任何位置:

P1[0] = 13;

您不能使用未初始化的变量。在取消引用之前,您需要初始化P1以指向某个对象。

于 2011-05-04T15:52:55.610 回答
2
double* P1;

未初始化。然后,您尝试将其第一个条目设置为 13。繁荣、访问冲突或更糟。

这些片段中的任何一个都应该起作用:

double P1;
P1 = 13;
UserInstruction(&P1, P2, P3);

或者

double P1[1];
P1[0] = 13;
UserInstruction(P1, P2, P3);

或者

double *P1 = new double[1];
P1[0] = 13;
UserInstruction(P1, P2, P3);
delete[] P1;
于 2011-05-04T15:54:57.627 回答
1

The following statements are also wrong when using UNICODE build:

  auto P2 = (TCHAR *)"3 Barrowstead";
  TCHAR* P3 = (TCHAR *)"3 Barrowstead"; 

because you're casting a normal (char) array to a wchar_t pointer.

if you build with UNICODE then you should change these in:

  LPCTSTR P2 = _T("3 Barrowstead");
  LPCTSTR P3 = _T("Barrowstead"); 
于 2011-05-04T18:15:21.753 回答
0

It is undefined behaviour to convert a string literal to a TCHAR*, as if UNICODE is defined then TCHAR* will become wchar_t*, and a string literal is not a wchar_t* and this pointer conversion is undefined.

于 2011-05-04T15:57:05.113 回答
0

I managed to find error the following way:

  • It was not in the line that the error poped-up.
  • It was actually in the last location I have PIN_PTR the memory.

I have used the following to copy a vector:

memcpy(&pined_ptr[0],&unmanagedvector[0],sizeofunmanagedvector);

The problem was PINED_PRT SIZE < unmanagedVectorSize ! Stupid error.

This messed up all managed memory, an expoded couple of lines & functions later.

How you can find in your code: Go disableling ranges of the code, until your code does not crash.

于 2012-10-28T18:13:47.533 回答