Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Main.cpp
1/*
2Copyright (C) 2016-2019,2022,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include <cstdio>
17#include <iostream>
18#include <QMessageBox>
19#include <QDebug>
20#include <mutex>
21#include "MainWindow.h"
22#include "WorldEditor.h"
23#include "aeongames/AeonEngine.hpp"
24
25#ifdef _MSC_VER
26#include <tlhelp32.h>
27#include <Dbghelp.h>
28#pragma comment(lib, "Dbghelp.lib")
29
30FILE* gAllocationLog = NULL;
31int AllocHook ( int allocType, void *userData, size_t size, int
32 blockType, long requestNumber, const unsigned char *filename, int
33 lineNumber )
34{
35 static std::mutex m;
37 if ( ( blockType == _CRT_BLOCK ) )
38 {
39 return TRUE;
40 }
41 if ( gAllocationLog && allocType != _HOOK_FREE )
42 {
43 STACKFRAME stack_frame{};
44 CONTEXT context{};
45 uint8_t symbol_buffer[sizeof ( IMAGEHLP_SYMBOL64 ) + MAX_PATH] {};
46 PIMAGEHLP_SYMBOL64 symbol = reinterpret_cast<PIMAGEHLP_SYMBOL64> ( symbol_buffer );
47 DWORD64 displacement{};
48 char name[1024] {};
49 std::lock_guard<std::mutex> hold ( m );
50
51 RtlCaptureContext ( &context );
52 stack_frame.AddrPC.Offset = context.Rip;
53 stack_frame.AddrPC.Mode = AddrModeFlat;
54 stack_frame.AddrStack.Offset = context.Rsp;
55 stack_frame.AddrStack.Mode = AddrModeFlat;
56 stack_frame.AddrFrame.Offset = context.Rbp;
57 stack_frame.AddrFrame.Mode = AddrModeFlat;
58
59 symbol->SizeOfStruct = sizeof ( IMAGEHLP_SYMBOL64 );
60 symbol->MaxNameLength = MAX_PATH;
61
62 fprintf ( gAllocationLog, "%s %ld Size %llu File %s Line %d\n",
63 ( allocType == _HOOK_ALLOC ) ? "ALLOCATION" :
64 ( allocType == _HOOK_REALLOC ) ? "REALLOCATION" : "DEALLOCATION",
65 requestNumber,
66 size, ( filename != NULL ) ? reinterpret_cast<const char*> ( filename ) : static_cast<const char*> ( "Unknown" ), lineNumber );
67
68 while ( StackWalk64 ( IMAGE_FILE_MACHINE_AMD64, GetCurrentProcess(), GetCurrentThread(), &stack_frame, &context, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL ) )
69 {
70 SymGetSymFromAddr64 ( GetCurrentProcess(), ( ULONG64 ) stack_frame.AddrPC.Offset, &displacement, symbol );
71 UnDecorateSymbolName ( symbol->Name, ( PSTR ) name, 1024, UNDNAME_COMPLETE );
72 fprintf ( gAllocationLog, "%s\n", name );
73 }
74
75 fflush ( gAllocationLog );
76 }
77 return TRUE;
78}
79#endif
80
81int ENTRYPOINT main ( int argc, char *argv[] )
82{
83#ifdef _MSC_VER
84#if 0
85 // This now works but the log gets huge and makes the process really slow to start up
86 // P.S. Looks like all leaks are related to Qt.
87 SymInitialize ( GetCurrentProcess(), nullptr, TRUE );
88 gAllocationLog = fopen ( "allocation.log", "wt" );
89 _CrtSetAllocHook ( AllocHook );
90#endif
91 /* Call _CrtDumpMemoryLeaks on exit, required because Qt does a lot of static allocations,
92 which are detected as false positives.
93 http://msdn.microsoft.com/en-us/library/5at7yxcs%28v=vs.71%29.aspx */
94 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
95 // Use _CrtSetBreakAlloc( ) to set breakpoints on allocations.
96#endif
98 {
99 return -1;
100 }
101 int retval = 0;
102 /* Insanity Check note:
103 The following context within a context of stack variables
104 works to ensure that mainWindow is destroyed before worldEditor.
105 IT SHOULD JUST WORK. IF IT BREAKS AGAIN ONLY IN MINGW64,
106 BUT NOT ON CLANG64 or UCRT64 OR VISUAL STUDIO BLAME MINGW64
107 DLLS, UPDATE THEM AND DO NOT SPEND ANY MORE TIME ON THIS,
108 AS IT IS ALL WASTED TIME. */
109 {
110 AeonGames::WorldEditor worldeditor ( argc, argv );
111 {
112 AeonGames::MainWindow mainWindow{};
113 mainWindow.showNormal();
114 retval = worldeditor.exec();
115 }
116 }
118#ifdef _MSC_VER
119#if 0
120 if ( gAllocationLog )
121 {
122 _CrtSetAllocHook ( nullptr );
123 FinalizeModuleTable();
124 fclose ( gAllocationLog );
125 gAllocationLog = NULL;
126 };
127#endif
128#endif
129 return retval;
130}
Main window of the world editor application.
Definition MainWindow.h:29
Main application class for the world editor.
Definition WorldEditor.h:39
DLL bool InitializeGlobalEnvironment(int argc=0, char *argv[]=nullptr)
Initialize the global engine environment.
DLL void FinalizeGlobalEnvironment()
Shut down the global engine environment and release resources.