Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks. #625

Merged
merged 6 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/Plasma/CoreLib/hsRefCnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class hsRefCnt {
dst = src; \
} while (0)

struct hsStealRef_Type {};
constexpr hsStealRef_Type hsStealRef;

template <class _Ref>
class hsRef
Expand All @@ -86,6 +88,7 @@ class hsRef
hsRef() : fObj(nullptr) { }
hsRef(std::nullptr_t) : fObj(nullptr) { }
hsRef(_Ref *obj) : fObj(obj) { if (fObj) fObj->Ref(); }
hsRef(_Ref *obj, hsStealRef_Type) : fObj(obj) { }
hsRef(const hsRef<_Ref> &copy) : fObj(copy.fObj) { if (fObj) fObj->Ref(); }
hsRef(hsRef<_Ref> &&move) : fObj(move.fObj) { move.fObj = nullptr; }

Expand Down Expand Up @@ -132,6 +135,13 @@ class hsRef
_Ref *operator->() const { return fObj; }
operator _Ref *() const { return fObj; }

void Steal(_Ref *obj)
{
if (fObj)
fObj->UnRef();
fObj = obj;
}

private:
_Ref *fObj;
};
Expand Down
14 changes: 5 additions & 9 deletions Sources/Plasma/FeatureLib/pfPasswordStore/pfPasswordStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,17 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
/* Get the pfPasswordStore instance */
pfPasswordStore* pfPasswordStore::Instance()
{
static pfPasswordStore* store = nullptr;

if (store == nullptr) {
#if defined(HS_BUILD_FOR_WIN32)
store = new pfWin32PasswordStore();
static pfWin32PasswordStore store;
#elif defined(HS_BUILD_FOR_APPLE)
store = new pfApplePasswordStore();
static pfApplePasswordStore store;
#elif defined(HAVE_LIBSECRET)
store = new pfUnixPasswordStore();
static pfUnixPasswordStore store;
#else
store = new pfFilePasswordStore();
static pfFilePasswordStore store;
#endif
}

return store;
return &store;
}
Hoikas marked this conversation as resolved.
Show resolved Hide resolved


Expand Down
1 change: 1 addition & 0 deletions Sources/Plasma/FeatureLib/pfPython/Pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include <exception>
#include <functional>
#include <locale>
#include <memory>
#include <string>

// Platform Library Includes
Expand Down
12 changes: 12 additions & 0 deletions Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ struct pythonClassName \
// This makes sure that our python new function can access our constructors
#define PYTHON_CLASS_NEW_FRIEND(pythonClassName) friend PyObject *pythonClassName##_new(PyTypeObject *type, PyObject *args, PyObject *keywords)

#define PYTHON_CLASS_VAULT_NODE_NEW_DEFINITION \
static PyObject* New(hsRef<RelVaultNode> vaultNode=nullptr);

// This defines the basic new function for a class
#define PYTHON_CLASS_NEW_DEFINITION static PyObject *New()

Expand All @@ -90,6 +93,15 @@ PyObject *glueClassName::New() \
return (PyObject*)newObj; \
}

#define PYTHON_CLASS_VAULT_NODE_NEW_IMPL(pythonClassName, glueClassName) \
PyObject* glueClassName::New(hsRef<RelVaultNode> nfsNode) \
{ \
pythonClassName* newObj = (pythonClassName*)pythonClassName##_type.tp_new(&pythonClassName##_type, nullptr, nullptr); \
if (nfsNode) \
newObj->fThis->fNode = std::move(nfsNode); \
return (PyObject*)newObj; \
}

// This defines the basic check function for a class
#define PYTHON_CLASS_CHECK_DEFINITION static bool Check(PyObject *obj)

Expand Down
92 changes: 38 additions & 54 deletions Sources/Plasma/FeatureLib/pfPython/pyVault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
//
//////////////////////////////////////////////////////////////////////

#include <memory>
#include <Python.h>
#pragma hdrstop

Expand Down Expand Up @@ -80,35 +81,29 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com

//============================================================================
static PyObject * GetFolder (unsigned folderType) {
PyObject * result = nil;
if (hsRef<RelVaultNode> rvnPlr = VaultGetPlayerNode()) {
if (hsRef<RelVaultNode> rvnFldr = rvnPlr->GetChildFolderNode(folderType, 1))
result = pyVaultFolderNode::New(rvnFldr);
return pyVaultFolderNode::New(rvnFldr);
}

return result;
return nullptr;
}

