Are standard c++ classes garbage collected?
Hey all, I'm currently chasing down an odd bug in a third party plugin that I wrote.
I have several data structs meant to transport information between my library dll and Unreal, exposed through DLLIMPORT/DLLEXPORT, with the intent of communicating between the two. Remarkably, structs instantiated in the library are fine, but structs (raw c++, not UStruct) that are instantiated in Unreal are causing a system access violation/corrupt memory exception when I pass them to the library. I have c++ lambdas (std::function) that I pass from Unreal and those callbacks work fine, but for whatever reason, all of my vanilla c++ structs are corrupt on arrival and throw exceptions.
Is Unreal garbage collecting my c++ structs? Or is it another issue?
asked Mar 30 '17 at 10:01 PM in C++ Programming
The structs that were causing problems contained std::string, which causes issues. Swapping over to const char * looks to have solved the problem, potentially. I'll update tomorrow if I find out otherwise.
answered Mar 30 '17 at 10:49 PM
C++ is still normal C++ so there is no magical gurbage collection made by UE4. UE4 has GC but it only deals with stuff that it manage and only UObjects classes are garbage collected by UE4, reflection system don't support any other pointer types so they are not get destroyed automatically as engine don't see there existence at all. Other non-pointer data are contained in class structure so it get wiped together with object.
I also think TSharedPtr TSharedRef also might get destroyed when all references are cleared, but i not sure about it, the main use of that pointers is to prevent invalid pointers so you should use them anyway if you deal with pointers other then UObject. But i'm also not sure how it works with standard C++ types.
Imo best will be if you manually manage those pointers from library, as you would manually use them in other non-UE4 C++ project, tied them with UObject class life circle, of object initiation create/get pointers, destroy them on object destruction.
You should also imidietly as soon as you can, convert standard C++ types to UE4 types, as UE4 code don't digest them at all. you saying you got issue with struct, if you use standard C++ don't use USTRUCT() so UE4 won't have any contact with it. But by doing that oyu wont be able to use this struct in bluerint, UFUNCTION and UPROPERTY (which is also optional). imo you should already convert to UE4 type when you put data in to struct that will be used in UE4 code.
answered Mar 31 '17 at 04:44 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here