STOP, TRAVELER!
Stop, traveler! This soil you walk upon unknowingly
Is where an era came to an end.
Bend down and listen, this quiet mound
Is where the heart of a nation beats.
This lonely, silent, forsaken hill
Is where all thoughts of heroes turned to blood.
Drop by, press your ear to this silent mound,
Because this flood is where a nation's freedom was won.
Greetings from Türkiye
All of this belongs to Gemini.
identifying-code-errors-that-cause-cpu-usage-t7627
Based on this topic, I will show you how to fix the two bugs that cause 99% CPU freezing.
CWebBrowser
Regarding the other CPU locking issue:const auto app = new CPythonApplication;
When I changed this to:CoInitializeEx(nullptr, COINIT_MULTITHREADED);
it got fixed."
Example of a CPU locking fix.
cwebrowser
userinterface.cpp
Cod: Selectaţi tot
bool Main(HINSTANCE hInstance, LPSTR lpCmdLine)
{
#ifdef LOCALE_SERVICE_YMIR
extern bool g_isScreenShotKey;
g_isScreenShotKey = true;
#endif
const DWORD dwRandSeed=time(nullptr)+DWORD(GetCurrentProcess());
srandom(dwRandSeed);
srand(random());
SetLogLevel(1);
#ifdef LOCALE_SERVICE_VIETNAM_MILD
extern BOOL USE_VIETNAM_CONVERT_WEAPON_VNUM;
USE_VIETNAM_CONVERT_WEAPON_VNUM = true;
#endif
if (_access("perf_game_update.txt", 0)==0)
{
DeleteFile("perf_game_update.txt");
}
if (_access("newpatch.exe", 0)==0)
{
system("patchupdater.exe");
return false;
}
ilInit();
if (!Setup(lpCmdLine))
return false;
#ifdef _DEBUG
OpenConsoleWindow();
OpenLogFile(true); // true == uses syserr.txt and log.txt
#else
OpenLogFile(false); // false == uses syserr.txt only
#endif
static CLZO lzo;
static CEterPackManager EterPackManager;
if (!PackInitialize("pack"))
{
LogBox("Pack Initialization failed. Check log.txt file..");
return false;
}
if(LocaleService_LoadGlobal(hInstance))
SetDefaultCodePage(LocaleService_GetCodePage());
#ifdef ENABLE_PYLIB_CHECK
if (!__CheckPyLibFiles())
return false;
#endif
#ifdef ENABLE_MILES_CHECK
if (!__CheckMilesFiles())
return false;
#endif
// ÇÖZÜM: CPythonApplication oluşturulmadan önce COM altyapısını başlatıyoruz
// CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
// ÇÖZÜM: Mesaj pompası kilitlenmesini önlemek için MTA (Multithreaded Apartment) kullanıyoruz
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
const auto app = new CPythonApplication;
// const auto app = new CPythonApplication;
app->Initialize(hInstance);
bool ret=false;
{
CPythonLauncher pyLauncher;
CPythonExceptionSender pyExceptionSender;
SetExceptionSender(&pyExceptionSender);
if (pyLauncher.Create())
{
ret=RunMainScript(pyLauncher, lpCmdLine);
}
//ProcessScanner_ReleaseQuitEvent();
app->Clear();
timeEndPeriod(1);
pyLauncher.Clear();
}
app->Destroy();
delete app;
// COM oturumunu güvenle kapatıyoruz
CoUninitialize();
return ret;
}
Cod: Selectaţi tot
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
#ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_CRT_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
ApplicationStringTable_Initialize(hInstance);
LocaleService_LoadConfig("locale.cfg");
SetDefaultCodePage(LocaleService_GetCodePage());
#if defined(CHECK_LATEST_DATA_FILES)
if (!CheckLatestFiles())
return 0;
#endif
bool bQuit = false;
bool bAuthKeyChecked = false;
int nArgc = 0;
PCHAR* szArgv = CommandLineToArgv(lpCmdLine, &nArgc);
for (int i = 0; i < nArgc; i++) {
if (szArgv[i] == nullptr)
continue;
if (__IsLocaleVersion(szArgv[i]))
{
char szModuleName[MAX_PATH];
char szVersionPath[MAX_PATH];
GetModuleFileName(nullptr, szModuleName, sizeof(szModuleName));
sprintf(szVersionPath, "%s.version", szModuleName);
FILE* fp = fopen(szVersionPath, "wt");
if (fp)
{
extern int METIN2_GET_VERSION();
fprintf(fp, "r%d\n", METIN2_GET_VERSION());
fclose(fp);
}
bQuit = true;
}
else if (__IsLocaleOption(szArgv[i]))
{
FILE* fp = fopen("locale.txt", "wt");
fprintf(fp, "service[%s] code_page[%d]",
LocaleService_GetName(), LocaleService_GetCodePage());
fclose(fp);
bQuit = true;
}
else if (__IsTimeStampOption(szArgv[i]))
{
__PrintTimeStamp();
bQuit = true;
}
else if ((strcmp(szArgv[i], "--force-set-locale") == 0))
{
if (nArgc <= i + 2)
{
MessageBox(nullptr, "Invalid arguments", ApplicationStringTable_GetStringz(IDS_APP_NAME, "APP_NAME"), MB_ICONSTOP);
goto Clean;
}
const char* localeName = szArgv[++i];
const char* localePath = szArgv[++i];
LocaleService_ForceSetLocale(localeName, localePath);
}
}
if (bQuit)
goto Clean;
#if defined(NEEDED_COMMAND_ARGUMENT)
if (strstr(lpCmdLine, NEEDED_COMMAND_ARGUMENT) == 0) {
MessageBox(nullptr, ApplicationStringTable_GetStringz(IDS_ERR_MUST_LAUNCH_FROM_PATCHER, "ERR_MUST_LAUNCH_FROM_PATCHER"), ApplicationStringTable_GetStringz(IDS_APP_NAME, "APP_NAME"), MB_ICONSTOP);
goto Clean;
}
#endif
#if defined(NEEDED_COMMAND_CLIPBOARD)
{
CHAR szSecKey[256];
CPostIt cPostIt("VOLUME1");
if (cPostIt.Get("SEC_KEY", szSecKey, sizeof(szSecKey)) == FALSE) {
MessageBox(nullptr, ApplicationStringTable_GetStringz(IDS_ERR_MUST_LAUNCH_FROM_PATCHER, "ERR_MUST_LAUNCH_FROM_PATCHER"), ApplicationStringTable_GetStringz(IDS_APP_NAME, "APP_NAME"), MB_ICONSTOP);
goto Clean;
}
if (strstr(szSecKey, NEEDED_COMMAND_CLIPBOARD) == 0) {
MessageBox(nullptr, ApplicationStringTable_GetStringz(IDS_ERR_MUST_LAUNCH_FROM_PATCHER, "ERR_MUST_LAUNCH_FROM_PATCHER"), ApplicationStringTable_GetStringz(IDS_APP_NAME, "APP_NAME"), MB_ICONSTOP);
goto Clean;
}
cPostIt.Empty();
}
#endif
WebBrowser_Startup(hInstance);
#ifndef ENABLE_PYLIB_CHECK
if (!CheckPythonLibraryFilenames())
{
__ErrorPythonLibraryIsNotExist();
goto Clean;
}
#endif
// Çakışmaya ve kilitlenmeye yol açan dış CoInitializeEx ve CoUninitialize tamamen kaldırıldı.
Main(hInstance, lpCmdLine);
WebBrowser_Cleanup();
::CoUninitialize();
if(gs_szErrorString[0])
MessageBox(nullptr, gs_szErrorString, ApplicationStringTable_GetStringz(IDS_APP_NAME, "APP_NAME"), MB_ICONSTOP);
Clean:
SAFE_FREE_GLOBAL(szArgv);
return 0;
}Cod: Selectaţi tot
void CPythonApplication::Loop()
{
MSG msg = {};
PeekMessageW(&msg, nullptr, 0U, 0U, PM_NOREMOVE);
while (WM_QUIT != msg.message)
{
// Use PeekMessage() so we can use idle time to render the scene.
bool messageReceived = (PeekMessageW(&msg, nullptr, 0U, 0U, PM_REMOVE) != 0);
if (messageReceived)
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
else
{
rmt_BeginCPUSample(ApplicationProcess, 0);
Process();
rmt_EndCPUSample();
// ÇÖZÜM: İşlemcinin (CPU) %100 çalışıp boşa dönmesini engellemek için
// boştடa kalan zaman diliminde işlemciyi 1 milisaniye serbest bırakıyoruz.
Sleep(1);
}
}
}Cod: Selectaţi tot
long EmbedBrowserObject(HWND hwnd)
{
HRESULT hr;
LPCLASSFACTORY pClassFactory = NULL;
IOleObject* browserObject = NULL;
IWebBrowser2* webBrowser2 = NULL;
RECT rect;
char* ptr;
_IOleClientSiteEx* _iOleClientSiteEx;
if (!(ptr = (char*)GlobalAlloc(GMEM_FIXED, sizeof(_IOleClientSiteEx) + sizeof(IOleObject*))))
{
return(-1);
}
_iOleClientSiteEx = (_IOleClientSiteEx*)(ptr + sizeof(IOleObject*));
_iOleClientSiteEx->client.lpVtbl = &MyIOleClientSiteTable;
_iOleClientSiteEx->inplace.inplace.lpVtbl = &MyIOleInPlaceSiteTable;
_iOleClientSiteEx->inplace.frame.frame.lpVtbl = &MyIOleInPlaceFrameTable;
_iOleClientSiteEx->inplace.frame.window = hwnd;
_iOleClientSiteEx->ui.ui.lpVtbl = &MyIDocHostUIHandlerTable;
// DÜZELTME: HRESULT ataması parantez içine alındı ve doğru kontrol sağlantı
hr = CoGetClassObject(
&CLSID_WebBrowser,
CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
NULL,
&IID_IClassFactory,
(void**)&pClassFactory
);
if (SUCCEEDED(hr) && pClassFactory != NULL)
{
// DÜZELTME: CreateInstance dönüş değeri S_OK ile karşılaştırıldı
if (pClassFactory->lpVtbl->CreateInstance(pClassFactory, NULL, &IID_IOleObject, (void**)&browserObject) == S_OK)
{
pClassFactory->lpVtbl->Release(pClassFactory);
*((IOleObject**)ptr) = browserObject;
SetWindowLong(hwnd, GWL_USERDATA, (LONG)ptr);
if (!browserObject->lpVtbl->SetClientSite(browserObject, (IOleClientSite*)_iOleClientSiteEx))
{
browserObject->lpVtbl->SetHostNames(browserObject, L"My Host Name", 0);
GetClientRect(hwnd, &rect);
if (!OleSetContainedObject((struct IUnknown*)browserObject, TRUE) &&
!browserObject->lpVtbl->DoVerb(browserObject, OLEIVERB_SHOW, NULL, (IOleClientSite*)_iOleClientSiteEx, -1, hwnd, &rect) &&
!browserObject->lpVtbl->QueryInterface(browserObject, &IID_IWebBrowser2, (void**)&webBrowser2))
{
webBrowser2->lpVtbl->put_Left(webBrowser2, 0);
webBrowser2->lpVtbl->put_Top(webBrowser2, 0);
webBrowser2->lpVtbl->put_Width(webBrowser2, rect.right);
webBrowser2->lpVtbl->put_Height(webBrowser2, rect.bottom);
webBrowser2->lpVtbl->Release(webBrowser2);
return(0);
}
}
UnEmbedBrowserObject(hwnd);
return(-4);
}
pClassFactory->lpVtbl->Release(pClassFactory);
GlobalFree(ptr);
return(-3);
}
GlobalFree(ptr);
return(-2);
}
/****************************** WindowProc() ***************************
* Our message handler for our window to host the browser.
*/
LRESULT CALLBACK WebBrowser_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_SIZE:
{
// Resize the browser object to fit the window
//ResizeBrowser(hwnd, LOWORD(lParam), HIWORD(lParam));
return(0);
}
case WM_CREATE:
{
// Embed the browser object into our host window. We need do this only
// once. Note that the browser object will start calling some of our
// IOleInPlaceFrame and IOleClientSite functions as soon as we start
// calling browser object functions in EmbedBrowserObject().
if (EmbedBrowserObject(hwnd))
return(-1);
// Another window created with an embedded browser object
// Success
return(0);
}
case WM_DESTROY:
{
// Detach the browser object from this window, and free resources.
UnEmbedBrowserObject(hwnd);
return(TRUE);
}
}
return(DefWindowProc(hwnd, uMsg, wParam, lParam));
}
static const char* WEBBROWSER_CLASSNAME = "WEBBROWSER";
static HINSTANCE gs_hInstance = NULL;
static HWND gs_hWndWebBrowser = NULL;
static HWND gs_hWndParent = NULL;
int WebBrowser_Show(HWND hParent, const char* addr, const RECT* rc)
{
if (gs_hWndWebBrowser)
return 0;
gs_hWndParent = hParent;
gs_hWndWebBrowser = CreateWindowEx(0, WEBBROWSER_CLASSNAME, "web browser", WS_CHILD|WS_VISIBLE,
rc->left, rc->top, rc->right - rc->left, rc->bottom - rc->top,
hParent, NULL, gs_hInstance, 0);
if (!gs_hWndWebBrowser)
return 0;
DisplayHTMLPage(gs_hWndWebBrowser, (LPTSTR)addr);
ShowWindow(gs_hWndWebBrowser, SW_SHOW);
UpdateWindow(gs_hWndWebBrowser);
SetFocus(gs_hWndWebBrowser);
return 1;
}
void WebBrowser_Move(const RECT* rc)
{
MoveWindow(gs_hWndWebBrowser, rc->left, rc->top, rc->right - rc->left, rc->bottom - rc->top, 1 );
}
void WebBrowser_Hide()
{
if (!gs_hWndWebBrowser)
return;
ShowWindow(gs_hWndWebBrowser, SW_HIDE);
if (IsWindow(gs_hWndWebBrowser))
DestroyWindow(gs_hWndWebBrowser);
gs_hWndWebBrowser = NULL;
SetFocus(gs_hWndParent);
}
int WebBrowser_IsVisible()
{
return (gs_hWndWebBrowser != NULL);
}
void WebBrowser_Destroy()
{
WebBrowser_Hide();
}
int WebBrowser_Startup(HINSTANCE hInstance)
{
// COM'un daha önce başlatılıp başlatılmadığını kontrol ediyoruz
// Eğer zaten başlatıldıysa CoInitializeEx sonsuz beklemeye (hang) girebilir.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
// RPC_E_CHANGED_MODE: Thread zaten farklı bir modelle (örn. MTA) başlatılmış demektir, görmezden gelebiliriz.
// S_FALSE: Bu thread'de COM zaten daha önce başlatılmıştır.
if (FAILED(hr) && hr != RPC_E_CHANGED_MODE && hr != S_FALSE)
{
return 0;
}
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInstance;
wc.lpfnWndProc = WebBrowser_WindowProc;
wc.lpszClassName = WEBBROWSER_CLASSNAME;
RegisterClassEx(&wc);
}
gs_hInstance = hInstance;
return 1;
}
void WebBrowser_Cleanup()
{
if (gs_hInstance)
UnregisterClass(WEBBROWSER_CLASSNAME, gs_hInstance);
// CoInitializeEx karşılığı olarak CoUninitialize çağrılır
CoUninitialize();
}
1
*Link download / Code:
2
*Link scanare VirusTotal (obligatoriu):
4

