MFC下VECTOR释放debug错误

文章讨论了在使用静态CRT时,DLL与Client间通过STL向量作为引用参数传递的问题。解释了DLL分配内存而Client尝试释放导致的运行时错误,并提供了将DLL运行时库更改为MDd的解决方案,确保了内存管理的一致性。

If your DLL and Exe use the static CRT, this won't work properly. Use the DLL CRT in both Exe and DLL. (/MD or /MDd)

 Unjedai wrote:

Can I have the following method in a DLL and call it from a Client, like this?

// DLL file

void DllFunction (std::vector<int> &B)
{
  B.push_back(1);
}

 ----------------------

// Client file

void SomeFunction ()
{
  std::vector<int> A;

  DllFunction(A);
}

When I try to do this I get a runtime error in _CrtIsValidHeapPointer (dbgheap.c) when SomeFunction returns.  I figured the problem is that the DLL allocates the memory and the Client is trying to free it and that's bad.

So, what do people do when they want to pass an STL vector as a reference parameter to a DLL?  Cannot such a basic thing be done?

I've made sure my runtime libraries are the same for the dll and the client (/MTd).

参考此处得到解决
项目属性->c/c++->code generation->runtime library 改为MDd;
项目属性->general->use of mfc->use mfc in a shared dll

转载于:https://www.cnblogs.com/mysunnyday/archive/2011/08/06/2129327.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值