Fix and test for ticket #4964 - assert in debug mode on x64

[This is a new version of the ex-pull request #6; this new one is set up for the develop branch.]

This fixes #4964 and adds a testcase which used to trigger the assert. Note that the testcase only fails on x64 (address-model=64) in debug mode on Windows (and when the fix is not in place, obviously).
I haven't had a chance to test this on ARM, but MSDN is clear on that this applies to ARM in the same way so the fix also applies to that platform.