x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

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?

Product Version: UE 4.15
Tags:
more ▼

asked Mar 30 '17 at 10:01 PM in C++ Programming

avatar image

jamestillerunc
0 2 11 8

avatar image TX_Alpha Mar 30 '17 at 10:47 PM

To the best of my knowledge, only items created using Unreal Tags are managed by the engine GC system: UClass UFunction UProperty etc

I'm making this as a comment, not answer because I'd prefer someone with more expertise to confirm.

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Mar 30 '17 at 10:49 PM

avatar image

jamestillerunc
0 2 11 8

avatar image TX_Alpha Mar 31 '17 at 03:42 PM

ya, for things like that, you'll also want to use UE types:

FString FVector TChar etc

(comments are locked)
10|2000 characters needed characters left

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.

more ▼

answered Mar 31 '17 at 04:44 PM

avatar image

Shadowriver
35.7k 925 168 1104

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question