Access violation on if statement

Hello,

I am experiencing a problem in my c++ project. I added a Widget blueprint from another project, and it started crashing and giving me an “Access violation” error. I removed the widget and every reference to it, but the project is still crashing. This happens both in standalone and in selected viewport mode. The bug is reported in my “Inventory” class, which I haven’t edited in a while. In the “Diagnostics.txt” file, there is no source code appearing between the [SOURCE START] and the [SOURCE END] tags.

Here is a code sample which causes the probem :

uint8 Number = Items.Num();
if (Index >= Number) {

}
if (Index >= Number){                         // This line is crashing
	if (EmptyItem->GetTexture() == nullptr) {
		EmptyItem->InitParams(ItemParams);
	}
	return EmptyItem;
}
return Items[Index];

This bug is strange, because the line causing the error is caused by the second “if” statement, although it is the same as the first one. I added the first “if” statement to show the strangeness of the bug. I have never seen an error like that while trying to compare two ints. However, if i replace the “Number” local variable by a literal integer (e.g if Index >= 5 ), then the project seems to find an error at the next line instead.

I have been trying to find a solution for the past 3 hours, and the only logical explanations I could find were either corrupted memory, or a corrupted project. I also tried rebooting my pc, and rebuilding the project multiple times, to no avail.

Something tells me EmptyItem might be null, this is only reason i can see calling on null pointer cause crash so do EmptyItem && EmptyItem->GetTexture() == nullptr. It might pointing wrong line due to buggus PDB files generated by VS compiler, but thats VS bug. Also run without debugging and after crash check the logs Saved/Logs in project directory, if log is cut without any message then i’m 99.9% procent sure EmptyItem is the cause

Thank you for the swift response. I added a test to verify that EmptyItem is not null, I even called a function on it before the line causing trouble, but the error still happens on the same line. I added a bit of code to see what caused the error :

uint8 Number = Items.Num();
if (EmptyItem) {
if (EmptyItem->GetTexture() == nullptr) {
//EmptyItem->InitParams(ItemParams);
}
}

if (Index >= Number){ // This line is crashing

When that 4th line is commented, the error still occurs on the same line as before. However, when i uncomment the 4th line, the error occurs on the 2nd line… I thought the problem was that ItemParams might be null too, but when I added a test for that too, the error moved to the first line. Then, when I removed that one, the error moved to the 2nd line…

I know that sounds very confusing…

  1. uint8 Number = Items.Num();
  2. if (EmptyItem) {
  3. if (EmptyItem->GetTexture() == nullptr) {
  4.   //EmptyItem->InitParams(ItemParams);
    
  5. }
  6. }
  7. if (Index >= Number){ // This line is crashing

Sorry looks like my indentation has been killed

After trying for a while to understand the problem, I took a backup from 4 days ago, copied all my new code and it now works. I don’t know what the problem was, I’ll probably at some point compare the two projects, but definitely not now.