Corrupted (?) memory mid usage

I’m a week now trying to understand what is going on without success.
I have this piece of code: (most of it comments explaining my issue)

void USomeClass::someFunction(TArray<UBaseA*>& outOthers, UBaseA* initiator, UBaseA* responder)
{
	// at this point the inputs to the method are correct and valid (data-wise) the pointed to data 
	// is not garbage

	if (not mIsRequiresOther) return;
	
	const auto manager = GetWorld()->GetGameInstance()->GetSubsystem<UManagerSubsystem>()->getInstance();
	checkf(manager != nullptr, TEXT("Couldn't get manager"));
	
	TArray<UBaseA*> possibleOthers = {};
	manager->getAllGameObjects(possibleOthers);

	// at this point, the possible others is filled with objects as i can see in the debugger, all are valid and correct

	for (int32 j = 0; j < possibleOthers.Num(); j++) {
		UBaseA* other = possibleOthers[j];

		// at this stage few things may happen:
		// - data inside other seem ok and not garbage
		// - data inside other may seem garbage and invalid as shown below, although if using 
		//    IsValid() or possibleOthers.IsIndexValid(j), all return true and work fine
		//    0x00000b65db518cd8 (Name = Illegal name (length > NAME_SIZE)_4294967294, 
		//    Owner = 0x0000000000000000 (Name=???))
		// - the most baffling issue is that at the moment the if statement below is executed, 
		//    something becomes garbage
		// - responder may show the same illegal name error shown above even though moment
		//    ago it was fine
		// - or all values in the debugger may seem good and absolutely satisfy the if statement, 
		//    but it returns false and then the responder becomes garbage
		// - the same could happen to other

		if ((initiator->mObjectName != other->mObjectName) && (responder->mObjectName != other->mObjectName)) {
			... do something ...
		}
	}
}

I tried so many things, including using for ranged loop, using IsValid on all possible objects, trying even to refactor the getAllGameObjects() to return array of FNames (mObjectName is FName) instead of objects to make sure nothing is garbage collected for some reason, although it shouldn’t be because all the participating member variables here are UPROPERTY().
I also tried to use other member variable of type int, but it results the same.

Anyone encountered something like this or can think of a reason for that to happen?

Just a guess, but you are using Game Instance, you might still be referencing objects that are no longer valid ( like when you change maps ).

    const auto manager = GetWorld()->GetGameInstance()->GetSubsystem<UManagerSubsystem>()->getInstance();
	checkf(manager != nullptr, TEXT("Couldn't get manager"));
	
	TArray<UBaseA*> possibleOthers = {};
	manager->getAllGameObjects(possibleOthers);

Hi, I have a pretty simple setup with 1 level only. I also use this game instance in many methods in the callstack and it seems to be ok, not sure how to check it otherwise

So the issue is fixed now, but I can only assume the reason for this.
Bottom line is that I still see the garbage values but now the code proceeds inside the “if” statement.

I had a bug in code where some boolean was set to false, but never able to be true.
I assume that some stuff were optimized out because there was no use for them, and in my case I think it was this all “if” statement with the “do something…” inside.
Because the code segment had no real mutable impact, it was just optimized out, leading me to believe that no matter what the evaluation of the expression inside the if condition is, it returns always false.

Fixing the code in a manner that this bool now can be assigned either true/false in some cases, fixed the issue, leading the code inside the if statement to be processed now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.