Why is a simple function crashing the editor?

Its been a while since I did any C++ but I can’t see why this is happening;

I’ve added a few lines of code to the character class;

This is in the header file;

  	/** Function for deleting hand hold from currently active hand-hold info */
    UFUNCTION(BlueprintCallable, Category=Inventory)
    TArray <float> DeleteHandHold( TArray <float> HandHold, TArray <float> ActiveHandHolds);

private:

	bool findHandHold(TArray<float> HandHold, TArray <float> TestHandHold);

};

This is in the cpp;

// function to sort array, find handhold and delete it, return the new array
TArray <float> ALedgeGrabCharacter::DeleteHandHold(TArray<float> HandHold, TArray <float> ActiveHandHolds)
{	
	int AHHSize = ActiveHandHolds.Num();
	int loopNum = AHHSize;
	for (int i = 0; i < loopNum / 6; i += 6) {
		TArray<float> TestHandHold;
		for (int y = i; y < 6; i++) {
			TestHandHold.Add(ActiveHandHolds[y]);
		}
		if (findHandHold(HandHold, TestHandHold) == true) {
			for (int x = 0; x < 6; x++) {
				if (ActiveHandHolds.Num() > 0) {
					ActiveHandHolds.RemoveAt(i);
					break;
				}
			}
		}
	}
	return ActiveHandHolds;
  }

bool ALedgeGrabCharacter::findHandHold(TArray<float> HandHold, TArray <float> TestHandHold) {
	
	if (HandHold[0] == TestHandHold[0] && HandHold[1] == TestHandHold[1] && HandHold[2] == TestHandHold[2] && HandHold[3] == TestHandHold[3] && HandHold[4] == TestHandHold[4] && HandHold[5] == TestHandHold[5]) {
		return true;
	}
	else return false;
}

This is how the function node is being using in BP. Its being fired by an end overlap event in the HandHold BP. Anyone see what I’ve done wrong? Many thanks!

The log might contain some information about why the editor is crashing. If you go to your project directory, navigate to Saved > Logs > LOGOFCRASH.log and open that up, the answer might be there. If not, what does the error say?

I apologize, I wasn’t clear about the log name, that name I put in there was just a place holder.

You would simply select the most recent log after you experience a crash in the editor and open it in word.

Run editor on debugger it should point you to line that crashes the editor

Hmm. There’s no log named that way in the saved > logs file. There’s a bunch of Dump files with “Exception Information: The thread tried to read or write to a virtual address for which it does not have appropriate access.” No error message, the game just hangs and then the editor crashes.

I just did it again and there’s a new dump file there, timed at when I did it…

Cool, thanks, good debugging tools, hadnt used them yet…

That worked well, thanks! The problem with what was getting sent to it- made the function go out of bounds on the array, I think.

Why never use int?

EDIT: Got it. “Don’t use the C++ int type in portable code, since it’s dependent on the compiler how large it is.”
That’s useful to know.

#Never Assume Length

You are consistently assuming array lengths!

 int AHHSize = ActiveHandHolds.Num();
    int loopNum = AHHSize;
    for (int i = 0; i < loopNum / 6; i += 6) {
       TArray<float> TestHandHold;
       for (int y = i; y < 6; i++) {
         TestHandHold.Add(ActiveHandHolds[y]);
       }

You should never assume length!

#IsValidIndex()

Use IsValidIndex() to ensure that all your index arithmetic and assumptions did actually pan out correctly.

if(ActiveHandHolds.IsValidIndex(y))
{
  //safe to access the index
  ActiveHandHolds[y] = //etc
}

#Never Use Int

 int AHHSize = ActiveHandHolds.Num();

use int32

int32 AHHSize = ActiveHandHolds.Num();

Hey
That’s useful to know, I agree! Also IsValidIndex(), thanks alot!

Thanks ! Love your work! Keep on keeping on!

This fixed an issue for me where online multiplayer was crashing the game–whereas local multiplayer and single player did not show any issues. If you check your logs you may find this error message:

LogScriptCore:Warning: Script Msg: Attempted to access index 0 from array EnemiesWithinRange of length 0!