CopyMemory fails after MapViewofFile
I have created a file mapping object. I have mapped views, taking into account allocation granularity. Yet, after MapViewOfFile returns a valid pointer, I have trouble assigning data to allocated memory pointed by the same.
Is there something that i have missed here?
Following is the piece of the code, i have added some comments to make it easier for one to read.
I have created a filemapping object reserving address space for an array of CTraceData objects.
someFunction()
{
...
...
//The already created fileMappingObject has a size of, the maxID * sizeof(CTraceData)
//msg.GetID() below basically returns me a unique number
//Any number starting from 1 to about say 3000
long lID = msg.GetId();
SYSTEM_INFO siInfo;
::GetSystemInfo(&siInfo);
DWORD dwSysGran = siInfo.dwAllocationGranularity;
//below (lID - nNeoLogRangeStart) results in a number ranging from 0 to about 3000
//sizeof(CTraceData) is about 2-3 KB
long lDataOffset = sizeof(CTraceData) * (lID - nNeoLogRangeStart);
long lMapViewStart = (lDataOffset / dwSysGran) * dwSysGran;
long lMapViewSize = (lDataOffset % dwSysGran) + sizeof(CTraceData);
long lDataPtr = (lMapViewStart + lMapViewSize) - sizeof(CTraceData);
void *m_pViewStart = ::MapViewOfFile (m_pFileMapI->GetFileMapHandle(),
FILE_MAP_ALL_ACCESS,
0,
lMapViewStart,
lMapViewSize);
if(pData != NULL)
{
//Try Copying here
}
...
...
}
At this point if I try...
::CopyMemory(pData, &objData, sizeof(objData));
where, objData is an object of CTraceData. objData is initialized & is declared as...
struct CTraceData
{
int nUniqueId;
char m_szFunctionName[MAX_PATH];
char m_szFileName[MAX_PATH];
long nLineNum;
char m_szTraceData[2001];
CTraceData();
};
I am unable to figure out what is wrong with my code.
Above, after i Map a view, if instead of CopyMemory, i try...
CTraceData *pData = (CTraceData *)((char*)m_pViewStart + lDataPtr);
and now i copy each member individually from the CTraceData local object to the memory pointed by pData. Debugger here shows the memory pointed by pData as invalid for all the members.
What have i done incorrectly
# 1 Re: CopyMemory fails after MapViewofFile
...
struct CTraceData
{
int nUniqueId;
char m_szFunctionName[MAX_PATH];
char m_szFileName[MAX_PATH];
long nLineNum;
char m_szTraceData[2001];
CTraceData();
};
[/code]
This structure is non-POD, as it contains a user-defined constructor. Therefore you can't use functions such as "CopyMemory" on it safely. Only POD types are guaranteed to not fail when doing this.
CTraceData *pData = (CTraceData *)((char*)m_pViewStart + lDataPtr);
This "bit-by-bit" copying and access is not guaranteed to do what you want, again because CTraceData is a non-POD type. You must actually create these objects and store them in a container of some type -- you can't treat them as if they are simple structs.
Unfortunately, the C++ language rules as it currently stands does not guarantee that your object will be handled safely because of it being non-POD. Yes, it's only a constructor that stops it from being non-POD, but those are the guarantees you have (or non-guarantee).
How do you create these CTraceData objects? Unless they are created as non-dynamic instances, or by using operator new, you have a problem.
Regards,
Paul McKenzie
# 2 Re: CopyMemory fails after MapViewofFile
Yes, it's only a constructor that stops it from being non-POD, but those are the guarantees you have (or non-guarantee).
How do you create these CTraceData objects? Unless they are created as non-dynamic instances, or by using operator new, you have a problem.
I create an object of CTraceData on the stack as below
CTraceData obj;
Now if i remove the constructor, shd it solve my problem? I only use the constructor to default initialize the members. Nothing more. I will be copying data into the struct each time i attempt to use it. So i can do away with the constructor without any hassels.
So, I understand that basically the idea here is for me to have a C type struct where the memory layout is fixed. I attempted the same by removing the constructor. Dosen't work. I changed struct to union, and again dosent work. :(