[BUG] FWindowsPlatformMemory fails if statements in MapnamedSharedMemoryRegion()

I’m trying to use FGenericPlatformMemory’s Shared Memory code so I can publish some game data using Shared Memory. I have to use Shared Memory because it’s a requirement. It would be very convenient for me if I could just use Unreal Engine’s shared memory stuff.

The problem is in the FWindowsPlatformMemory::MapNamedSharedMemoryRegion() function. There is a block of if-statements that never get executed no matter what you pass into the function.

It’s almost as if the if statements are being compiled out. It’s causing my code to fail, and I get this in the Unreal Editor Log:

LogHAL:Warning: CreateFileMapping(file=INVALID_HANDLE_VALUE, security=NULL, protect=0x4, MaxSizeHigh=0, MaxSizeLow=262168, name='Global\UnrealEngineVideoCapture') failed with GetLastError() = 5

The GetLastError()=5 corresponds to Windows System Error code of ERROR_ACCESS_DENIED.

I literally step through my code using breakpoints in Visual Studio and I can see the debugger stepping passed the if statement when it was supposed to go into the if statement block on line 181 of WindowsPlatformMemory.cpp

It’s logically equivalent to this little example:

myfunc(4)

void myfunc(int x)
{
	if(x == 4)
	{
	 print hello
	}
}

and your program never prints hello.

It’s completely bizarre, so that’s why I believe those if statements are getting compiled out.

This is how I’m creating the Shared Memory region:

		bool create = true;

		SharedMemoryRegion =
			FPlatformMemory::MapNamedSharedMemoryRegion(
			TEXT("UnrealEngineVideoCapture"),
			create,
			FPlatformMemory::ESharedMemoryAccess::Write | FPlatformMemory::ESharedMemoryAccess::Read,
			GetRenderTargetSize() + SHARED_MEMORY_HEADER_SIZE);

This is the file I am talking about and these are the if statements:

in WindowsPlatformMemory.cpp lines 174-182 and lines 189-196:

FPlatformMemory::FSharedMemoryRegion* FWindowsPlatformMemory::MapNamedSharedMemoryRegion(const FString& InName, bool bCreate, uint32 AccessMode, SIZE_T Size)
{
	FString Name(TEXT("Global\\"));
	Name += InName;

	DWORD OpenMappingAccess = FILE_MAP_READ;
	check(AccessMode != 0);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////           THIS IF-ELSE BLOCK GETS SKIPPED
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if (AccessMode == FPlatformMemory::ESharedMemoryAccess::Write)
	{
		OpenMappingAccess = FILE_MAP_WRITE;
	}
	else if (AccessMode == (FPlatformMemory::ESharedMemoryAccess::Write | FPlatformMemory::ESharedMemoryAccess::Read))
	{
		OpenMappingAccess = FILE_MAP_ALL_ACCESS;
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////          END
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	HANDLE Mapping = NULL;
	if (bCreate)
	{
		DWORD CreateMappingAccess = PAGE_READONLY;
		check(AccessMode != 0);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////           THIS IF-ELSE BLOCK ALSO GETS SKIPPED
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		if (AccessMode == FPlatformMemory::ESharedMemoryAccess::Write)
		{
			CreateMappingAccess = PAGE_WRITECOPY;
		}
		else if (AccessMode == (FPlatformMemory::ESharedMemoryAccess::Write | FPlatformMemory::ESharedMemoryAccess::Read))
		{
			CreateMappingAccess = PAGE_READWRITE;
		}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////           END
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		///................... SOME MORE CODE HERE........................

	return new FWindowsSharedMemoryRegion(Name, AccessMode, Ptr, Size, Mapping);
}

Has anyone else had this problem? How did you resolve it?
Is there any way to disable some of the compiler flags when I build my project?

Thanks