# Access Violation When Trying To Create TSharedPtr of (this)Object

**URL:** <https://forums.unrealengine.com/t/access-violation-when-trying-to-create-tsharedptr-of-this-object/356005>\
**Category:** C++\
**Tags:** error, unreal-engine, crash, CPP, question, pointers\
**Created:** [April 30, 2016, 1:17pm UTC](https://forums.unrealengine.com/t/access-violation-when-trying-to-create-tsharedptr-of-this-object/356005 "2016-04-30T13:17:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diddykonga](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Diddykonga](https://forums.unrealengine.com/u/Diddykonga)\
**Post date:** [April 30, 2016, 1:17pm UTC](https://forums.unrealengine.com/t/access-violation-when-trying-to-create-tsharedptr-of-this-object/356005/1 "2016-04-30T13:17:25Z")

</div>

// Parse the folders array  
const TArray\<TSharedPtr\>\* FolderDescriptors;  
if (Tree-\>TryGetArrayField(TEXT(“folder”), /_out_/ FolderDescriptors))  
{  
for (TSharedPtr FolderDescriptor : \*FolderDescriptors)  
{  
TSharedPtr Folder = TSharedPtr(new FSpriterFolder());  
Folders.Add(Folder);

```
		const bool bParsedFolderOK = Folder->ParseFromJSON(TSharedPtr<FSpriterSCON>(this), FolderDescriptor->AsObject(), NameForErrors, bSilent);
		bSuccessfullyParsed = bSuccessfullyParsed && bParsedFolderOK;
	}
}

```

Heres the code that is creating the Access Violation.

It happens when I try to pass a TSharedPtr to the ParseFromJSON method.

I need to pass the Current Object(FSpriterSCON) to the Folder(FSpriterFolder), how would i do this using Smart Pointers?

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [May 2, 2016, 6:50pm UTC](https://forums.unrealengine.com/t/access-violation-when-trying-to-create-tsharedptr-of-this-object/356005/2 "2016-05-02T18:50:40Z")

</div>

You could make your object derive from `TSharedFromThis` and then use `AsShared()` to get a shared pointer from, however that relies on your object having been allocated on the heap.

I’d question more why you need to use a shared pointer at all. Would a raw pointer or reference work instead? Keeping a shared pointer to your owner object from within a child object could easily lead to a reference cycle and a memory leak (unless you stored it as a weak pointer).

---

<div class="post-metadata">

**Author:** ![Diddykonga](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Diddykonga](https://forums.unrealengine.com/u/Diddykonga)\
**Post date:** [May 3, 2016, 2:35am UTC](https://forums.unrealengine.com/t/access-violation-when-trying-to-create-tsharedptr-of-this-object/356005/3 "2016-05-03T02:35:37Z")

</div>

Thanks for the answer! I was refactoring most of my code to work with Smart Pointers, but then realized that most of my Structs only need to store value types anyways, and the ones that wanted references/pointers to other structs would just have to be removed.

The reason behind this is becuase FSpriterSCON is stored as a UProperty inside of a UObject that gets created by a File Factory.

So everytime that UObject was serialized and then reloaded the pointers/references would be NULL because they cant be UPropertys. Also dont ever try and create a Shared Pointer of a value type that wasnt created on the heap/ or if its a UProperty, cause i guess Smart Pointers and UPropertys dont know about eachother’s reference counts.

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [May 3, 2016, 1:25pm UTC](https://forums.unrealengine.com/t/access-violation-when-trying-to-create-tsharedptr-of-this-object/356005/4 "2016-05-03T13:25:09Z")

</div>

Yeah, `UPROPERTY` and `TSharedPtr` are two completely separate memory management schemes that should never meet.

Use `UPROPERTY` for any `UCLASS` or `USTRUCT` marked types that either need serialisation or GC references. These use garbage collection to manage their lifetime.

Use `TSharedPtr` for any pure C++ types that exist outside of the UE4 GC logic. These use reference counting to manage their lifetime.

---

<div class="post-metadata">

**Author:** ![Diddykonga](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Diddykonga](https://forums.unrealengine.com/u/Diddykonga)\
**Post date:** [May 3, 2016, 11:25pm UTC](https://forums.unrealengine.com/t/access-violation-when-trying-to-create-tsharedptr-of-this-object/356005/5 "2016-05-03T23:25:56Z")

</div>

This makes things very clear and understandable, thank you very much!
