Sign-up....

stl 高手请进!

我发现一个很奇怪的地方

如果写个dll里面类有个函数返回一个std::string变量,并且这个变量被付了一个大于16长度的字符串,函数返回的返回值临时变量会分配和析够内存失败。谁知道原因?谢谢!

#include <string>

using namespace std;

class TD_EXPORT CRecord

{

// constructor/destructor

public:

CSQLRecordset();

std::string _orderBy;

std::string _groupBy;

std::string _result;

std::string GetData();

};

// error function

std::string CRecord::GetData()

{

_result = "safasfasfasdfasdfsadfasfsafsadfsafasdasf";

return _result;

}

main(...)

{

CRecord rec;

rec.GetData() // error

}

[591 byte] By [msdn] at [2007-8-15 8:45:14]
# 1 Re: stl 高手请进!

哦,是因为DLL和EXE的内存分配问题。

你的stl分别在dll内和exe内编译。

实际上是依靠dll中的new分配的内存,在exe中释放这个内存。

这个问题在Win32核心编程有说明。

我估计你是VC6,

VC7,VC8就没有这个问题了。

healer_kx at 2006-5-22 16:10:56 >
# 2 Re: stl 高手请进!

在VC6内也可以解决这个问题,

重新typedef basic_string<, MyAlloc<T>,>

重写这个类 MyAlloc<T> ,可以使用自己的内存池或者是 GlobalAlloc等API

healer_kx at 2006-5-22 16:12:23 >
# 3 Re: stl 高手请进!

后来我找到原因了,stl 的这个string是共享内存机制的,healer_kx(甘草(begin to new journey)) 说得很对。但是我用的vc7啊,你说没这个问题,好像不对哦。

请问大家有什么好的解决方法呢。

coolgong at 2006-5-22 20:33:02 >
# 4 Re: stl 高手请进!

vc7下不作大的改动,有什么方法解决呢?

coolgong at 2006-5-22 20:35:02 >
# 5 Re: stl 高手请进!

VC7下应该没有问题。

lz试一试用_bstr_t类型做返回值,使用这种类型应该是可以的。

ugg at 2006-5-22 21:16:02 >
# 6 Re: stl 高手请进!

顶下

顺便学习

cattlenzq at 2006-5-22 22:06:37 >
# 7 Re: stl 高手请进!

std::string

这个东西很烦人的!

很不好把握

roger_77 at 2006-5-22 22:29:52 >

C/C++

All Classified