Download

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


 // Parse the folders array
 const TArray<TSharedPtr<FJsonValue>>* FolderDescriptors;
 if (Tree->TryGetArrayField(TEXT("folder"), /*out*/ FolderDescriptors))
 {
     for (TSharedPtr<FJsonValue> FolderDescriptor : *FolderDescriptors)
     {
         TSharedPtr<FSpriterFolder> Folder = TSharedPtr<FSpriterFolder>(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?
AnswerHub - Access Violation When Trying To Create TSharedPtr of (this)Object - UE4 AnswerHub

Try it with Makesharable(this)?

Unfortunately I’ve tried that already. Thanks for the suggestion though!

Anybody else got any insight on this issue?

I think you should dig into where in the Folder->ParseFromJSON you get the error. To get an access violation you could be referencing something that’s invalid. TSharePtr has the method “IsValid()” to check if it is. Perhaps after passing in TSharePtr(this), the method uses it incorrectly? If you find yourself unable to step through because of optimization, put this on the top of your file:

#pragma optimize("", off)

Remember to take it off when you are done.

The folder isnt actually using the TSharedPtr at all actually, i removed that code to check if that was it but it was not.

Its a weird situation, cause when i added Logs to the code above, i actually see that it Calls ParseFromJSON on the folder, which then calls ParseFromJSON on all the folder’s files, and when ParseFromJSON from the folder returns i output a log after that as well. This means the error isnt coming from inside the method, but also that the TSharedPtr im creating is working, but only the first time through the for loop, as it throws an error the second time through the for loop.

I would say that it might be the For loop that has problems, but i have many other For loops exactly like that one, except in those other For loops i’m not creating a SharedPtr to the This to pass

This sounds like a memory corruption where you have deleted say an element from an array and after the delete, you modified a reference to that memory slot. Try to backtrace to where you modified memory. Do you get different callstack each time?

I dont delete any information at all, I just create the FSpriterFolder SharedPtr then add it to the FSpriterSCON’s Folders array of TSharedPtr’s, then with that same Folder TSharedPtr reference that i created i call ParseFromJSON on it, and also pass the current FSpriterSCON as TSharedPtr parameter.

So there is no deleting going on at all, as far as im aware. Also im not sure what you mean by going through the call stack(im not very good at using VS to debug).