mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-11 19:04:08 +00:00
fix(windows): release FFmpeg memory and capture native crashes
This commit is contained in:
1 parent
89dfbe4c45
commit
89bd3f3af0
17 files changed
+486
-17
No files matched your search
@@ -7,6 +7,7 @@ project(runner LANGUAGES CXX)
|
||||
#
|
||||
# Any new source files that you add to the application should be added here.
|
||||
add_executable(${BINARY_NAME} WIN32
|
||||
"crash_handler.cpp"
|
||||
"flutter_window.cpp"
|
||||
"main.cpp"
|
||||
"utils.cpp"
|
||||
@@ -34,6 +35,7 @@ target_compile_definitions(${BINARY_NAME} PRIVATE "NOMINMAX")
|
||||
# dependencies here.
|
||||
target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app)
|
||||
target_link_libraries(${BINARY_NAME} PRIVATE "dwmapi.lib")
|
||||
target_link_libraries(${BINARY_NAME} PRIVATE "dbghelp.lib")
|
||||
target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}")
|
||||
|
||||
# Run the Flutter tool portions of the build. This must not be removed.
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# Windows native crash diagnostics
|
||||
|
||||
The runner installs diagnostics before starting Flutter and plugins. Files are
|
||||
written beside the executable under `storage/logs`, matching Windows app storage:
|
||||
|
||||
- `native-crash.log`: UTC timestamp, app version, process/thread, exception code,
|
||||
faulting module/address/offset, and minidump success or the Windows error code.
|
||||
- `native-crash.dmp`: the most recent native crash, including thread stacks,
|
||||
loaded modules and unloaded module information.
|
||||
- `crash.log`: Go runtime crash output, kept separately from native diagnostics.
|
||||
|
||||
Logs are appended and existing dumps survive normal restarts. A subsequent native
|
||||
crash replaces the previous dump. Dump generation is best effort, particularly
|
||||
for stack exhaustion, heap corruption or unavailable disk space. Process kills
|
||||
and fail-fast termination can bypass exception handlers.
|
||||
|
||||
The top-level unhandled filter covers native exceptions on process threads. An
|
||||
SEH boundary inside the runner's window callback records the original escaping exception
|
||||
before Windows wraps a window callback failure as `0xC000041D`. A vectored handler
|
||||
also observes that fatal callback status, since it can bypass the unhandled
|
||||
filter. All handlers continue exception processing; they do not attempt recovery
|
||||
or log ordinary handled first-chance exceptions.
|
||||
|
||||
On Windows, verify a release build by exercising tray clicks, Explorer restart,
|
||||
system font changes and child-window open/close. In a disposable test build,
|
||||
raise an unhandled access violation inside a window callback and on a worker
|
||||
thread, then raise `0xC000041D`; check that the log and dump are written and
|
||||
survive relaunch. A debugger may intercept these before application handlers.
|
||||
Open the dump with WinDbg and matching runner/Flutter/plugin PDBs to locate the
|
||||
faulting stack. Minidumps can contain application data; inspect them before
|
||||
sharing.
|
||||
@@ -0,0 +1,119 @@
|
||||
#include "crash_handler.h"
|
||||
|
||||
#include <dbghelp.h>
|
||||
#include <strsafe.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
namespace {
|
||||
|
||||
HANDLE crash_log = INVALID_HANDLE_VALUE;
|
||||
std::wstring dump_path;
|
||||
volatile LONG recording = 0;
|
||||
LPTOP_LEVEL_EXCEPTION_FILTER previous_filter = nullptr;
|
||||
|
||||
void WriteLog(const char* text) {
|
||||
if (crash_log == INVALID_HANDLE_VALUE) return;
|
||||
DWORD written = 0;
|
||||
WriteFile(crash_log, text, static_cast<DWORD>(strlen(text)), &written,
|
||||
nullptr);
|
||||
FlushFileBuffers(crash_log);
|
||||
}
|
||||
|
||||
LONG WINAPI UnhandledException(EXCEPTION_POINTERS* exception) {
|
||||
RecordNativeCrash(exception);
|
||||
return previous_filter ? previous_filter(exception)
|
||||
: EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
LONG CALLBACK FatalCallbackException(EXCEPTION_POINTERS* exception) {
|
||||
// Windows can terminate on this callback exception without invoking the
|
||||
// process's unhandled filter. Ignore ordinary first-chance exceptions (Go,
|
||||
// Dart and plugins may handle those) and observe only this fatal status.
|
||||
if (exception->ExceptionRecord->ExceptionCode == 0xC000041D) {
|
||||
RecordNativeCrash(exception);
|
||||
}
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
LONG RecordNativeCrash(EXCEPTION_POINTERS* exception) {
|
||||
if (!exception || InterlockedCompareExchange(&recording, 1, 0) != 0) {
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
SYSTEMTIME time;
|
||||
GetSystemTime(&time);
|
||||
const auto* record = exception->ExceptionRecord;
|
||||
HMODULE module = nullptr;
|
||||
wchar_t module_path[MAX_PATH] = L"unknown";
|
||||
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
|
||||
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
||||
reinterpret_cast<LPCWSTR>(record->ExceptionAddress),
|
||||
&module);
|
||||
if (module) GetModuleFileNameW(module, module_path, MAX_PATH);
|
||||
|
||||
char line[2048] = {};
|
||||
StringCchPrintfA(
|
||||
line, ARRAYSIZE(line),
|
||||
"\r\n%04u-%02u-%02uT%02u:%02u:%02uZ Gopeed %s pid=%lu tid=%lu "
|
||||
"exception=0x%08lX address=%p module=%ls offset=0x%llX\r\n",
|
||||
time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute,
|
||||
time.wSecond, FLUTTER_VERSION, GetCurrentProcessId(), GetCurrentThreadId(),
|
||||
record->ExceptionCode, record->ExceptionAddress, module_path,
|
||||
static_cast<unsigned long long>(
|
||||
reinterpret_cast<uintptr_t>(record->ExceptionAddress) -
|
||||
reinterpret_cast<uintptr_t>(module)));
|
||||
WriteLog(line);
|
||||
|
||||
HANDLE dump = CreateFileW(dump_path.c_str(), GENERIC_WRITE, 0, nullptr,
|
||||
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (dump != INVALID_HANDLE_VALUE) {
|
||||
MINIDUMP_EXCEPTION_INFORMATION info = {};
|
||||
info.ThreadId = GetCurrentThreadId();
|
||||
info.ExceptionPointers = exception;
|
||||
info.ClientPointers = FALSE;
|
||||
const BOOL ok = MiniDumpWriteDump(
|
||||
GetCurrentProcess(), GetCurrentProcessId(), dump,
|
||||
static_cast<MINIDUMP_TYPE>(MiniDumpNormal | MiniDumpWithThreadInfo |
|
||||
MiniDumpWithUnloadedModules),
|
||||
&info, nullptr, nullptr);
|
||||
const DWORD error = ok ? ERROR_SUCCESS : GetLastError();
|
||||
FlushFileBuffers(dump);
|
||||
CloseHandle(dump);
|
||||
StringCchPrintfA(line, ARRAYSIZE(line), "minidump success=%d error=%lu\r\n",
|
||||
ok, error);
|
||||
WriteLog(line);
|
||||
} else {
|
||||
StringCchPrintfA(line, ARRAYSIZE(line), "minidump open error=%lu\r\n",
|
||||
GetLastError());
|
||||
WriteLog(line);
|
||||
}
|
||||
|
||||
// Do not swallow an exception or resume a potentially corrupted process.
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
void InstallCrashHandler() {
|
||||
// Match Dart's Windows storage path independently of the working directory.
|
||||
wchar_t executable[32768] = {};
|
||||
const DWORD length = GetModuleFileNameW(nullptr, executable, ARRAYSIZE(executable));
|
||||
if (length == 0 || length >= ARRAYSIZE(executable)) return;
|
||||
const auto directory = std::filesystem::path(executable).parent_path() /
|
||||
L"storage" / L"logs";
|
||||
std::error_code error;
|
||||
std::filesystem::create_directories(directory, error);
|
||||
if (error) return;
|
||||
|
||||
dump_path = (directory / L"native-crash.dmp").wstring();
|
||||
crash_log = CreateFileW((directory / L"native-crash.log").c_str(),
|
||||
FILE_APPEND_DATA, FILE_SHARE_READ, nullptr, OPEN_ALWAYS,
|
||||
FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
previous_filter = SetUnhandledExceptionFilter(UnhandledException);
|
||||
AddVectoredExceptionHandler(1, FatalCallbackException);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef RUNNER_CRASH_HANDLER_H_
|
||||
#define RUNNER_CRASH_HANDLER_H_
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
// Installs best-effort native diagnostics before Flutter or plugins start.
|
||||
void InstallCrashHandler();
|
||||
|
||||
// Records the exception and continues normal Windows exception processing.
|
||||
LONG RecordNativeCrash(EXCEPTION_POINTERS* exception);
|
||||
|
||||
#endif // RUNNER_CRASH_HANDLER_H_
|
||||
@@ -63,7 +63,9 @@ FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
|
||||
|
||||
switch (message) {
|
||||
case WM_FONTCHANGE:
|
||||
flutter_controller_->engine()->ReloadSystemFonts();
|
||||
if (flutter_controller_ && flutter_controller_->engine()) {
|
||||
flutter_controller_->engine()->ReloadSystemFonts();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <desktop_multi_window/desktop_multi_window_plugin.h>
|
||||
|
||||
#include "app_links/app_links_plugin_c_api.h"
|
||||
#include "crash_handler.h"
|
||||
#include "flutter_window.h"
|
||||
#include "flutter/generated_plugin_registrant.h"
|
||||
#include "utils.h"
|
||||
@@ -43,6 +44,7 @@ bool IsMultiWindowLaunch(const std::vector<std::string>& arguments) {
|
||||
|
||||
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
|
||||
_In_ wchar_t *command_line, _In_ int show_command) {
|
||||
InstallCrashHandler();
|
||||
// Attach to console when present (e.g., 'flutter run') or create a
|
||||
// new console when running with a debugger.
|
||||
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
|
||||
@@ -77,11 +79,12 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
|
||||
window.SetQuitOnClose(true);
|
||||
|
||||
::MSG msg;
|
||||
while (::GetMessage(&msg, nullptr, 0, 0)) {
|
||||
BOOL message_result;
|
||||
while ((message_result = ::GetMessage(&msg, nullptr, 0, 0)) > 0) {
|
||||
::TranslateMessage(&msg);
|
||||
::DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
::CoUninitialize();
|
||||
return EXIT_SUCCESS;
|
||||
return message_result == -1 ? EXIT_FAILURE : static_cast<int>(msg.wParam);
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <dwmapi.h>
|
||||
#include <flutter_windows.h>
|
||||
|
||||
#include "crash_handler.h"
|
||||
#include "resource.h"
|
||||
|
||||
namespace {
|
||||
@@ -158,19 +159,26 @@ LRESULT CALLBACK Win32Window::WndProc(HWND const window,
|
||||
UINT const message,
|
||||
WPARAM const wparam,
|
||||
LPARAM const lparam) noexcept {
|
||||
if (message == WM_NCCREATE) {
|
||||
auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
|
||||
SetWindowLongPtr(window, GWLP_USERDATA,
|
||||
reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams));
|
||||
// Capture inside the callback, before user32 wraps or suppresses an escaping
|
||||
// exception. Keep C++ objects requiring unwinding out of this SEH boundary.
|
||||
__try {
|
||||
if (message == WM_NCCREATE) {
|
||||
auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
|
||||
SetWindowLongPtr(window, GWLP_USERDATA,
|
||||
reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams));
|
||||
|
||||
auto that = static_cast<Win32Window*>(window_struct->lpCreateParams);
|
||||
EnableFullDpiSupportIfAvailable(window);
|
||||
that->window_handle_ = window;
|
||||
} else if (Win32Window* that = GetThisFromHandle(window)) {
|
||||
return that->MessageHandler(window, message, wparam, lparam);
|
||||
auto that = static_cast<Win32Window*>(window_struct->lpCreateParams);
|
||||
EnableFullDpiSupportIfAvailable(window);
|
||||
that->window_handle_ = window;
|
||||
} else if (Win32Window* that = GetThisFromHandle(window)) {
|
||||
return that->MessageHandler(window, message, wparam, lparam);
|
||||
}
|
||||
|
||||
return DefWindowProc(window, message, wparam, lparam);
|
||||
} __except (RecordNativeCrash(GetExceptionInformation())) {
|
||||
// The filter always continues the exception search; this is unreachable.
|
||||
return 0;
|
||||
}
|
||||
|
||||
return DefWindowProc(window, message, wparam, lparam);
|
||||
}
|
||||
|
||||
LRESULT
|
||||
|
||||
Reference in new issue
Block a user