动态链接库里分配的内存,在主程序中怎么不能释放。

xiangshenyang 2005-11-15 04:59:30
比如在动态链接库里分配一个空间:
DLL_API char * NewChar(int i)
{
char *p=new char[i];
strncpy(p,"this is a test string",i);
return p;
}

主调程序里释放资源时出错:
char *p=NewChar(100);
cout<<p<<endl;
delete [] p;//错!
怎么回事,怎么解决。
...全文
511 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ddmor 2005-11-21
  • 打赏
  • 举报
回复
还是那句话:动态链接库申请的资源需要由它自己来释放。
比如添加以下成对内存分配和释放代码:
DLL_API char * NewChar(int i)
{
char *p=new char[i];
return p;
}

DLL_API void DeleteChar(char*p)
{
delete [] p;
}
weiym 2005-11-18
  • 打赏
  • 举报
回复
在DLL再加一个释放的函数就解决所有问题了,EXE中要释放时就调用这个函数
xiangshenyang 2005-11-17
  • 打赏
  • 举报
回复
我使用的是动态链接库,当然是动态链接啊,按你的说法不会出错的喔,但是还是释放资源出错喔
GoAround 2005-11-17
  • 打赏
  • 举报
回复
如果你用new/delete(malloc/free)来分配/释放内存,实际上是CRT在帮你管理heap。

如果两个模块都是动态连接到CRT,运行时CRT库的实例只有一个,也就是只有一个堆,因此不会有问题。但是如果有一个或多个模块静态链接到CRT,那么CRT库的实例会有多个,也就会有多个heap,这时应该遵循“谁分配谁释放”的原则
woshizei 2005-11-17
  • 打赏
  • 举报
回复
DLL和主调程序既然是分别编译的,那么编译时他们必然要使用2个不同的堆.各自管理各自的.将DLL中申请到的堆内存的指针交给主调程序去释放时,主调程序的堆管理部分(我猜想的,应该有对堆的管理)就会发现这个指针不合法.就失败了.
可以通过在DLL中VirtualAlloc在程序地址空间中申请一段空间,然后在然后在主调程序中通过VirtualFree释放.
GoAround 2005-11-17
  • 打赏
  • 举报
回复
xiangshenyang兄弟,我假定你用的是VC6,创建DLL时没有修改工程属性。请你检查:
Project->Settings,选C/C++属性页,Category选Code generation,看看Use runtime library一项是什么?是Debug/Release Multithread吧?再用同样的方法看EXE,一样吗?是不是后面多了个DLL?

如果有兴趣,你可以跟踪一下EXE和DLL里new和delete的源代码,注意CRT代码中的_crtheap这个变量,那就是heap handle。
taianmonkey 2005-11-16
  • 打赏
  • 举报
回复
从理论上讲,动态链接库加载到进程空间中,也是作为进程的一个线程来调用,因此本进程完全是可以释放动态库new的内存!
huangwc 2005-11-16
  • 打赏
  • 举报
回复
strncpy(p,"this is a test string",i);

nod
这个不会帮你结尾的
gyscsdn 2005-11-16
  • 打赏
  • 举报
回复
学习
GoAround 2005-11-16
  • 打赏
  • 举报
回复
我知道一个情况可以导致这个问题,如果dll和exe链接到CRT的方式不同,例如,如果dll静态链接到CRT而exe动态链接到CRT,这两个模块将使用不同的heap,就会导致异常产生。
xiangshenyang 2005-11-16
  • 打赏
  • 举报
回复
csdn真是没有高手了。楼上几位也犯低级错误,这根本就不关strncpy什么事情,因为本来出错前没有cout,后来出错后加入strncpy是为了测试用。

DLL_API char * NewChar(int i)
{
char *p=new char[i];
//strncpy(p,"this is a test string",i);//后来才添加的
return p;
}
主调程序里释放资源时出错:
char *p=NewChar(100);
//cout<<p<<endl;
delete [] p;//错!
DentistryDoctor 2005-11-15
  • 打赏
  • 举报
回复
p没加上null character会导致cout时就异常了,而不是delete []造成的异常。
DentistryDoctor 2005-11-15
  • 打赏
  • 举报
回复
Remarks
The strncpy function copies the initial count characters of strSource to strDest and returns strDest.

=============================================================
If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string.
=============================================================

If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.


看到strncpy的说明了吗?
strncpy并不会自动在拷贝时加上null character的,所以

DLL_API char * NewChar(int i)
{
char *p=new char[i];
memset(p,0,i);
strncpy(p,"this is a test string",i);
return p;
}
ddmor 2005-11-15
  • 打赏
  • 举报
回复
动态链接库申请的资源需要由它自己来释放。
zkx2321 2005-11-15
  • 打赏
  • 举报
回复
好像根本就不应该在那里释放。

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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