rsk82
rsk82

Reputation: 29377

function called multiple times causes program to exit with enigmatic error

I isolated the problem to this code:

#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");
}

The thing is if the loop is running less than 63 times it works, if length of chars loaded from console is less than 80 it also works... I have no Idea what is happening here. Are there any resources I must explicitly close... but why, if a function closes it should close also all of its resources. But what is happening here I have no idea, the compiled program (without any error) exits before silently system() function. There were other error codes as I removed portions of code from my project, sometimes it was that program requested termination in a unusual way, at other times program hanged and stopped accept keyboard input.

-- edit:

I've updated the code according to suggestions:

#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");
}

output:

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.

(The first snipped didn't produce any error at all.)

Upvotes: 0

Views: 157

Answers (2)

JimR
JimR

Reputation: 16113

dwChars should be passed to ReadConsoleOutputCharacterW as dwChars -1. You're overwriting the end of the array.

Upvotes: 1

Christian Stieber
Christian Stieber

Reputation: 12496

It's probably just the "off by one". You are allocating space for "Len" characters, you reading "Len" characters, but you're putting an additional \0 at the end.

Change your new to allocate Len+1 and you'll probably be fine.

Upvotes: 1

Related Questions