I've been working on this for months and am absolutely livid that me nor my team can figure this out :/
Here is the situation - I am managing the memory of another program, specifically reading structures. When reading something simple, like an integer, it works fine. When reading a structure, I need to shift my reading location back 4 bytes, else I end up reading at the wrong location and the values stored are incorrect.
Here is an example:
ReadProcessMemory( hProcess, lpBaseAddress, &xBuffer, sizeof( xBuffer ), nullptr );
So long that xBuffer is not a Class or a Struct, this works fine. Say that xBuffer is declared as an object of vector_t - this is the only way my read will work:
class vector_t
{
public:
float x, y, z;
};
vector_t xBuffer;
ReadProcessMemory( hProcess, lpBaseAddress - 0x4, &xBuffer, sizeof( xBuffer ), nullptr );
I know this code isn't MCRE/reproducible, because I've tried copying over snippets of my code and it works fine, but this is all that is happening and I have no idea what else could cause this.
Getting the size of a class, it returns size of all members +0x4, when it should be +0x1, right? Maybe that helps?
EDIT: After further debugging, I found the issue is with atomic. Why?
#include <Windows>
#include <atomic>
class vector_t
{
public:
float x, y, z;
};
/* ... */
std::atomic< DWORD > dwBaseAddress { };
/* ... */
std::atomic< vector_t > vecBuffer { };
// unless we subtract 4 bytes from lpBaseAddress._My_val, we will not read properly
ReadProcessMemory( hProcess, LPVOID( lpBaseAddress._My_val ), &vecBuffer, sizeof( vector_t ), nullptr );
When I make a copy of vecBuffer (that isn't atomic), it works fine. What can I do to ensure this situation doesn't happen?