Skip to content

Modern console issue: initial screen buffer can never be freed #4

@rprichard

Description

@rprichard

In Win8.1 and Win10 (and presumably Win8 as well), it's impossible to free the initial screen buffer. It is possible to enter a state where the initial buffer can't be accessed via any means, but I can still see it taking up memory via the Task Manager.

Test 1:

// Tie the child to a new buffer, then kill the parent.  There is no way to
// reference the initial screen buffer, but it's still taking up memory.
int main() {
    Worker p;
    p.getStdout().setFirstChar('A');
    p.getStdout().trySetScreenBufferSize({ 30000, 1000 });
    p.newBuffer(false, 'B').activate();
    auto c = p.child();
    p.exit();
    Sleep(60000);
}

Test 2:

// This test is identical to Test 1, except that the parent reactivates the
// initial screen buffer just before it exits.  I would have expected buffer A
// to be freed after the parent died, activating the remaining buffer B,
// because SetConsoleActiveScreenBuffer does not normally prevent a buffer from
// being freed.  The initial buffer is never freed, though.
int main() {
    Worker p;
    p.getStdout().setFirstChar('A');
    p.getStdout().trySetScreenBufferSize({ 30000, 1000 });
    p.newBuffer(false, 'B').activate();
    auto c = p.child();
    p.getStdout().activate();
    p.exit();
    Sleep(60000);
}

This behavior does not apply to Windows 7. e.g.:

int main() {
    Worker w;
    w.getStdout().setFirstChar('A');
    w.getStdout().trySetScreenBufferSize({ 30000, 1000 });
    auto b = w.newBuffer(false, 'B');
    w.getStdout().close();                 // -- L1 --
    w.getStderr().close();                 // -- L2 --
    CHECK_EQ(w.openConout().firstChar(), 'B');
    Sleep(30000);
}

If I comment out lines L1 and L2 above, then the CHECK fails, and the RAM usage of conhost.exe increases to ~60MB on Win7 32-bit. Otherwise, it's only about 1.5MB.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions