1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-16 00:03:45 +00:00

Fixed memory leak

This commit is contained in:
Bryan Roe
2020-12-18 10:42:04 -08:00
parent 081c866765
commit 5c1b44e749
4 changed files with 125 additions and 78 deletions

View File

@@ -1880,36 +1880,50 @@ End Mesh Agent Duktape Abstraction
char* MeshAgent_MakeAbsolutePathEx(char *basePath, char *localPath, int escapeBackSlash)
{
MeshAgentHostContainer *agent = ILibMemory_CanaryOK(basePath) ? ((MeshAgentHostContainer**)ILibMemory_Extra(basePath))[0] : NULL;
duk_context *ctx = (agent != NULL && agent->meshCoreCtx != NULL) ? agent->meshCoreCtx : ILibDuktape_ScriptContainer_InitializeJavaScriptEngineEx(SCRIPT_ENGINE_NONE, 0, agent!=NULL?agent->chain:NULL, NULL, NULL, agent!=NULL?agent->exePath: basePath, NULL, NULL, agent!=NULL?agent->chain:NULL);
size_t basePathLen = strnlen_s(basePath, sizeof(ILibScratchPad2) - 4);
size_t localPathLen = strnlen_s(localPath, sizeof(ILibScratchPad2) - 4);
size_t len;
char *wd;
if (duk_peval_string(ctx, "require('util-pathHelper');") == 0) // [helper]
if (agent != NULL && agent->configPathUsesCWD != 0)
{
duk_push_string(ctx, basePath); // [helper][basePath]
duk_push_string(ctx, localPath); // [helper][basePath][localPath]
duk_push_boolean(ctx, agent!=NULL?(agent->configPathUsesCWD != 0):0); // [helper][basePath][localPath][bool]
if (duk_pcall(ctx, 3) == 0) // [result]
{
if (escapeBackSlash != 0)
{
duk_string_split(ctx, -1, "\\"); // [result][array]
duk_array_join(ctx, -1, "\\\\"); // [result][array][string]
duk_remove(ctx, -2); // [result][string]
duk_remove(ctx, -2); // [string]
#ifdef WIN32
int i = ILibString_LastIndexOf(basePath, basePathLen, "\\", 1) + 1;
wd = ILibWideToUTF8((LPWSTR)ILibScratchPad2, GetCurrentDirectoryW(sizeof(ILibScratchPad2) / 2, (LPWSTR)ILibScratchPad2));
sprintf_s(ILibScratchPad2, sizeof(ILibScratchPad2), "%s\\%s", wd, basePath + i);
#else
int i = ILibString_LastIndexOf(basePath, basePathLen, "/", 1) + 1;
getcwd(ILibScratchPad, sizeof(ILibScratchPad));
sprintf_s(ILibScratchPad2, sizeof(ILibScratchPad2), "%s/%s", ILibScratchPad, basePath + i);
#endif
}
duk_size_t len;
char *buffer = Duktape_GetBuffer(ctx, -1, &len);
if (len < sizeof(ILibScratchPad2))
else
{
ILibScratchPad2[len] = 0;
memcpy_s(ILibScratchPad2, sizeof(ILibScratchPad2), buffer, len);
duk_pop(ctx); // ...
if (agent == NULL || agent->meshCoreCtx == NULL) { Duktape_SafeDestroyHeap(ctx); }
sprintf_s(ILibScratchPad2, sizeof(ILibScratchPad2), "%s", basePath);
}
len = strnlen_s(ILibScratchPad2, sizeof(ILibScratchPad2));
if (localPath[0] == '.')
{
#ifndef WIN32
sprintf_s(ILibScratchPad2 + len, sizeof(ILibScratchPad2) - len, "%s", localPath);
#else
int i = ILibString_LastIndexOf(ILibScratchPad2, len, ".", 1);
sprintf_s(ILibScratchPad2 + i, sizeof(ILibScratchPad2) - i, "%s", localPath);
#endif
}
else
{
#ifdef WIN32
int i = ILibString_LastIndexOf(ILibScratchPad2, len, "\\", 1) + 1;
#else
int i = ILibString_LastIndexOf(ILibScratchPad2, len, "/", 1) + 1;
#endif
sprintf_s(ILibScratchPad2 + i, sizeof(ILibScratchPad2) - i, "%s", localPath);
}
//printf("MeshAgent_MakeAbsolutePathEx[%s,%s] = %s\n", basePath, localPath, ILibScratchPad2);
return(ILibScratchPad2);
}
}
}
ILIBCRITICALEXITMSG(254, "PATH MANIPULATION ERROR");
}
#ifndef MICROSTACK_NOTLS

View File

