题
// A Mutex allows threads mutually exclusive access to a resource.
//-----------------------------------------------------------------------
class Mutex
{
private:
CRITICAL_SECTION m_mutex;
public:
Mutex() { InitializeCriticalSection(&m_mutex); }
~Mutex() { DeleteCriticalSection(&m_mutex); }
void acquire() { EnterCriticalSection(&m_mutex); }
void release() { LeaveCriticalSection(&m_mutex); }
};
使用Entrek Codesnitch软件调试和测试任何内存泄漏等,它会报告以下错误:
InitializeCriticalSection Error: lpCriticalSection (0x000387d4) points to an invalid
memory location (0x00018984) Mutex::Mutex in lockmutex.h, line 29
也许我所有不眠之夜终于到了我的面前。但我不明白它究竟在抱怨什么。有什么想法吗?
解决方案
我敢打赌,你可以用:: memset(<!> amp; m_mutex,0,sizeof(m_mutex))伪装出金属片。在调用init之前。
其他提示
CodeSnitch显然不够聪明,不知道InitializeCriticalSection()希望处理包含未初始化指针的结构。
从CodeSnitch的角度考虑它。你正在做什么和这之间的区别是什么:
struct Customer {
char * name;
};
extern void greetCustomer(Customer* c);
class CheckoutLine {
private:
Customer m_customer;
public CheckoutLine() {
greetCustomer(&m_customer);
}
};
这对于人眼来说看起来更可疑,因为我们推断greetCustomer可能会依赖于m_customer被初始化,这显然不是。但从语义上讲,这与您的代码完全相同。
可能值得向Entrek提交一个错误; InitializeCriticalSection()是<!>的合理例外;结构应该在将它们传递给函数<!>之前进行初始化;规则。
我认为您的班级定义没有任何问题。它在哪里被使用?特定实例仍可能使用不当。
CRITICAL_SECTION的Win32定义包括指向'struct _RTL_CRITICAL_SECTION *'的指针。操作系统可能在处理这个结构时非常聪明,这会使工具混淆。
不隶属于 StackOverflow