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"

Explicitely Delete a UObject

Hiya, I am wondering what the best way to destroy a UObject (not an actor) created with NewObject is. Afaik only AActors have the Destroy function. I'm afraid to just call "delete" on it, because I feel like NewObject created objects have extra shutdown steps.

What's the recommended way to deal with this?

Product Version: Not Selected
Tags:
more ▼

asked Apr 28 '15 at 05:13 PM in C++ Programming

avatar image

mrooney
603 30 24 114

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Hello, mrooney

UObjects are managed by the garbage collector. To create a UObject appropriately, use NewObject(), NewNamedObject() and ConstructObject(). It is possible to configure the way UObjects will be handled by garbage collector at the time of creation with Object Flags enumeration. (If you like to learn more about UObject Instance Creation, you can go here: https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Objects/Creation/index.html ) This way, you should not call new or delete on UObjects. If UObject is no longer needed, it usually means that there are no references to it (this may, however differ, depending on the context and garbage collection flags used at the moment of UObject creation). In this situation, you can run ForceGarbageCollection() function:

 GetWorld()->ForceGarbageCollection(true);

Please note, that calling this method may cause crashes in some situations, particularly when object is already being destroyed by garbage collector or has a value of null.

Also, if you like to learn more about Unreal Object Handling, you can go here: https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Objects/Optimizations/index.html

Hope this helped! Good luck!

more ▼

answered Apr 29 '15 at 07:19 AM

avatar image

Gleb.Bulgakov
1.7k 75 19 137

avatar image mrooney Apr 29 '15 at 03:17 PM

Thanks much. What I'm wanting to know is if there are ways to force destroy a UObject, similar to AActor::Destroy() or ways to ref count a UObject. I have a "system" object kind of like the UAIPerceptionSystem, which should only live in a certain place that controls its lifetime. I'm not sure what would be the best way to implement that.

I've noticed it's come up a couple times where I know an object should be destroyed at a certain point, but I have no way of checking if there's anything preventing that from happening or forcing the engine to destroy it.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

ConditionalBeginDestroy()

"Thanks much. What I'm wanting to know is if there are ways to force destroy a UObject, similar to AActor::Destroy() or ways to ref count a UObject. "

 if(YourObject)
 {
   YourObject->ConditionalBeginDestroy();
 }

Please note this is not exactly recommended workflow but it works great for me, use at your discretion.

Gleb's answer is extremely effective as well, though it will clear all non-UPROPERTY linked UObjects all at once.

I recommend Gleb's answer unless you find you really need to clear individual UObjects. I've now shared my own personal solution for what your exact inquiry seems to be.

My solution is really only for if you dont want to wait till the next GC pass, and also dont want to trigger GC to run.

I don't recommend deleting UObject's that would not have gotten cleared by GC naturally.

Rama

more ▼

answered Apr 30 '15 at 09:10 AM

avatar image

Rama
9.7k 426 309 1032

avatar image FTWinston Sep 06 '15 at 11:02 PM

I have a class that persists across map changes, and the game was crashing when you quit due to this not being properly disposed of. Combined with this other answer, this completely sorted the problem. Thanks!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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