【问题标题】:Allocating memory with new in the DLL - how to release?在 DLL 中使用 new 分配内存 - 如何释放?
【发布时间】:2021-05-20 18:22:36
【问题描述】:

我在 DLL 中有许多输出指针的导出函数,它们在这些函数中分配内存。例如:

DLL_EXPORT void some_function(const char*** param)
{
    *param= new const char*[somenumber];
    //someting
    //in some for-cycle
    char* somestr = new char[somenumber1];
    strcpy(somestr , somelocalstr);
    //someting
}

在其他类似项目中使用过(这里就不写LoadLibrary()GetProcAddress()了,已经写好了):

void some_function_that_uses_dll()
{
    const char** param;
    some_function(&param); 
    //something
    //some for-cycle
    const char* somestring = param[i];
    //something
    non_local_std_string = somestring;
}

这就是我收到项目的方式。

这里似乎发生了明显的内存泄漏。但是当我尝试在non_local_std_string = somestring; 之后写delete[] somestring; 时,我遇到了崩溃。可能是因为它是不同的项目。

有没有办法释放那些在 DLL 中分配的内存,在它被复制到std::string (non_local_std_string) 之后?或者,std::string 会移动那些记忆吗?

【问题讨论】:

  • 从你的dll中导出一个调用delete的函数

标签: c++ string c++11 memory dll


【解决方案1】:

不,std::string 不会获得分配内存的所有权。 DLL 的用户在使用完之后负责释放内存,例如将其复制到std::string

在这种情况下,您必须:

  1. 导出一个附加函数,DLL 的用户在使用完数组后必须调用delete[] 数组,例如:
DLL_EXPORT int some_function(char*** param)
{
    *param = nullptr;
    int res = 0;

    try
    {
        *param = new char*[somenumber];
        for(int i = 0; i < somenumber; ++i)
        {
            ...
            char* somestr = new char[somenumber1];
            strcpy(somestr, somelocalstr);
            (*param)[res] = somestr;
            ++res;
        }
    }
    catch (...)
    {
        for(int i = 0; i < res; ++i) {
            delete[] (*param)[i];
        }
        delete[] *param;
        return -1;
    }

    return res;
}

DLL_EXPORT void free_function(char** param, int n)
{
    for(int i = 0; i < n; ++i) {
        delete[] param[i];
    }
    delete[] param;
}
void some_function_that_uses_dll()
{
    char** param;
    int n = some_function(&param); 

    for(int i = 0; i < n; ++i)
    {
        char* somestring = param[i];
        //...
    }

    free_function(param, n);
}
  1. 使用操作系统提供的内存管理函数分配和释放数组,而不是new[]/delete[]。这样,DLL 的用户就可以直接调用 OS 函数,例如:
DLL_EXPORT int some_function(char*** param)
{
    *param = (char**) LocalAlloc(LMEM_FIXED, sizeof(char*) * somenumber);
    if (*param == NULL) return -1;

    int res = 0;

    for(int i = 0; i < somenumber; ++i)
    {
        ...
        char* somestr = (char*) LocalAlloc(LMEM_FIXED, sizeof(char) * somenumber1);
        if (somestr == NULL)
        {
            for(int j = 0; j < res; ++j) {
                LocalFree((*param)[j]);
            }
            LocalFree(*param);
            return -1;
        }
        
        strcpy(somestr, somelocalstr);
        (*param)[res] = somestr;
        ++res;
    }

    return res;
}
void some_function_that_uses_dll()
{
    const char** param;
    int n = some_function(&param); 

    for(int i = 0; i < n; ++i)
    {
        char* somestring = param[i];
        //...
    }

    for(int i = 0; i < n; ++i) {
        LocalFree(param[i]);
    }
    LocalFree(param);
}

【讨论】:

    猜你喜欢
    • 2020-11-09
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 2010-11-23
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多