CopyMemory causes Access Violation on Win8

64-bit, delphi

Solution

At this point:

Pointer(LongWord(Self.FMemory) + Self.FPosition)

you truncate a 64 bit pointer to 32 bit. Hence the access violation. Instead you need

Pointer(NativeUInt(Self.FMemory) + Self.FPosition)

Your code is just as broken on Win7, but somehow you were unlucky and only ever ran this code with pointers with address < 4GB.

You should run some top-down memory allocation testing to flush out any other such errors.

Problem

I have a piece of code that compiles using Delphi XE3 into 64-bit COM DLL. ``` function TRPMFileReadStream.Read(var Buffer; const Count: Longint): Longint; begin if ((Self.FPosition >= 0) and (Count > 0)) then begin Result := Self.FSize - Self.FPosition; if ((Result > 0) and (Result >= Count)) then begin if (Result > Count) then begin Result := Count; end; CopyMemory( Pointer(@Buffer), Pointer(LongWord(Self.FMemory) + Self.FPosition), Result ); Inc(Self.FPosition, Result); Exit; end; end; Result := 0; end; ``` On Win7-64bit, the above works fine. but On Win8-64bit, The same DLL file will throw Access Violation on CopyMemory. The CopyMemory is implemented in WinAPI.windows unit. It is like this. ``` procedure CopyMemory(Destination: Pointer; Source: Pointer; Length: NativeUInt); begin Move(Source^, Destination^, Length); end; ``` Any ideas? Thanks.

Original source

Related problems