调试时访问冲突写入位置异常

时间:2019-05-19 13:30:43

标签: c++ windows service visual-studio-debugging

抛出以下异常(仅在调试模式下,实际上不抛出异常)时,我正在尝试调试c ++ Windows服务: “在xxxxxxxx.exe中的0x00007FF75127C410处引发异常:0xC0000005:访问冲突写入位置0x0000018818AF3000”。

重要说明: 该代码之前对我有用,但可能是某些工作空间更改已损坏它。当它起作用时,我正在VS 17中进行开发,最近我已将IDE更改为VS19。出现问题时,我曾尝试回到VS 17,但没有起作用。

  • 代码中没有任何变化!与以前的版本相同
  • 我尝试回到VS 17
  • 我曾尝试更改项目配置,例如“基本运行时检查”或“运行时库”,因为我无法弄清楚在这种情况下,调试和释放的工作原理为何不同

编辑:

    USHORT          logFileHeaderVersion;
    DLLS_CHECK_MODE eDllCheckMode;
    LONGLONG        lastClientTimeZValue;
    LONGLONG        lastServerClientTimeDelta;
    wstring         computerName;
    string          macAddresses;
    BYTE*           buffer;
    DWORD           dwBytesToWrite  = 0;
    DWORD           dwBytesWritten  = 0;

    logFileHeaderVersion        = LOG_FILE_HEADER_VERSION;
    eDllCheckMode               = getConfigurationManager()->getDllsCheckMode();
    lastClientTimeZValue        = Globals::getLastClientTimeZValue();
    lastServerClientTimeDelta   = Globals::getLastServerClientTimeDelta();
    computerName                = getComputerName();
    macAddresses                = getMacAddresses();

    // Convert time delta values from ticks to milliseconds.
    lastClientTimeZValue        = lastClientTimeZValue      / MILLISECOND_IN_TICKS;
    lastServerClientTimeDelta   = lastServerClientTimeDelta / MILLISECOND_IN_TICKS;

    // Determine header size.
    dwBytesToWrite  +=  2;                                      // Header-size size.
    dwBytesToWrite  +=  2;                                      // Header version size.
    dwBytesToWrite  +=  1;                                      // Mode size.
    dwBytesToWrite  +=  8;                                      // Client time Z value size.
    dwBytesToWrite  +=  8;                                      // Server client time delta size.
    dwBytesToWrite  +=  (   (computerName.size() + 1) * 2   );  // Computer name size (+ null terminator, wide string).
    dwBytesToWrite  +=  (   macAddresses.size() + 1         );  // Mac addresses size (+ null terminator).

    // Allocate the header.
    buffer = new BYTE[dwBytesToWrite];

    // Fill the header.

    *((USHORT*)     (buffer + dwBytesWritten)) = (USHORT)   dwBytesToWrite;
    dwBytesWritten  +=  2;

    *((USHORT*)     (buffer + dwBytesWritten)) =            logFileHeaderVersion;
    dwBytesWritten += 2;

    *((BYTE*)       (buffer + dwBytesWritten)) = (BYTE)     eDllCheckMode;
    dwBytesWritten  +=  1;

    *((LONGLONG*)   (buffer + dwBytesWritten)) =            lastClientTimeZValue;
    dwBytesWritten  +=  8;

    *((LONGLONG*)   (buffer + dwBytesWritten)) =            lastServerClientTimeDelta;
    dwBytesWritten  +=  8;

    wcscpy_s(   (PWCHAR)(buffer + dwBytesWritten),  dwBytesToWrite - dwBytesWritten,    computerName.c_str());
    dwBytesWritten  +=  (   (computerName.size() + 1) * 2   );

以下行中引发了异常:我认为这与wstring的使用有关。也许是RELEASE进行了一些优化,以解决DEBUG中的问题

    wcscpy_s(   (PWCHAR)(buffer + dwBytesWritten),  dwBytesToWrite - dwBytesWritten,    computerName.c_str());

0 个答案:

没有答案