Wednesday, July 7

BUG Report: Failed When a Native Event is Unhooked with with Multi-Heritance
SYMPTOMS
当在一个使用多继承类作为event_receiver的未托管应用程序中unhook (__unhook) 一个native events时, 将会失败。__unhook将返回1(表明没有成功地unhook。在应用程序结束以后native event机制造成了内存泄漏.
CAUSE
这个问题发生在事件的接受者(event_receiver)是多继承类的情况下。在unhook的时候event的注入代码无法正确比较成员函数指针。
WORKAROUND
这个问题可以使用下面的代码重现:

#include

class A
{
int a;
};

class B
{
int b;
};

[event_source(native)]
class CSource
{
public:
__event void MyEvent(int nValue);
};

[event_receiver(native)]
class CReceiver : public A, public B //多继承
{
public:
void MyHandler1(int nValue) {
printf("MyHandler1 was called with value %d.\n", nValue);
}

void MyHandler2(int nValue) {
printf("MyHandler2 was called with value %d.\n", nValue);
}
};

int main() {
CSource source;
CReceiver receiver;

__unhook(&CSource::MyEvent, &source, &CReceiver::MyHandler2, &receiver);
__hook(&CSource::MyEvent, &source, &CReceiver::MyHandler1, &receiver);
__raise source.MyEvent(1);
__unhook(&CSource::MyEvent, &source, &CReceiver::MyHandler1, &receiver);//失败
__hook(&CSource::MyEvent, &source, &CReceiver::MyHandler2, &receiver);
__raise source.MyEvent(2);
__unhook(&CSource::MyEvent, &source, &CReceiver::MyHandler2, &receiver);//失败

}


运行结果

MyHandler1 was called with value 1.
MyHandler2 was called with value 2.
MyHandler1 was called with value 2.

MyHandler2没有被正确unhook。

The information in this article applies to:

Microsoft Visual C++ .NET (2002)
Microsoft Visual C++ .NET (2003)
Visual C++ 2005 Express Edition Beta
Wednesday, July 07, 2004 11:10 AM

No comments: