TMap remove actor key problem


TMap(TSoftObjectPtr(AActor), FValue) AMap;  
for (auto ItRemove = AMap.CreateIterator(); ItRemove; ++ItRemove)  
{  
    if ((ItRemove.Key().IsValid())  
	{		
		if (Key.IsValid())  
		{  
			Key->Destroy();  
		}  
        ItRemove.RemoveCurrent();  
	    break;  
	}  
}

In some case, when i destroy my actor key, the AMap will remove the pair automatically, so when next ItRemove.RemoveCurrent(); was executed, my program will crash, but in other case, when i destroy my actor key, the AMap will not remove that pair, i don’t know why tmap remove the pair when the actor key was destroy?

pls help me, thx

Hello! What is Key variable? There is no declaration for it in attached code and brackets are not balanced…

Don’t use Actors like keys. In fact You using a memory address as a key when doing this. It is a bad practice, because your actor can be destroyed and using the key (that referenced the deleted actor) can provide a segmentation fault.

All object has an unique ID. You can use it, if You want to access some date by specific actor. Or save this data into the Actors, then this data will be deleted with the actor