Fix memory leaks

This commit is contained in:
King_DuckZ 2022-05-15 19:19:56 +02:00
parent 4d3dfce93f
commit 692393285d
5 changed files with 24 additions and 23 deletions

View file

@ -102,12 +102,16 @@ System.print("You have %(cale.appointment_count()) appointment(s)")
char* load_module_fn(wren::VM* vm, std::string_view module_name) { char* load_module_fn(wren::VM* vm, std::string_view module_name) {
if (module_name == "calendar") { if (module_name == "calendar") {
constexpr const std::size_t buff_sz = sizeof(g_calendar_src); constexpr const std::size_t buff_sz = sizeof(g_calendar_src);
char* const buff = static_cast<char*>(MyConf::reallocate_fn(nullptr, buff_sz)); char* const buff = new char[buff_sz];
std::copy(g_calendar_src, g_calendar_src + buff_sz / sizeof(g_calendar_src[0]), buff); std::copy(g_calendar_src, g_calendar_src + buff_sz / sizeof(g_calendar_src[0]), buff);
return buff; return buff;
} }
return nullptr; return nullptr;
} }
void load_module_complete_fn(wren::VM*, std::string_view, char* buffer) {
delete[] buffer;
}
}; };
} //unnamed namespace } //unnamed namespace

View file

