Fix memory leaks

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

View file

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