在 DLL 中使用 new 分配内存 - 如何释放?

Allocating memory with new in the DLL - how to release?

我在一个输出指针的 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 是否移动了那些内存?

不,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. 使用 OS 提供的内存管理函数分配和释放数组,而不是 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);
}