Skip to content

Commit 8ae04f9

Browse files
committed
Trying to solve memory leaks on C# loader.
1 parent e859756 commit 8ae04f9

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
lines changed

source/loaders/cs_loader/netcore/source/MetacallEntryPoint.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ public static bool ExecutionPathW([System.Runtime.InteropServices.MarshalAs(Syst
5959

6060
public static bool Load(string source)
6161
{
62-
try {
62+
try
63+
{
6364
return loader.LoadFromSourceFunctions(new string[] { source });
64-
} catch (Exception ex) {
65+
}
66+
catch (Exception ex)
67+
{
6568
// TODO: Implement error handling
6669
log.Error(ex.Message, ex);
6770
return false;
@@ -70,9 +73,12 @@ public static bool Load(string source)
7073

7174
public static bool Load(string[] files)
7275
{
73-
try {
76+
try
77+
{
7478
return loader.LoadFromFileFunctions(files);
75-
} catch (Exception ex) {
79+
}
80+
catch (Exception ex)
81+
{
7682
// TODO: Implement error handling
7783
log.Error(ex.Message, ex);
7884
return false;
@@ -176,5 +182,16 @@ public static bool LoadFromAssemblyW([System.Runtime.InteropServices.MarshalAs(S
176182
{
177183
return LoadFromAssembly(assemblyFile);
178184
}
185+
186+
// TODO: Is this needed?
187+
/*
188+
public static void Destroy()
189+
{
190+
loader = null;
191+
log = null;
192+
GC.Collect();
193+
GC.WaitForPendingFinalizers();
194+
}
195+
*/
179196
}
180-
}
197+
}

source/loaders/cs_loader/netcore/source/Providers/LoaderBase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ protected LoaderBase(ILog log)
2121
this.log = log;
2222
}
2323

24+
~LoaderBase()
25+
{
26+
paths.Clear();
27+
}
28+
2429
public bool ExecutionPath(string path)
2530
{
2631
if (!paths.Contains(path))
@@ -71,7 +76,7 @@ private string LoadFromFileFunctionsRelative(string file)
7176
{
7277
return System.IO.File.ReadAllText(Path.Combine(path, file));
7378
}
74-
catch (Exception ex)
79+
catch
7580
{
7681
continue;
7782
}

source/loaders/cs_loader/source/cs_loader_impl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <cs_loader/simple_netcore.h>
3636
#include <stdlib.h>
3737

38+
// TODO: This design has to change, it needs an AppDomain per list of functions (https://github.com/metacall/core/issues/123)
39+
3840
typedef struct
3941
{
4042
netcore_handle handle;
@@ -63,6 +65,7 @@ function_return function_cs_interface_invoke(function func, function_impl impl,
6365
}
6466
else
6567
{
68+
// TODO: Do not hardcode this, take the information from the function and (probably) preload the list of arguments
6669
parameters params[10];
6770

6871
for (int i = 0; i < cs_f->func->param_count; ++i)

source/loaders/cs_loader/source/netcore_linux.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ netcore_linux::netcore_linux(char *dotnet_root, char *dotnet_loader_assembly_pat
5555

5656
netcore_linux::~netcore_linux()
5757
{
58-
//this->stop();
58+
this->stop();
5959
}
6060

6161
bool netcore_linux::ConfigAssemblyName()
@@ -280,11 +280,9 @@ bool netcore_linux::start()
280280

281281
void netcore_linux::stop()
282282
{
283-
int status = -1;
284-
285-
status = (*this->coreclr_shutdown)(this->hostHandle, this->domainId);
283+
int status = (*this->coreclr_shutdown)(this->hostHandle, this->domainId);
286284

287-
if (status < 0)
285+
if (status != 0)
288286
{
289287
log_write("metacall", LOG_LEVEL_ERROR, "Stop status (0x%08x)", status);
290288
}

source/loaders/cs_loader/source/simple_netcore.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ reflect_function *simple_netcore_get_functions(netcore_handle handle, int *count
5454
return core->get_functions(count);
5555
}
5656

57-
void simple_netcore_destroy(netcore_handle handle)
58-
{
59-
netcore *core = (netcore *)handle;
60-
delete core;
61-
}
62-
6357
int simple_netcore_load_script_from_files(netcore_handle handle, const char *files[MAX_FILES], size_t size)
6458
{
6559
netcore *core = (netcore *)handle;
@@ -110,3 +104,14 @@ void simple_netcore_destroy_execution_result(netcore_handle handle, execution_re
110104

111105
core->destroy_execution_result(er);
112106
}
107+
108+
void simple_netcore_destroy(netcore_handle handle)
109+
{
110+
#if defined(__linux) | defined(linux)
111+
netcore_linux *netcore_impl = (netcore_linux *)handle;
112+
#else
113+
netcore_win *netcore_impl = (netcore_win *)handle;
114+
#endif
115+
116+
delete netcore_impl;
117+
}

source/tests/metacall_cs_test/source/metacall_cs_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,14 @@ TEST_F(metacall_cs_test, Fail)
9898

9999
EXPECT_EQ((int)1, (int)metacall_load_from_memory("cs", buffer, sizeof(buffer), NULL));
100100
}
101+
102+
TEST_F(metacall_cs_test, FailRelativePath)
103+
{
104+
/* TODO: Review source/ports/node_port/test/index.js in the fail/require section for more information */
105+
/* This test is not reproducing the same bug (segmentation fault), we should investigate why */
106+
static const char *scripts[] = {
107+
"./asd.invalid"
108+
};
109+
110+
EXPECT_EQ((int)1, (int)metacall_load_from_file("cs", scripts, sizeof(scripts) / sizeof(scripts[0]), NULL));
111+
}

0 commit comments

Comments
 (0)