是否在dll中动态分配内存并尝试将其释放到dll之外是否会导致内存泄漏? [英] Does dynamically allocating memory in a dll and attempting to release it outside the dll amount to memory leak?

查看:77
本文介绍了是否在dll中动态分配内存并尝试将其释放到dll之外是否会导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c风格的dll中动态分配内存并尝试在dll之外释放它是否会导致内存泄漏?

解决方案

是的。内存泄漏将发生。

因为以c风格分配内存将取决于crt-heap,而crt-heap是在模块初始化开始时初始化的全局变量值。

因此,如果你在A模块中分配内存,但在B模块中释放它,它将找不到正确的堆句柄,并且发生内存泄漏。


A内存泄漏就是当你忘记分配和释放内存时,所以如果你跟踪你的分配,你就不会有泄漏。


hi,

基本的经验法则是分配记忆的人,也应该释放它。它是一个有用的指南,有助于避免麻烦。简单地说,如果已经在dll外部分配了内存并且其句柄作为输入传递给dll,则最初应该释放该memroy分配的实体应该释放它。如果内存分配完成,比如说dll中的函数,那么最好从dll本身中释放它。无论如何更多的谷歌搜索应该在这里有所帮助。

请看一下这个问题 [ ^ ]

Does dynamically allocating memory in a c-style dll and attempting to release it outside the dll amount to memory leak?

解决方案

Yes. Memory leak will happen.
Because allocating memory in c-style will depend on the crt-heap, and the crt-heap is a global variable value that initialized at the beginning of module initialization.
So, if you allocate memory in A module, but free it in B module, it won't find the correct handle of heap, and memory leak happens.


A memory leak is when you loose track of allocating and releasing memory, so if you keep track of your allocations then you won't have a leak.


hi,
The basic rule of thumb is 'whoever allocated the memory, should free it too' . Its a helpful guideline which helps avoid trouble. Simply put, if memory was already allocated outside the dll and its handle is passed as input to the dll, then whichever entity did this memroy allocation,initially, should free it . And if the memory allocation was done ,say by a function inside the dll, then it is better to free it from within the dll itself. Anyways more googling should be helpful here.
Please take a look at this question[^]


这篇关于是否在dll中动态分配内存并尝试将其释放到dll之外是否会导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