@ -81,9 +81,10 @@ System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>")
public: public:
char* load_module_fn(wren::VM* vm, std::string_view module_name) { char* load_module_fn(wren::VM* vm, std::string_view module_name) {
if (module_name == "math_vector") { if (module_name == "math_vector") {
constexpr const std::size_t buff_sz = g_math_vector_src.size(); constexpr const std::size_t buff_sz = g_math_vector_src.size() + 1;
char* const buff = static_cast<char*>(MyConf::reallocate_fn(nullptr, buff_sz)); char* const buff = static_cast<char*>(MyConf::reallocate_fn(nullptr, buff_sz));
std::copy(g_math_vector_src.cbegin(), g_math_vector_src.cend(), buff); std::copy(g_math_vector_src.cbegin(), g_math_vector_src.cend(), buff);
buff[buff_sz - 1] = '\0';
return buff; return buff;
} }
return nullptr; return nullptr;

View file

@ -51,7 +51,7 @@ namespace wren {
char* (*load_module_fn)(Configuration&, VM*, wren_string_t) {nullptr}; char* (*load_module_fn)(Configuration&, VM*, wren_string_t) {nullptr};
foreign_method_t (*foreign_method_fn)(Configuration&, VM*, wren_string_t, wren_string_t, bool, wren_string_t) {nullptr}; foreign_method_t (*foreign_method_fn)(Configuration&, VM*, wren_string_t, wren_string_t, bool, wren_string_t) {nullptr};
foreign_class_t (*foreign_class_fn)(Configuration&, VM*, wren_string_t, wren_string_t) {nullptr}; foreign_class_t (*foreign_class_fn)(Configuration&, VM*, wren_string_t, wren_string_t) {nullptr};
void (*load_module_complete_fn)(Configuration&, VM*, wren_string_t); void (*load_module_complete_fn)(Configuration&, VM*, wren_string_t, char*) {nullptr};
Configuration* config_obj {nullptr}; Configuration* config_obj {nullptr};
VM* owner {nullptr}; VM* owner {nullptr};
@ -130,6 +130,7 @@ namespace wren {
define_method_info(load_module_fn, load_module, char*, VM*, wren_string_t); define_method_info(load_module_fn, load_module, char*, VM*, wren_string_t);
define_method_info(foreign_method_fn, foreign_method, foreign_method_t, VM*, wren_string_t, wren_string_t, bool, wren_string_t); define_method_info(foreign_method_fn, foreign_method, foreign_method_t, VM*, wren_string_t, wren_string_t, bool, wren_string_t);
define_method_info(foreign_class_fn, foreign_class, foreign_class_t, VM*, wren_string_t, wren_string_t); define_method_info(foreign_class_fn, foreign_class, foreign_class_t, VM*, wren_string_t, wren_string_t);
define_method_info(load_module_complete_fn, load_module_complete, void, VM*, wren_string_t, char*);
template <typename T, typename F> struct AnyFunctionWrap; template <typename T, typename F> struct AnyFunctionWrap;
template <typename T, typename R, typename... Args> template <typename T, typename R, typename... Args>
@ -185,6 +186,9 @@ namespace wren {
if constexpr (detail::method_foreign_class::exists<T>::value) if constexpr (detail::method_foreign_class::exists<T>::value)
ret.foreign_class_fn = &detail::AnyFunctionWrap<T, decltype(&T::foreign_class_fn)>::template call<&T::foreign_class_fn>; ret.foreign_class_fn = &detail::AnyFunctionWrap<T, decltype(&T::foreign_class_fn)>::template call<&T::foreign_class_fn>;
if constexpr (detail::method_load_module_complete::exists<T>::value)
ret.load_module_complete_fn = &detail::AnyFunctionWrap<T, decltype(&T::load_module_complete_fn)>::template call<&T::load_module_complete_fn>;
return ret; return ret;
} }

View file

@ -21,6 +21,7 @@
#include "wrenpp/vm.hpp" #include "wrenpp/vm.hpp"
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <cstdlib>
namespace wren { namespace wren {
namespace { namespace {
@ -48,25 +49,12 @@ namespace wren {
} }
void* DefConfiguration::reallocate_fn (void* ptr, std::size_t size) { void* DefConfiguration::reallocate_fn (void* ptr, std::size_t size) {
if (ptr and size) { if (0 == size) {
//reallocate std::free(ptr);
char* const old_mem = static_cast<char*>(ptr); return nullptr;
char* const new_mem = new char[size];
std::copy(old_mem, old_mem + size, new_mem);
delete[] old_mem;
return new_mem;
} }
else if (ptr and not size) {
//free return std::realloc(ptr, size);
char* const old_mem = static_cast<char*>(ptr);
delete[] old_mem;
}
else if (not ptr and size) {
//allocate
char* const new_mem = new char[size];
return new_mem;
}
return nullptr;
} }
foreign_method_t DefConfiguration::foreign_method_fn (VM* vm, wren_string_t module_name, wren_string_t class_name, bool is_static, wren_string_t signature) { foreign_method_t DefConfiguration::foreign_method_fn (VM* vm, wren_string_t module_name, wren_string_t class_name, bool is_static, wren_string_t signature) {

View file

@ -77,7 +77,7 @@ namespace wren {
return ::WrenLoadModuleResult{ return ::WrenLoadModuleResult{
.source = source, .source = source,
.onComplete = (cb->load_module_complete_fn ? &load_module_complete_fn : nullptr), .onComplete = (cb->load_module_complete_fn ? &load_module_complete_fn : nullptr),
.userData = user_data .userData = nullptr
}; };
} }
@ -102,7 +102,11 @@ namespace wren {
assert(cb); assert(cb);
assert(cb->load_module_complete_fn and cb->config_obj and cb->owner); assert(cb->load_module_complete_fn and cb->config_obj and cb->owner);
(*cb->load_module_complete_fn)(*cb->config_obj, cb->owner, name); //Documentation says result is the same object we returned from
//load_module_fn. It should then be safe to const_cast source,
//since it wasn't const when we got it there
char* const non_const_buff = const_cast<char*>(result.source);
(*cb->load_module_complete_fn)(*cb->config_obj, cb->owner, name, non_const_buff);
} }
WrenForeignClassMethods foreign_class_fn (WrenVM* wvm, const char* module, const char* class_name) { WrenForeignClassMethods foreign_class_fn (WrenVM* wvm, const char* module, const char* class_name) {