//============================================================================
static PyObject * GetPlayerInfoList (unsigned folderType) {
PyObject * result = nil;
if (hsRef<RelVaultNode> rvnPlr = VaultGetPlayerNode()) {
if (hsRef<RelVaultNode> rvnFldr = rvnPlr->GetChildPlayerInfoListNode(folderType, 1))
result = pyVaultPlayerInfoListNode::New(rvnFldr);
return pyVaultPlayerInfoListNode::New(rvnFldr);
}

return result;
return nullptr;
}

//============================================================================
static PyObject * GetAgeInfoList (unsigned folderType) {
PyObject * result = nil;
if (hsRef<RelVaultNode> rvnPlr = VaultGetPlayerNode()) {
if (hsRef<RelVaultNode> rvnFldr = rvnPlr->GetChildAgeInfoListNode(folderType, 1))
result = pyVaultAgeInfoListNode::New(rvnFldr);
return pyVaultAgeInfoListNode::New(rvnFldr);
}

return result;
return nullptr;
}

//////////////////////////////////////////////////
Expand All @@ -119,11 +114,11 @@ PyObject* pyVault::GetPlayerInfo()
if (hsRef<RelVaultNode> rvnPlrInfo = rvnPlr->GetChildNode(plVault::kNodeType_PlayerInfo, 1))
result = pyVaultPlayerInfoNode::New(rvnPlrInfo);
}

// just return an empty node
if (!result)
result = pyVaultPlayerInfoNode::New(nil);
result = pyVaultPlayerInfoNode::New();

return result;
}

Expand Down Expand Up @@ -182,8 +177,8 @@ PyObject* pyVault::GetAgeJournalsFolder()

// just return an empty node
if (!result)
result = pyVaultFolderNode::New(nil);
result = pyVaultFolderNode::New();

return result;
}

Expand Down Expand Up @@ -395,34 +390,25 @@ PyObject* pyVault::GetInviteFolder()

