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 <stdio.h>

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
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

Baryon

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值