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"

Destroying an Actor Referenced in an Array

Hello everyone

I have an array of actors which i use for some purposes through out entire level. When I try to destroy an actor and remove it from the array, i get a crash saying Assertion Failed: IsInGameThread().

Here is the crash log: Crash Log

This is where debugging leads me to. If i just remove the line that says this->Destroy() everything becomes fine! I am not sure about what i am doing wrong. I hope someone can help me. Thanks in advance.

 for(int32_t i = PiecesOnBoard.Num()-1; i >= 0; i--)
 {
     AChessPiece * ChessPiece = dynamic_cast<AChessPiece*>(this->PiecesOnBoard[i]);
     if(ChessPiece && ChessPiece->GetPiecePosCurrent() == Position)
     {
         ChessPiece->PieceGotEaten();
         this->PiecesOnBoard.RemoveAtSwap(i);
         break;
     }
 }

Here is PieceGotEaten function:

 void AChessPiece::PieceGotEaten_Implementation()
 {
     CurrentPiecePosition = EPiecePosition::NotOnBoard;
     this->SetActorEnableCollision(false);
     PieceMesh->SetVisibility(false);
     Capsule->SetCollisionEnabled(ECollisionEnabled::NoCollision);
     Capsule->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
 
     this->Destroy();
 }


Product Version: UE 4.14
Tags:
more ▼

asked Jan 05 '17 at 07:40 AM in C++ Programming

avatar image

Corpse0327
186 26 25 38

avatar image emrahgunduz Jan 05 '17 at 07:45 AM

try destroying the item after removing it from the array.

avatar image Corpse0327 Jan 05 '17 at 07:49 AM

It doesn't make any sense, but still tried and well, crash...

avatar image emrahgunduz Jan 05 '17 at 08:04 AM

destroy is done at the end of tick, i thought maybe a null pointer might be crashing your game. you can check if your actor is really getting destroyed, Destroy command returns a bool, if true your actor is/will be destroyed. you can also try K2_DestroyActor instead of Destroy. Another thing, you can call garbage collection right after destroying: GetWorld()->ForceGarbageCollection(true);... also before calling you class method, why not check if it is valid? if(!ChessPiece) continue; and if(!ChessPiece->IsValidLowLevel()) continue; maybe...

avatar image Corpse0327 Jan 05 '17 at 09:00 AM

I spent some time on playing around. But it seems the cause of the problem is elsewhere while the problem occurs there. I will try to play around a bit more and find out what is wrong.

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

2 answers: sort voted first

if you are on another thread, then you are definitely hitting a race condition :) you must not manipulate renderable items outside of the main (render) thread. Try calling your code in something like this instead:

 FGraphEventRef Task = FFunctionGraphTask::CreateAndDispatchWhenReady([&]() {
   // code here runs on game thread
 }, TStatId(), NULL, ENamedThreads::GameThread);
 
 // waits till task completes, you can disable this if you want
 FTaskGraphInterface::Get().WaitUntilTaskCompletes(Task);
more ▼

answered Jan 05 '17 at 10:41 AM

avatar image

emrahgunduz
168 13 15 21

avatar image Corpse0327 Jan 05 '17 at 11:03 AM

Wow, that is interesting. It works like a charm! There must be some voodoo magic involved :)

Thanks!

avatar image dani2442 Aug 23 '18 at 06:00 PM

I have a question, Is there any relationship between this post and this

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

I might have spotted the issue, however i am not sure and would be happy if someone confirmed me.

It seems Unreal Engine has an assertion to make sure that no Actor would get destroyed by a class that exists in another thread.

In my case, i have a thread that runs another process. When that process gives signal, an event named BestMoveReceived is called and executes the above functions. I guess thats where the crash happens. It detects an actor is being deleted by a call from another thread and safety belt kicks in.

If there is such a thing in Unreal, then i need to search for other methods.

more ▼

answered Jan 05 '17 at 10:33 AM

avatar image

Corpse0327
186 26 25 38

(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