熟悉stl给看一看!
我在dll里写了这样一个函数:
void meGetAtl( map< string,vector<unsigned _int32> > &stlmap )
编译,连接都通过,
客户程序调用dll里的这个函数,
运行时,当使用传回的stlmap时
出现access violation错误。谁知道理由亚?
问题点数:22、回复次数:6Top
1 楼myan()回复于 2001-08-09 21:21:36 得分 5
写DLL时不要使用STL。Top
2 楼geobean()回复于 2001-08-10 09:10:17 得分 0
为什么?Top
3 楼xiterator(xi)回复于 2001-08-10 14:43:57 得分 15
一个原因是STL大量使用了在heap中new对象方法,而dll和客户端exe拥有不同的heap,所以在跨跃dll边界时,像处理对象析构的代码无法正常工作(如:由客户端release dll的heap中的创建的对象),下面是引用msdn中的一段话,可作说明:
The “local” heap refers to the heap created and managed by a particular instance of the C run-time library. If a dynamically linked library (DLL) contains a static link to the run-time library, then it has its own instance of the run-time heap, and therefore its own heap, independent of the application’s local heap.
Top
4 楼gorge_an(木头)回复于 2001-08-10 15:01:02 得分 2
我想为什么要用map 的引用呢? 用指针可不可以?(stl 我不是很懂)答错了可要见笑了。
但是写dll的时候是可以用stl的。我现在手上的工程中就是用到了的。Top
5 楼xiterator(xi)回复于 2001-08-10 16:24:31 得分 0
我也不是很清楚stl,只是谈谈:不是不可以在dll中使用stl,而是尽量不要在导出函数中涉及stl的代码,以免混用dll和exe两方的heap。Top
6 楼xiterator(xi)回复于 2001-08-10 16:29:48 得分 0
我也不是很清楚stl,只是谈谈自己的想法:不是不可以在dll中使用stl,而是尽量不要在导出函数中涉及stl的代码,以免混用dll和exe两方的heap,所以用map指针也不推荐,因为若在dll方往map中增加item(其中自会在dll的专有heap中new对象),那么在exe release map时就会出现问题.Top




