Ensure memory is handled properly with userdata
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-14 17:35:05 +02:00
parent 3217fd1479
commit 2c313791d9
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 19 additions and 11 deletions

View File

@ -8,7 +8,7 @@
#include "UserDataStorage.hpp"
class UserDataScriptType : public ScriptType{
UserData* _userData;
shared_ptr<UserData> _userData;
public:
explicit UserDataScriptType(int id) : ScriptType(TypeClass::UserData){
_userData = UserDataStorage::GetUserDataType(id);

View File

@ -1,4 +1,4 @@
#include "UserDataStorage.hpp"
std::unordered_map<int, UserData*> UserDataStorage::_userData = {};
UserDataStorage::_internalDataStorage UserDataStorage::_internal = UserDataStorage::_internalDataStorage();

View File

@ -6,14 +6,19 @@
#include "UserData.hpp"
class UserDataStorage {
static std::unordered_map<int, UserData*> _userData;
class _internalDataStorage{
public:
std::unordered_map<int, shared_ptr<UserData>> _userData;
};
static _internalDataStorage _internal;
public:
static void RegisterType(int i, UserData* ud){
UserDataStorage::_userData.insert({i, ud});
UserDataStorage::_internal._userData.insert({i, shared_ptr<UserData>(ud)});
}
static UserData* GetUserDataType(int i){
return UserDataStorage::_userData[i];
static shared_ptr<UserData> GetUserDataType(int i){
return UserDataStorage::_internal._userData[i];
}
};

View File

@ -1,18 +1,19 @@
#ifndef PORYGONLANG_USERDATAVALUE_HPP
#define PORYGONLANG_USERDATAVALUE_HPP
#include <utility>
#include "../Evaluator/EvalValues/EvalValue.hpp"
#include "UserData.hpp"
#include "UserDataStorage.hpp"
class UserDataValue : public EvalValue{
UserData* _userData;
shared_ptr<UserData> _userData;
void* _obj;
public:
UserDataValue(UserData* userData, void* obj){
_userData = userData;
UserDataValue(shared_ptr<UserData> userData, void* obj){
_userData = std::move(userData);
_obj = obj;
}

View File

@ -56,8 +56,10 @@ end
auto obj = new UserDataTestObject();
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), obj);
script->CallFunction("testFunc", {parameter});
REQUIRE(obj->foo == 5000);
delete script;
REQUIRE(obj->foo == 5000);
delete obj;
delete parameter;
}