Skip to content

Commit

Permalink
fixes memory leak due to circular shared reference to parent in FObject
Browse files Browse the repository at this point in the history
  • Loading branch information
TCR-Nick committed Mar 16, 2024
1 parent 69005c1 commit 0dc9551
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ TSharedPtr<Ink::FObject> Ink::FContainer::GetContentWithPathComponent(const Ink:

else if (InComponent.IsParent())
{
return Parent;
return Parent.Pin();
}

else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int32 Ink::FObject::GetHashCode()

TSharedPtr<Ink::FObject> Ink::FObject::GetParent() const
{
return Parent;
return Parent.Pin();
}

void Ink::FObject::SetParent(TSharedPtr<Ink::FObject> InParent)
Expand Down Expand Up @@ -68,7 +68,7 @@ TSharedPtr<Ink::FPath> Ink::FObject::GetPath()
}

child = container;
container = DynamicCastTo<FContainer>(container->Parent);
container = DynamicCastTo<FContainer>(container->Parent.Pin());
}
Algo::Reverse(componentsStack); // explicitly reverse the array, instead of copying the c# code's implicit technique of handing over a stack to an array

Expand All @@ -87,7 +87,7 @@ Ink::FSearchResult Ink::FObject::ResolvePath(TSharedPtr<Ink::FPath> InPath)
if (!nearestContainer.IsValid())
{
ensureAlwaysMsgf(Parent != nullptr, TEXT("Can't resolve relative path because we don't have a parent"));
nearestContainer = DynamicCastTo<Ink::FContainer>(Parent);
nearestContainer = DynamicCastTo<Ink::FContainer>(Parent.Pin());
ensureAlwaysMsgf(nearestContainer != nullptr, TEXT("Expected parent to be a container"));
ensureAlways(InPath->GetComponent(0)->IsParent());
InPath = InPath->GetTail();
Expand All @@ -106,7 +106,7 @@ TSharedPtr<Ink::FContainer> Ink::FObject::GetRootContentContainer()
TSharedPtr<Ink::FObject> ancestor = this->AsShared();
while (ancestor->Parent.IsValid())
{
ancestor = ancestor->Parent;
ancestor = ancestor->Parent.Pin();
}

return FObject::DynamicCastTo<FContainer>(ancestor);
Expand Down Expand Up @@ -204,7 +204,7 @@ TSharedPtr<Ink::FDebugMetadata> Ink::FObject::GetDebugMetadata() const
{
if (Parent.IsValid())
{
return Parent->DebugMetadata;
return Parent.Pin()->DebugMetadata;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace Ink
// StoryException being static is causing problems during the tests as it isn't always resetting between runs. Currently using simple logging instead of this

protected:
TSharedPtr<Ink::FObject> Parent;
TWeakPtr<Ink::FObject> Parent;
TSharedPtr<Ink::FPath> _Path;
TSharedPtr<Ink::FDebugMetadata> DebugMetadata;
Ink::FContainer* RootContentContainer;
Expand Down

0 comments on commit 0dc9551

Please sign in to comment.