From 6f07d042b63eead387f66ff9ce7b4ec6bfe64ccf Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Sat, 4 Jan 2025 14:27:03 +0100 Subject: [PATCH] Fix potential crash in benchmark mode with no console attached Fixes #1175 --- base/benchmark.hpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/base/benchmark.hpp b/base/benchmark.hpp index 9d742968b..2126821fb 100644 --- a/base/benchmark.hpp +++ b/base/benchmark.hpp @@ -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";