PyObject* pyVault::GetPsnlAgeSDL() const
{
PyObject * result = nil;
hsRef<NetVaultNode> templateNode = new NetVaultNode;

if (hsRef<RelVaultNode> rvnFldr = VaultGetAgesIOwnFolder()) {

templateNode->SetNodeType(plVault::kNodeType_AgeInfo);
VaultAgeInfoNode ageInfo(templateNode);
NetVaultNode templateNode;
templateNode.SetNodeType(plVault::kNodeType_AgeInfo);
VaultAgeInfoNode ageInfo(&templateNode);
ageInfo.SetAgeFilename(kPersonalAgeFilename);

if (hsRef<RelVaultNode> rvnInfo = rvnFldr->GetChildNode(templateNode, 2)) {
templateNode->Clear();
templateNode->SetNodeType(plVault::kNodeType_SDL);
if (hsRef<RelVaultNode> rvnInfo = rvnFldr->GetChildNode(&templateNode, 2)) {
templateNode.Clear();
templateNode.SetNodeType(plVault::kNodeType_SDL);

if (hsRef<RelVaultNode> rvnSdl = rvnInfo->GetChildNode(templateNode, 1)) {
if (hsRef<RelVaultNode> rvnSdl = rvnInfo->GetChildNode(&templateNode, 1)) {
VaultSDLNode sdl(rvnSdl);
plStateDataRecord * rec = new plStateDataRecord;
if (sdl.GetStateDataRecord(rec, plSDL::kKeepDirty))
result = pySDLStateDataRecord::New(rec);
else
delete rec;
auto rec = std::make_unique<plStateDataRecord>();
if (sdl.GetStateDataRecord(rec.get(), plSDL::kKeepDirty))
return pySDLStateDataRecord::New(rec.release());
}
}
}

if (!result)
PYTHON_RETURN_NONE;

return result;
PYTHON_RETURN_NONE;
}

void pyVault::UpdatePsnlAgeSDL( pySDLStateDataRecord & pyrec )
Expand All @@ -431,19 +417,17 @@ void pyVault::UpdatePsnlAgeSDL( pySDLStateDataRecord & pyrec )
if ( !rec )
return;

hsRef<NetVaultNode> templateNode = new NetVaultNode;

NetVaultNode templateNode;
if (hsRef<RelVaultNode> rvnFldr = VaultGetAgesIOwnFolder()) {

templateNode->SetNodeType(plVault::kNodeType_AgeInfo);
VaultAgeInfoNode ageInfo(templateNode);
templateNode.SetNodeType(plVault::kNodeType_AgeInfo);
VaultAgeInfoNode ageInfo(&templateNode);
ageInfo.SetAgeFilename(kPersonalAgeFilename);

if (hsRef<RelVaultNode> rvnInfo = rvnFldr->GetChildNode(templateNode, 2)) {
templateNode->Clear();
templateNode->SetNodeType(plVault::kNodeType_SDL);
if (hsRef<RelVaultNode> rvnInfo = rvnFldr->GetChildNode(&templateNode, 2)) {
templateNode.Clear();
templateNode.SetNodeType(plVault::kNodeType_SDL);

if (hsRef<RelVaultNode> rvnSdl = rvnInfo->GetChildNode(templateNode, 1)) {
if (hsRef<RelVaultNode> rvnSdl = rvnInfo->GetChildNode(&templateNode, 1)) {
VaultSDLNode sdl(rvnSdl);
sdl.SetStateDataRecord(rec, plSDL::kDirtyOnly | plSDL::kTimeStampOnRead);
}
Expand Down Expand Up @@ -529,12 +513,12 @@ void _InvitePlayerToAge(ENetError result, void* state, void* param, RelVaultNode

void pyVault::InvitePlayerToAge( const pyAgeLinkStruct & link, uint32_t playerID )
{
hsRef<NetVaultNode> templateNode = new NetVaultNode;
templateNode->SetNodeType(plVault::kNodeType_TextNote);
VaultTextNoteNode visitAcc(templateNode);
NetVaultNode templateNode;
templateNode.SetNodeType(plVault::kNodeType_TextNote);
VaultTextNoteNode visitAcc(&templateNode);
visitAcc.SetNoteType(plVault::kNoteType_Visit);
visitAcc.SetVisitInfo(*link.GetAgeLink()->GetAgeInfo());
VaultCreateNode(templateNode, (FVaultCreateNodeCallback)_InvitePlayerToAge, nil, (void*)(uintptr_t)playerID);
VaultCreateNode(&templateNode, (FVaultCreateNodeCallback)_InvitePlayerToAge, nullptr, (void*)(uintptr_t)playerID);
}

//============================================================================
Expand All @@ -557,12 +541,12 @@ void pyVault::UnInvitePlayerToAge( const char * str, uint32_t playerID )
}
}

hsRef<NetVaultNode> templateNode = new NetVaultNode;
templateNode->SetNodeType(plVault::kNodeType_TextNote);
VaultTextNoteNode visitAcc(templateNode);
NetVaultNode templateNode;
templateNode.SetNodeType(plVault::kNodeType_TextNote);
VaultTextNoteNode visitAcc(&templateNode);
visitAcc.SetNoteType(plVault::kNoteType_UnVisit);
visitAcc.SetVisitInfo(info);
VaultCreateNode(templateNode, (FVaultCreateNodeCallback)_UninvitePlayerToAge, nil, (void*)(uintptr_t)playerID);
VaultCreateNode(&templateNode, (FVaultCreateNodeCallback)_UninvitePlayerToAge, nullptr, (void*)(uintptr_t)playerID);
}

//============================================================================
Expand Down
10 changes: 2 additions & 8 deletions Sources/Plasma/FeatureLib/pfPython/pyVaultAgeInfoListNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,9 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com

#include "plVault/plVault.h"

// should only be created from C++ side
pyVaultAgeInfoListNode::pyVaultAgeInfoListNode(RelVaultNode* nfsNode)
: pyVaultFolderNode(nfsNode)
{
}

//create from the Python side
pyVaultAgeInfoListNode::pyVaultAgeInfoListNode(int n)
: pyVaultFolderNode(n)
pyVaultAgeInfoListNode::pyVaultAgeInfoListNode()
: pyVaultFolderNode()
{
fNode->SetNodeType(plVault::kNodeType_AgeInfoList);
}
Expand Down
9 changes: 2 additions & 7 deletions Sources/Plasma/FeatureLib/pfPython/pyVaultAgeInfoListNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,16 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com

struct RelVaultNode;


class pyVaultAgeInfoListNode : public pyVaultFolderNode
{
protected:
// should only be created from C++ side
pyVaultAgeInfoListNode(RelVaultNode* nfsNode);

// python-side ctor
pyVaultAgeInfoListNode(int n=0);
pyVaultAgeInfoListNode();
public:

// required functions for PyObject interoperability
PYTHON_CLASS_NEW_FRIEND(ptVaultAgeInfoListNode);
static PyObject *New(RelVaultNode* nfsNode);
static PyObject *New(int n=0);
PYTHON_CLASS_VAULT_NODE_NEW_DEFINITION;
PYTHON_CLASS_CHECK_DEFINITION; // returns true if the PyObject is a pyVaultAgeInfoListNode object
PYTHON_CLASS_CONVERT_FROM_DEFINITION(pyVaultAgeInfoListNode); // converts a PyObject to a pyVaultAgeInfoListNode (throws error if not correct type)

Expand Down
16 changes: 2 additions & 14 deletions Sources/Plasma/FeatureLib/pfPython/pyVaultAgeInfoListNodeGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,10 @@ PYTHON_START_METHODS_TABLE(ptVaultAgeInfoListNode)
PYTHON_END_METHODS_TABLE;

// Type structure definition
PLASMA_DEFAULT_TYPE_WBASE(ptVaultAgeInfoListNode, pyVaultFolderNode, "Params: n=0\nPlasma vault age info list node");
PLASMA_DEFAULT_TYPE_WBASE(ptVaultAgeInfoListNode, pyVaultFolderNode, "Plasma vault age info list node");

// required functions for PyObject interoperability
PyObject *pyVaultAgeInfoListNode::New(RelVaultNode* nfsNode)
{
ptVaultAgeInfoListNode *newObj = (ptVaultAgeInfoListNode*)ptVaultAgeInfoListNode_type.tp_new(&ptVaultAgeInfoListNode_type, NULL, NULL);
newObj->fThis->fNode = nfsNode;
return (PyObject*)newObj;
}

PyObject *pyVaultAgeInfoListNode::New(int n /* =0 */)
{
ptVaultAgeInfoListNode *newObj = (ptVaultAgeInfoListNode*)ptVaultAgeInfoListNode_type.tp_new(&ptVaultAgeInfoListNode_type, NULL, NULL);
// oddly enough, nothing to do here
return (PyObject*)newObj;
}
PYTHON_CLASS_VAULT_NODE_NEW_IMPL(ptVaultAgeInfoListNode, pyVaultAgeInfoListNode);

PYTHON_CLASS_CHECK_IMPL(ptVaultAgeInfoListNode, pyVaultAgeInfoListNode)
PYTHON_CLASS_CONVERT_FROM_IMPL(ptVaultAgeInfoListNode, pyVaultAgeInfoListNode)
Expand Down
11 changes: 2 additions & 9 deletions Sources/Plasma/FeatureLib/pfPython/pyVaultAgeInfoNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include "pnUUID/pnUUID.h"
#include "plVault/plVault.h"

// should only be created from C++ side
pyVaultAgeInfoNode::pyVaultAgeInfoNode(RelVaultNode* nfsNode)
: pyVaultNode(nfsNode)
{
}

//create from the Python side
pyVaultAgeInfoNode::pyVaultAgeInfoNode(int n)
: pyVaultNode(new RelVaultNode)
pyVaultAgeInfoNode::pyVaultAgeInfoNode()
: pyVaultNode()
{
fNode->SetNodeType(plVault::kNodeType_AgeInfo);
}
Expand Down
8 changes: 2 additions & 6 deletions Sources/Plasma/FeatureLib/pfPython/pyVaultAgeInfoNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,13 @@ class plUUID;
class pyVaultAgeInfoNode : public pyVaultNode
{
protected:
// should only be created from C++ side
pyVaultAgeInfoNode(RelVaultNode* vaultNode);

//create from the Python side
pyVaultAgeInfoNode(int n=0);
pyVaultAgeInfoNode();

public:
// required functions for PyObject interoperability
PYTHON_CLASS_NEW_FRIEND(ptVaultAgeInfoNode);
static PyObject *New(RelVaultNode* vaultNode);
static PyObject *New(int n=0);
PYTHON_CLASS_VAULT_NODE_NEW_DEFINITION;
PYTHON_CLASS_CHECK_DEFINITION; // returns true if the PyObject is a pyVaultAgeInfoNode object
PYTHON_CLASS_CONVERT_FROM_DEFINITION(pyVaultAgeInfoNode); // converts a PyObject to a pyVaultAgeInfoNode (throws error if not correct type)

Expand Down
Loading