Issues using Named Shared Memory

So basically what I'm trying to do is create a shared memory for an agent, where it stores information about itself on a regular basis, and then somewhere else in my program, I want to access that information and use it. I've found the easiest way to do this is to use Global Shared Memory due to certain constraints placed by the rest of the program.

The issue that I'm having is that some of the information which I'm putting into shared memory seems to be becoming corrupted, but I can't figure out the reason behind it.

The following code is how I'm implementing the writing. (There's a separate creation of the shared memory in the initialising function which doesn't close the handle, so that the shared memory doesn't disappear)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
struct policies
{
	int numPol;
	string namePolicies[20];
};

policies MyPol;
MyPol.numPol = numPolicies;

if (i < numPolicies)
{
	MyPol.namePolicies[i] = test->getPolicyName();
}
i++;


if (useSharedMemory)
{
	struct properties
	{
		int reward;
		string state;
	};

	string shmName = "Global\\";
	string name = getName();
	shmName += name;
	shmName += "_";
	shmName += test->getPolicyName();
	LPCTSTR shmName2 = shmName.c_str();

	HANDLE hMapFileFirst;
	LPCTSTR upDate;

	properties Me;
	Me.reward = test->getMostRecentReward();
	Me.state = stateName;

	hMapFileFirst = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shmName2);

	if (hMapFileFirst == NULL)
	{
		_tprintf(TEXT("Could not open file mapping object DWL1(%d).\n"), GetLastError());
	}

	upDate = (LPTSTR)MapViewOfFile(hMapFileFirst, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);

	if (upDate == NULL)
	{
		_tprintf(TEXT("Could not map view of file DWL1(%d).\n"), GetLastError());
	}
	else
	{
		CopyMemory((PVOID)upDate, &Me, sizeof(Me));

		UnmapViewOfFile(upDate);
		CloseHandle(hMapFileFirst);
	}
}


This appears to work fine. If I try to access this shared memory directly after it's created, all of the data comes out exactly as it should.

The problem arises after I've done stuff with other shared memory (I have a number of agents, each with their own shared memory). After I've done this, the second position of the string array in my first agent (i.e. Agent1.namePolicies[1]) will have changed to a jumble of random characters. (I only actually use position [0] and [1] at the minute in the array, so I don't know if further positions would also be affected, but position [0] is never affected.

The code I use to access the shared memory is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
string shmName = "Global\\";
shmName += NeighName;
LPCTSTR shmName2 = shmName.c_str();

HANDLE hMapFileNeigh;
LPCTSTR pBufNeigh;

policies NeighA;
NeighA.numPol = 0;

policies* pData;

hMapFileNeigh = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shmName2);

if (hMapFileNeigh == NULL)
{
	_tprintf(TEXT("Could not open file mapping object nIpI2 (%d).\n"), GetLastError());
}

pBufNeigh = (LPCTSTR)MapViewOfFile(hMapFileNeigh, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);

if (pBufNeigh == NULL)
{
	_tprintf(TEXT("Could not map view of file nIpI2 (%d).\n"), GetLastError());

	CloseHandle(hMapFileNeigh);
}
else
{
	// Calculate the pointer to the data.
	pData = (policies *)pBufNeigh;
	NeighA = *pData;

	UnmapViewOfFile(pBufNeigh);
	CloseHandle(hMapFileNeigh);
}

string neighbour;

for (int i = 0; i < NeighA.numPol; i++)
{
	neighbour = NeighName;
	neighbour += "_";
	neighbour += NeighA.namePolicies[i];

	addNeighbours(neighbour);
}


Anyone any ideas? Have I just done something stupid with this?

If any more explanation/code is needed just ask away.
Figured it out, shared memory didn't like how long the string I was putting into the second position of the array was (even though it was only 20 characters)
Topic archived. No new replies allowed.