Skip to content

Commit bad33e8

Browse files
authored
Merge pull request #294 from rxbryan/bug-fix1
fix segfault in metacall-ext-test
2 parents 2b1e504 + 2647969 commit bad33e8

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

source/loaders/ext_loader/source/ext_loader_impl.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <log/log.h>
3434

3535
#include <filesystem>
36+
#include <map>
3637
#include <set>
3738
#include <string>
3839
#include <vector>
@@ -63,6 +64,8 @@ union loader_impl_function_cast
6364
int (*fn)(void *, void *);
6465
};
6566

67+
static std::map<std::string, loader_impl_ext_handle_lib_type> destroy_list;
68+
6669
dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const loader_path path);
6770
int ext_loader_impl_load_from_file_handle(loader_impl_ext ext_impl, loader_impl_ext_handle ext_handle, const loader_path path);
6871
static void ext_loader_impl_destroy_handle(loader_impl_ext_handle ext_handle);
@@ -143,6 +146,14 @@ dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const l
143146

144147
int ext_loader_impl_load_from_file_handle(loader_impl_ext ext_impl, loader_impl_ext_handle ext_handle, const loader_path path)
145148
{
149+
auto iterator = destroy_list.find(path);
150+
if (iterator != destroy_list.end())
151+
{
152+
log_write("metacall", LOG_LEVEL_DEBUG, "Unloading handle: %s <%p>", iterator->second.name.c_str(), iterator->second.handle);
153+
dynlink_unload(iterator->second.handle);
154+
destroy_list.erase(path);
155+
}
156+
146157
dynlink lib = ext_loader_impl_load_from_file_dynlink(ext_impl, path);
147158

148159
if (lib == NULL)
@@ -256,7 +267,14 @@ int ext_loader_impl_clear(loader_impl impl, loader_handle handle)
256267

257268
if (ext_handle != NULL)
258269
{
259-
ext_loader_impl_destroy_handle(ext_handle);
270+
for (size_t i = 0; i < ext_handle->extensions.size(); i++)
271+
{
272+
log_write("metacall", LOG_LEVEL_DEBUG, "Storing handle: %s <%p> in destroy list", ext_handle->extensions[i].name.c_str(), ext_handle->extensions[i].handle);
273+
destroy_list[ext_handle->extensions[i].name] = ext_handle->extensions[i];
274+
ext_handle->extensions.erase(ext_handle->extensions.begin() + i);
275+
}
276+
277+
delete ext_handle;
260278

261279
return 0;
262280
}
@@ -294,6 +312,15 @@ int ext_loader_impl_destroy(loader_impl impl)
294312

295313
delete ext_impl;
296314

315+
/* Destroy all handles */
316+
if (!destroy_list.empty())
317+
{
318+
for (auto iterator : destroy_list)
319+
{
320+
log_write("metacall", LOG_LEVEL_DEBUG, "Unloading handle: %s <%p>", iterator.second.name.c_str(), iterator.second.handle);
321+
dynlink_unload(iterator.second.handle);
322+
}
323+
}
297324
return 0;
298325
}
299326

source/tests/metacall_ext_test/source/metacall_ext_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ TEST_F(metacall_ext_test, DefaultConstructor)
3535
const char *ext_scripts[] = {
3636
"sum_extension" /* The library extension (dll, so, dylib) is crossplatform so we should not add it here */
3737
};
38+
void *handle = NULL;
39+
EXPECT_EQ((int)0, (int)metacall_load_from_file("ext", ext_scripts, sizeof(ext_scripts) / sizeof(ext_scripts[0]), &handle));
40+
EXPECT_EQ((int)0, (int)metacall_clear(handle));
3841

3942
EXPECT_EQ((int)0, (int)metacall_load_from_file("ext", ext_scripts, sizeof(ext_scripts) / sizeof(ext_scripts[0]), NULL));
4043

@@ -45,7 +48,6 @@ TEST_F(metacall_ext_test, DefaultConstructor)
4548
EXPECT_EQ((long)metacall_value_to_long(ret), (long)7);
4649

4750
metacall_value_destroy(ret);
48-
4951
/* Print inspect information */
5052
{
5153
size_t size = 0;

0 commit comments

Comments
 (0)