dll的一个问题。

fbms3 2006-01-02 11:00:12

dll中声明下面的函数。
vector<string> __stdcall test(vector<string> vs)
{
vs.push_back("test");
return vs;
}
在exe中动态调用,编译不会报错,但是打开codeguard会报用new创建的一个对象没有删除。为什么呢?
exe的代码:
vector<string> vs;
vs=test(vs);
...全文
180 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang2006 2006-01-25
  • 打赏
  • 举报
回复
vector是不是需要传引用呢
vector<string> __stdcall test(vector<string> &vs)
fbms3 2006-01-06
  • 打赏
  • 举报
回复
to leonatcs(LeonKennedy)(每天逛一逛csdn,有益身心健康。)
我是动态加载的.
fbms3 2006-01-03
  • 打赏
  • 举报
回复
不行不行。更严重了,2个resource leak了。
看来是bcb的问题。
zephyr007 2006-01-03
  • 打赏
  • 举报
回复
如果编译Exe时选择了use dynamic RTL,编译dll时也必须使用相同的编译选项
fbms3 2006-01-03
  • 打赏
  • 举报
回复
好,我试试。
leonatcs 2006-01-03
  • 打赏
  • 举报
回复
理解那段话简单,但是和这个问题没什么关系啊。
http://blog.csdn.net/leonatcs/archive/2005/05/21/377293.aspx

我在BCB6下做实验,好像没事。
我用的是Waiting4you(毛毛)的传饮用的方法,静态加载。
ccrun.com 2006-01-02
  • 打赏
  • 举报
回复
函数定义的不怎么合适吧。我认为不需要返回vector,传入参数时用传址方式就可以了嘛。
fbms3 2006-01-02
  • 打赏
  • 举报
回复
各位,是string不是String。

zephyr007() ( ) 信誉:100 你的说法是什么意思。我都是
#include <vector>
#include <string>
using namespace std;

zephyr007 2006-01-02
  • 打赏
  • 举报
回复
可能是编译exe和dll时使用的运行库不一致
constantine 2006-01-02
  • 打赏
  • 举报
回复
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

所以最好不要用AnsiString做参数或是返回值
编译失败 2006-01-02
  • 打赏
  • 举报
回复
BCB不推荐返回值或参数包含String,你用char *代替看看
Waiting4you 2006-01-02
  • 打赏
  • 举报
回复
偶刚在BCB2006下面试了一下,用CG时BCB会有一个提示:
Two different CRTLDLLs are loaded. CG might report false errors
(C:\WINDOWS\system32\CC3270MT.DLL)
(C:\WINDOWS\system32\CC3270.DLL)
不知道是不是这原因.
我想,也许,大概,可能是exe的和dll的在调试时调用了不同的内存管理模块.偶在BCB2006里被一大堆的设定弄晕了,正在找关于这个的设置中...
BTW,BCB2006的帮助怎么学VS了,偶还是喜欢原来的那种,呵呵
Waiting4you 2006-01-02
  • 打赏
  • 举报
回复
改成这样试试,应该不是传String的问题,毕竟string是c++标准库的,BCB不会连这个都搞不定

vector<string> & __stdcall test(vector<string> &vs)
{
vs.push_back("test");
return vs;
}

也有可能是CG误报.
fbms3 2006-01-02
  • 打赏
  • 举报
回复
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you

这段话看来不是废话,但是我没能理解
fbms3 2006-01-02
  • 打赏
  • 举报
回复
改成传址也是一样的。
只好钩上use dynamic RTL
codeguard就没言语了
看来不用char的传递都要用borlndmm.dll

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