在获取/设置过程中,内存将被自动覆盖 [英] Memory being overwritten apparenty automatically during Get/Set

查看:71
本文介绍了在获取/设置过程中,内存将被自动覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些任意的数据类负责锁定并发访问:

CMyData.h:

I have a some arbitrary data class that is in charge of locking out concurrent access:

CMyData.h:

class CMyData
{
public:
	CMyData(void);
	virtual ~CMyData(void);
public:
	void Get(CMyDataItem** ppMyData, int& nSize);
	void Set(const CMyDataItem* pMyData, int nSize);
private:
	CMyDataItem* m_pMyData; // array of CMyDataItem
	int m_nTableSize;       // size of array
	CCriticalSection m_CriticalSection;
};



Get和Set的实现为:



The implementations for Get and Set are:

void CMyData::Get(CMyDataItem** ppMyData, int& nSize)
{
	CSingleLock lock(&m_CriticalSection, TRUE);
	if(NULL != m_pMyData)
	{
		if(m_nTableSize != nSize || NULL == *ppMyData)
		{
			// Table size has changed so rebuild
			delete[] *ppMyData;
			nSize = m_nTableSize;
			*ppMyData = new CMyDataItem[m_nTableSize];
		}
		CopyMemory(*ppMyData, m_pMyData, sizeof(CMyDataItem)*m_nTableSize);
	}
	else
	{
		ASSERT(0); // By context, shouldn't happen
	}
}







and

void CMyData::Set(const CMyDataItem* pMyData, int nSize)
{
	CSingleLock lock(&m_CriticalSection, TRUE);
	if(NULL != pMyData)
	{
		if(m_nTableSize != nSize)
		{
				// Table size has changed so rebuild
				m_nTableSize = nSize;
				delete[] m_pMyData;
				m_pMyData = new CMyDataItem[nSize];
		}
		CopyMemory(m_pMyData, pMyData, sizeof(CMyDataItem)*nSize);
	}
	else
	{
		ASSERT(0); // by context, this shouldn't happen
	}
}



如果我重复几次Get and Set,在某个时候,m_pMyData的内容会变成gobbledygook,并且我保证我没有其他方法可以更改m_pMyData的内容!

调用示例:



If I repeat Get and Set a few times, at some point, the content of m_pMyData turns into gobbledygook, and I promise I haven''t got any other way of changing the content of m_pMyData!

Calling example:

	CMyDataItem* pMyData = NULL;
	m_pMyData->Get(&pMyData);
	pMyData->m_nID = 10;
	m_pMyData->Set(pMyData);
	delete[] pMyData;
	pMyData = NULL;
// repeat...


有任何想法吗?请帮忙!

============

PS:

这里是更多信息.


Any ideas? Please help!

=============

PS:

Here is a bit more info.

class CMyDataItem
{
public:
	CMyDataItem();
	virtual ~CMyDataItem();
public:
	int m_nSomeID;
	int m_nAnotherID;
	int m_nNumber;
	double m_dValue1;
	double m_dValue2;
	double m_dValue3;
	double m_dValue4;
	double m_dValue5;
	double m_dValue6;
	double m_dValue7;
	double m_dValue8;
	double m_dValue9;
	bool m_bBool1;
	bool m_bBool2;
	bool m_bBool3;
	unsigned int m_uiMyID;
	CString m_strString1;
	CString m_strString2;
};

推荐答案

您应确保CMyDataItem不包含任何指针,或不包含指针的类,例如字符串类等.如果这样做,CopyMemory将破坏您的数据.在这种情况下,您可以改用std::copy并实现复制构造函数.

我猜这是最有可能发生的情况,因为您正在使用单个线程进行测试.
You should make sure CMyDataItem does not contain any pointers, or classes that in turn uses pointers, such as string classes or the like. The CopyMemory will ruin your data if it does. In that case you could use std::copy instead, and implement copy constructor of course.

I guess this is the most likely scenario since you are testing with a single thread.


这篇关于在获取/设置过程中,内存将被自动覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