This project is read-only.


TestApp randomly crashes with heap corruption errors


I'm on windows 8. I believe the problem is here:
public ScreenBufferBackupRecord(Coord coord, Coord size)
            Trace.WriteLine(String.Format("Coord: ({0}, {1}); size: ({2}, {3}).", coord.X, coord.Y, size.X, size.Y), "ScreenBufferBackupRecord");
            _characters = new string[size.Y];
            _attributes = new ushort[size.Y][];
            _size = size;
            _coord = coord;

            StringBuilder sb = new StringBuilder();

            for (short y = 0; y < size.Y; y++)
                uint read;
                // FIX: The size of the buffer pointed to by the lpAttribute parameter should be nLength * sizeof(WORD).
                _attributes[y] = new ushort[size.X * 4]; // previously was size.X
                NativeMethods.ReadConsoleOutputCharacter(NativeMethods.ConsoleHandle, sb, (uint)size.X, coord.AddY(y), out read);
                NativeMethods.ReadConsoleOutputAttribute(NativeMethods.ConsoleHandle, _attributes[y], (uint)size.X, coord.AddY(y), out read);
                _characters[y] = sb.ToString();
This seems to have fixed the random crashing. Your attribute buffer was too small.
Closed Aug 19, 2014 at 11:06 AM by nertsch77


nertsch77 wrote Aug 19, 2014 at 11:04 AM

The Problem was Located in this Line:

StringBuilder sb = new StringBuilder();

The string has to be allocated, BEFORE the Builder is Marhsaled to the WinApi:

StringBuilder sb = new StringBuilder(size.X);


wrote Aug 19, 2014 at 11:05 AM

wrote Aug 19, 2014 at 11:06 AM