# How to remove nullptr as a key from TMap

**URL:** <https://forums.unrealengine.com/t/how-to-remove-nullptr-as-a-key-from-tmap/418824>\
**Category:** Platform & Builds\
**Tags:** tmap, unreal-engine, question\
**Created:** [March 3, 2018, 1:39am UTC](https://forums.unrealengine.com/t/how-to-remove-nullptr-as-a-key-from-tmap/418824 "2018-03-03T01:39:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShankIsUsed](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ShankIsUsed](https://forums.unrealengine.com/u/ShankIsUsed)\
**Post date:** [March 3, 2018, 1:39am UTC](https://forums.unrealengine.com/t/how-to-remove-nullptr-as-a-key-from-tmap/418824/1 "2018-03-03T01:39:32Z")

</div>

Hello guys:

I am writing a Threat Map for target selection for battle that has a large number of AI.  
Sometimes, here is my Tmap declaration:

TMap ThreatMap;

Like so, I am using the Character ref to find the threat value in the TMap. Sometime, the key = nullptr, and I try to remove it with ThreatMap.Remove(Key), however the Tmap nullptr key element is still there. Anyone know what is going on?

Thanks

---

<div class="post-metadata">

**Author:** ![GIGA-Money](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GIGA-Money](https://forums.unrealengine.com/u/GIGA-Money)\
**Post date:** [March 3, 2018, 4:17am UTC](https://forums.unrealengine.com/t/how-to-remove-nullptr-as-a-key-from-tmap/418824/2 "2018-03-03T04:17:33Z")

</div>

…so C++  
its great isn’t it.  
NULL POINTER (nullprt) is …well here is some info

> <https://stackoverflow.com/questions/14189709/why-cant-you-take-the-address-of-nullptr>

if you didn’t have that NULL POINTER you would crash your system (weather soft or hard crash)  
so no you can’t remove that. You will need more info on pointers to continue.

---

<div class="post-metadata">

**Author:** ![GIGA-Money](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GIGA-Money](https://forums.unrealengine.com/u/GIGA-Money)\
**Post date:** [March 3, 2018, 4:20am UTC](https://forums.unrealengine.com/t/how-to-remove-nullptr-as-a-key-from-tmap/418824/3 "2018-03-03T04:20:20Z")

</div>

[http://www.cplusplus.com/doc/tutorial/pointers/](http://www.cplusplus.com/doc/tutorial/pointers/)  
more pointers.

So you need to assert the condition, if its null then move on, write to that spot, not sure what your doing.

---

<div class="post-metadata">

**Author:** ![Inconceivable](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Inconceivable](https://forums.unrealengine.com/u/Inconceivable)\
**Post date:** [March 3, 2018, 6:51am UTC](https://forums.unrealengine.com/t/how-to-remove-nullptr-as-a-key-from-tmap/418824/4 "2018-03-03T06:51:20Z")

</div>

First of all, it’s a little odd that you’re using a pointer as a key in an associative array. It’s not _wrong_ necessarily, just strange. I think personally I’d use the name of the character instead of the pointer to it.

Second, how are you using this? You’re not putting nullptr into the TMap yourself, are you? (Don’t.) It’s that you have a character that has been removed from the scene, so its reference is null right? If that’s the case, then it’s likely because the garbage collector doesn’t care about the TMap keys. (But that’s just a guess, I haven’t looked at the TMap code). To test that theory, Try making an additional TArray and put each character in that array as well. The garbage collecter **does** respect the TArray, so it will prevent the characters from being removed while they’re still in use in the TMap.

Obviously that shouldn’t be your final solution, but it might help you figure out what is happening.

---

<div class="post-metadata">

**Author:** ![klobodnf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@klobodnf](https://forums.unrealengine.com/u/klobodnf)\
**Post date:** [May 13, 2024, 7:20am UTC](https://forums.unrealengine.com/t/how-to-remove-nullptr-as-a-key-from-tmap/418824/5 "2024-05-13T07:20:54Z")

</div>

```
for (auto MapIt = TMapInstance.CreateIterator(); MapIt; ++MapIt)
{
	if (MapIt.Key().ResolveObjectPtr() == nullptr)
	{
		MapIt.RemoveCurrent();
	}
}

```
