多次调用的函数会导致程序以神秘错误退出

时间:2012-07-21 18:20:15

标签: c++ winapi console

我将问题隔离到了这段代码:

#include <windows.h>
using namespace std;

const wchar_t* readLine(int posX, int posY, int len) {
  wchar_t* wcharFromConsole = new wchar_t[len];
  COORD pos = {posX,posY};
  DWORD dwChars;
  ReadConsoleOutputCharacterW(GetStdHandle(STD_OUTPUT_HANDLE),
    wcharFromConsole,  // Buffer where store symbols
    len,     // Read len chars
    pos,    // Read from row=8, column=6
    &dwChars);  // How many symbols stored
  wcharFromConsole [dwChars] = L'\0'; // Terminate, so string functions can be used
  return wcharFromConsole;
}

int main() {
  for (int i = 0; i <= 63; i++) {
    readLine(0,0,80);
  }
  system("pause");
}

问题是如果循环运行的次数少于63次,如果从控制台加载的字符长度小于80,它也可以工作......我不知道这里发生了什么。有没有我必须明确关闭的资源...但是为什么,如果一个函数关闭它,它也应该关闭它的所有资源。但是这里发生的事情我不知道,编译后的程序(没有任何错误)在静默system()函数之前退出。还有其他错误代码,因为我从项目中删除了部分代码,有时是程序请求以不寻常的方式终止,有时程序被挂起并停止接受键盘输入。

- 编辑:

我已根据建议更新了代码:

#include <iostream>
#include <windows.h>

using namespace std;

const wchar_t* readLine(int posX, int posY, int len) {
  wchar_t* wcharFromConsole = new wchar_t[len];
  COORD pos = {posX,posY};
  DWORD dwChars = 0;
  if(!ReadConsoleOutputCharacterW(GetStdHandle(STD_OUTPUT_HANDLE),
    wcharFromConsole,  // Buffer where store symbols
    len,     // Read len chars
    pos,    // Read from row=8, column=6
    &dwChars))  // How many symbols stored
  {
    cout << "ReadConsoleOutputCharacterW failed, code" << GetLastError() << endl;
  }
  wcharFromConsole [dwChars] = L'\0'; // Terminate, so string functions can be used
  return wcharFromConsole;
}

int main() {
  for (int i = 0; i <= 100; i++) {
    cout << "loop count: " << i << endl;
    readLine(0,0,80);
  }
  system("pause");
}

输出:

loop count: 0
loop count: 1
loop count: 2
loop count: 3
// [...]
loop count: 63
loop count: 64

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

(第一个剪辑根本没有产生任何错误。)

2 个答案:

答案 0 :(得分:1)

这可能只是“一个接一个”。你正在为“Len”字符分配空间,你读“Len”字符,但是你最后会加上\ 0。

更改你的新内容以分配Len + 1,你可能会没事的。

答案 1 :(得分:1)

dwChars应作为ReadConsoleOutputCharacterW传递给dwChars -1。 你正在覆盖数组的末尾。