@@ -1068,9 +1068,33 @@ INT_PTR CALLBACK DialogHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lP
ILibChain_PartialStart(dialogchain);
duk_context *ctx = ILibDuktape_ScriptContainer_InitializeJavaScriptEngineEx(0, 0, dialogchain, NULL, NULL, selfexe, NULL, NULL, dialogchain);
if (duk_peval_string(ctx, "require('util-language').current.toLowerCase().split('_').join('-');") == 0) { lang = (char*)duk_safe_to_string(ctx, -1); }
if (duk_peval_string(ctx, "(function foo(){return(JSON.parse(_MSH().translation));})()") == 0)
if (duk_peval_string(ctx, "(function foo(){return(JSON.parse(_MSH().translation));})()") != 0)
{
duk_push_object(ctx); // [translation]
duk_push_object(ctx); // [translation][en]
duk_push_string(ctx, "Install"); duk_put_prop_string(ctx, -2, "install");
duk_push_string(ctx, "Uninstall"); duk_put_prop_string(ctx, -2, "uninstall");
duk_push_string(ctx, "Connect"); duk_put_prop_string(ctx, -2, "connect");
duk_push_string(ctx, "Disconnect"); duk_put_prop_string(ctx, -2, "disconnect");
duk_push_string(ctx, "Update"); duk_put_prop_string(ctx, -2, "update");
duk_push_array(ctx);
duk_push_string(ctx, "NOT INSTALLED"); duk_array_push(ctx, -2);
duk_push_string(ctx, "RUNNING"); duk_array_push(ctx, -2);
duk_push_string(ctx, "NOT RUNNING"); duk_array_push(ctx, -2);
duk_put_prop_string(ctx, -2, "status");
duk_put_prop_string(ctx, -2, "en"); // [translation]
}
if (DIALOG_LANG != NULL) { lang = DIALOG_LANG; }
if (!duk_has_prop_string(ctx, -1, lang))
{
duk_push_string(ctx, lang); // [obj][string]
duk_string_split(ctx, -1, "-"); // [obj][string][array]
duk_array_shift(ctx, -1); // [obj][string][array][string]
lang = (char*)duk_safe_to_string(ctx, -1);
duk_dup(ctx, -4); // [obj][string][array][string][obj]
}
if (duk_has_prop_string(ctx, -1, lang))
{
duk_get_prop_string(ctx, -1, lang);
@@ -1117,7 +1141,7 @@ INT_PTR CALLBACK DialogHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lP
state_running = Dialog_GetTranslationEx(ctx, Duktape_GetStringPropertyIndexValue(ctx, -1, 1, NULL));
state_notrunning = Dialog_GetTranslationEx(ctx, Duktape_GetStringPropertyIndexValue(ctx, -1, 2, NULL));
}
}
fileName = MeshAgent_MakeAbsolutePath(selfexe, ".msh");
{

View File

@@ -2552,7 +2552,13 @@ extern void ILibDuktape_HttpStream_Init(duk_context *ctx);
duk_context *ILibDuktape_ScriptContainer_InitializeJavaScriptEngine_minimal()
{
ILibDuktape_ContextData *ctxd = (ILibDuktape_ContextData*)ILibMemory_SmartAllocate(sizeof(ILibDuktape_ContextData));
#ifndef MICROSTACK_NOTLS
util_openssl_init();
#endif
do { util_random(sizeof(ctxd->nonce), (char*)&(ctxd->nonce)); } while (ctxd->nonce == 0);
#ifndef MICROSTACK_NOTLS
util_openssl_uninit();
#endif
ctxd->threads = ILibLinkedList_Create();
duk_context *ctx = duk_create_heap(ILibDuktape_ScriptContainer_Engine_malloc, ILibDuktape_ScriptContainer_Engine_realloc, ILibDuktape_ScriptContainer_Engine_free, ctxd, ILibDuktape_ScriptContainer_Engine_fatal);

View File

@@ -3773,10 +3773,6 @@ void ILibChain_PartialStart(void *Chain)
#endif
}
#endif
if (chain->nowatchdog == 0)
{
chain->WatchDogThread = ILibSpawnNormalThread(ILibChain_WatchDogStart, chain);
}
#endif
//
@@ -3831,6 +3827,13 @@ ILibExportMethod void ILibStartChain(void *Chain)
FD_ZERO(&errorset);
FD_ZERO(&writeset);
#if defined(ILibChain_WATCHDOG_TIMEOUT)
if (chain->TerminateFlag == 0 && chain->nowatchdog == 0)
{
chain->WatchDogThread = ILibSpawnNormalThread(ILibChain_WatchDogStart, chain);
}
#endif
while (chain->TerminateFlag == 0)
{
slct = 0;