多线程下vc2003,vc2005对虚函数表处理的BUG?
来源:互联网 发布:mac 编译php 编辑:程序博客网 时间:2024/04/29 20:20
考虑一下多线程代码,在设计上,App为了获取更多的功能,从Window派生,而App同时为了获取
某个模块的回调(所谓的Listener),App同时派生Listener,并将自己的指针交给另一个模块,
另一个模块通过该指针多态回调到App的实现(对Listener规定的接口的implemention)。设计上
只是一个很简单的Listener回调,在单线程模式下一切都很正常(后面我会罗列代码),但是换到
多线程下,编译器似乎就对语言机制的支持不够了:
某个模块的回调(所谓的Listener),App同时派生Listener,并将自己的指针交给另一个模块,
另一个模块通过该指针多态回调到App的实现(对Listener规定的接口的implemention)。设计上
只是一个很简单的Listener回调,在单线程模式下一切都很正常(后面我会罗列代码),但是换到
多线程下,编译器似乎就对语言机制的支持不够了:
/**////
/// to demonstrate the fucking bug.
///
#include <iostream>
#include <process.h>
#include <windows.h>
class Window
{
public:
/**////
virtual void wrong()
{
std::cout << "wrong" << std::endl;
}
virtual ~Window()
{
std::cout << "~Window" << std::endl;
}
};
class Listener
{
public:
/**//// as most listener class, it only put some interface here
virtual void show() {}
};
class Game
{
public:
Game() : _listener( 0 ) { }
void init( Listener *listener )
{
_listener = listener;
/**//// it will call Window::wrong function but not App::show.
_listener->show();
}
private:
Listener *_listener;
};
Game gGame;
static unsigned int __stdcall ThreadFunc( void *p )
{
Listener *listener = (Listener*) p;
gGame.init( listener );
while( true )
{
std::cout << ".";
Sleep( 100 );
}
_endthreadex( 0 );
return 0;
}
class App : public Window, public Listener
{
public:
void init()
{
// create the game thread
_game_thread = (HANDLE)_beginthreadex( NULL, 0, ThreadFunc, this, 0, NULL );
}
/**//// implement the interface
void show()
{
std::cout << "App::show" << std::endl;
}
/**//// exit
void exit()
{
/**//// just for testing purpose
::TerminateThread( _game_thread, 1 );
::CloseHandle( _game_thread );
}
private:
HANDLE _game_thread;
};
App gApp;
int main()
{
gApp.init();
std::cout << "Press enter key to exit!" << std::endl;
std::cin.get();
gApp.exit();
return 0;
}
/// to demonstrate the fucking bug.
///
#include <iostream>
#include <process.h>
#include <windows.h>
class Window
{
public:
/**////
virtual void wrong()
{
std::cout << "wrong" << std::endl;
}
virtual ~Window()
{
std::cout << "~Window" << std::endl;
}
};
class Listener
{
public:
/**//// as most listener class, it only put some interface here
virtual void show() {}
};
class Game
{
public:
Game() : _listener( 0 ) { }
void init( Listener *listener )
{
_listener = listener;
/**//// it will call Window::wrong function but not App::show.
_listener->show();
}
private:
Listener *_listener;
};
Game gGame;
static unsigned int __stdcall ThreadFunc( void *p )
{
Listener *listener = (Listener*) p;
gGame.init( listener );
while( true )
{
std::cout << ".";
Sleep( 100 );
}
_endthreadex( 0 );
return 0;
}
class App : public Window, public Listener
{
public:
void init()
{
// create the game thread
_game_thread = (HANDLE)_beginthreadex( NULL, 0, ThreadFunc, this, 0, NULL );
}
/**//// implement the interface
void show()
{
std::cout << "App::show" << std::endl;
}
/**//// exit
void exit()
{
/**//// just for testing purpose
::TerminateThread( _game_thread, 1 );
::CloseHandle( _game_thread );
}
private:
HANDLE _game_thread;
};
App gApp;
int main()
{
gApp.init();
std::cout << "Press enter key to exit!" << std::endl;
std::cin.get();
gApp.exit();
return 0;
}
App多重继承Window和Listener,在Game里回调App::show时,却调用到了Window::wrong函数。看上去,传给
Game的Listener指针所指向的虚函数表错误了(vtable指针错了)。App先继承Listener后继承Window时,情况
就正确了。(因为使用了_beginthreadex,程序需要链接多线程的运行时库)
单线程情况下:
/**////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// to demonstrate the fucking bug.
///
/// OK, even it links the multi-thread crt.
#include <iostream>
#include "kl_thread.h"
class Window
{
public:
/**////
virtual ~Window()
{
std::cout << "~Window" << std::endl;
}
};
class Listener
{
public:
/**//// as most listener class, it only put some interface here
virtual void show() {}
};
/**////
Listener *gListener;
class App : public Window, public Listener
//class App : public Listener, public Base
{
public:
void init()
{
gListener = this;
}
/**//// implement the interface
void show()
{
std::cout << "App::show" << std::endl;
}
};
App gApp;
int main()
{
gApp.init();
gListener->show();
return 0;
}
///
/// to demonstrate the fucking bug.
///
/// OK, even it links the multi-thread crt.
#include <iostream>
#include "kl_thread.h"
class Window
{
public:
/**////
virtual ~Window()
{
std::cout << "~Window" << std::endl;
}
};
class Listener
{
public:
/**//// as most listener class, it only put some interface here
virtual void show() {}
};
/**////
Listener *gListener;
class App : public Window, public Listener
//class App : public Listener, public Base
{
public:
void init()
{
gListener = this;
}
/**//// implement the interface
void show()
{
std::cout << "App::show" << std::endl;
}
};
App gApp;
int main()
{
gApp.init();
gListener->show();
return 0;
}
无论Listener, Window的顺序如何,一切都很正常。这起码说明了,在语言层次,我的做法是正确的。
而这个时候即使链接了多线程的运行时库,结果也是正确的。
那么错误可以归结于多线程,可能是在多线程下编译器对虚函数表初始化不正确所致。这是否真的是
VC2003、VC2005的BUG?
posted on 2008-04-24 14:40 Kevin Lynx 阅读(866) 评论(10) 编辑 收藏 引用 所属分类: C++
2008-04-24 16:09 Fox
- 多线程下vc2003,vc2005对虚函数表处理的BUG?
- VC2005 VC2003 VC6工程的转换
- VC2005 转换成 VC2003
- boost_1_32_0在vc2003下的编译
- boost_1_32_0在vc2003下的编译
- VC2005打开VC2003项目时fatal error CVT1100: 重复的资源。type:MANIFEST
- visual studio R6034解决方案集 从VC6.0 或VC2003 到VC2005发现的问题
- faint,今早居然连遇着两个vc2003的bug.
- 使用目录创建VC2003/VC2005工程
- vc2005/MFC-添加不常用的消息的消息处理函数/消息处理函数.
- 浅谈VC2003对C++虚继承/共享继承实现
- VC2003下查内存泄漏的简单方法
- vc2003/2005下,菜单始终无法变灰的问题
- VC2005下的编译错误
- 多线程环境下libcurl的一个Bug
- 在没有安装VC2005的环境下运行VC2005程序
- 关闭VC2005中对Unicode的支持
- VC2005 Bug汇总
- MSRA相关考题
- 理解SQL Server 2000中的错误处理
- 在SQL Server中使用相关子查询
- Windows Server 2003 R2 with SP2 安装Realtek HD AUDIO Driver的解决方法
- 常见内存错误 - iu_81的专栏 - CSDNBlog
- 多线程下vc2003,vc2005对虚函数表处理的BUG?
- 用sp_lock诊断SQL Sever的性能问题
- 您知道SQLServer2005的10个高级特性吗
- ACI 转 RGB
- 在SQL Server中使用NewID()方法产生随机集
- 一个Javascript程序的烦恼!!!!达人赐教
- 在SQL Server 2005中获得详细错误处理信息
- 如何添加新的系统调用
- 使用SQL Server 2005 Express Edition的3种场合
评论
还是写出来看的清楚:static unsigned int __stdcall ThreadFunc( void *p )
{
Listener *listener = (Listener*) p;
应该是
Listener *listener = (App*) p;
回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 17:34 giscn
或者这样
_game_thread = (HANDLE)_beginthreadex( NULL, 0, ThreadFunc, (Listener*)this, 0, NULL ); 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 17:40 giscn
错误由void* 指针转换引起,与多线程无关,C++ 的指针转换不同于C, 如果是多集继承,参数同样是 this, 其实际值不一定相同,取决于参数类型 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 17:40 eXile
楼上的正解。在使用多重继承时要注意对象的布局。 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 18:09 亨德列克
Listener *listener = (Listener*) p; 是这一行错了,这个错误应该很多人都会犯…… 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 19:18 Kevin Lynx
@亨德列克
不是那一行错了,App和Game两个类分属不同模块,为了不让两个模块耦合,这里使用Listener *listener = (Listener*) p,而不是(App*)p。
giscn和eXile (他删除了他的第二条评论:) )的方法是正确的。可以被采用,再次表示感谢。
这让我意识到,void*在C++里缺乏安全性。 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 22:49 饭中淹
这不是多线程的问题
当你先继承window后继承Listener的时候,App的内存结构如下:
class App
vt of Window
data of Window
vt of Listener
data of Listener
data of App
_beginthreadex的参数是void*,你把this传递进去,相当于传递CApp* this。其实隐含的就是Window*this,那么里面调用Listener->Show,自然就会去Window的vt里面查找对应索引的函数,就会调用错函数。
而第二个,因为你显式的=this,所以,编译器会进行转换,从而把正确的Listener地址赋值给那个全局指针,这时,无论继承顺序如何,都是正确的结果。
这其实是因为对象指针转换不准确导致的,不是vc的bug,也不是多线程的问题。
回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-25 08:32 FongLuo
收藏,收藏,^_^ 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-25 17:03 #Ant
多继承下还有这样的问题,学习了。。。 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG?[未登录] 2008-04-26 00:49 杨粼波
指针偏移,多线程会发生这样滴问题。。。。