Fix potential crash in benchmark mode with no console attached

Fixes #1175
This commit is contained in:
Sascha Willems 2025-01-04 14:27:03 +01:00
parent 025394453d
commit 6f07d042b6

View file

@ -1,7 +1,7 @@
/*
* Benchmark class - Can be used for automated benchmarks
*
* Copyright (C) 2016-2024 by Sascha Willems - www.saschawillems.de
* Copyright (C) 2016-2025 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
@ -36,12 +36,15 @@ namespace vks
active = true;
this->deviceProps = deviceProps;
#if defined(_WIN32)
AttachConsole(ATTACH_PARENT_PROCESS);
freopen_s(&stream, "CONOUT$", "w+", stdout);
freopen_s(&stream, "CONOUT$", "w+", stderr);
bool consoleAttached = AttachConsole(ATTACH_PARENT_PROCESS);
if (!consoleAttached) {
consoleAttached = AttachConsole(GetCurrentProcessId());
}
if (consoleAttached) {
freopen_s(&stream, "CONOUT$", "w+", stdout);
freopen_s(&stream, "CONOUT$", "w+", stderr);
}
#endif
std::cout << std::fixed << std::setprecision(3);
// Warm up phase to get more stable frame rates
{
double tMeasured = 0.0;
@ -64,7 +67,8 @@ namespace vks
frameCount++;
if (outputFrames != -1 && outputFrames == frameCount) break;
};
std::cout << "Benchmark finished" << "\n";
std::cout << std::fixed << std::setprecision(3);
std::cout << "Benchmark finished\n";
std::cout << "device : " << deviceProps.deviceName << " (driver version: " << deviceProps.driverVersion << ")" << "\n";
std::cout << "runtime: " << (runtime / 1000.0) << "\n";
std::cout << "frames : " << frameCount << "\n";