簡體   English   中英

在 DLL 中分配 memory - 如何釋放?

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

我在 DLL 和 output 指針中有許多導出函數,它們在這些函數中分配了 memory。 例如:

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;
}

這就是我收到項目的方式。

似乎這里發生了明顯的 memory 泄漏。 但是當我試圖寫delete[] somestring; non_local_std_string = somestring; ,我崩潰了。 可能是因為是不同的項目。

有沒有辦法釋放那些在 DLL 中分配的 memory,在它被復制到std::string ( non_local_std_string ) 之后? 或者, std::string會移動那些 memory 嗎?

不, std::string不會取得分配的 memory 的所有權。您的 DLL 的用戶有責任在使用完 memory 后釋放它,例如將其復制到std::string中。

在這種情況下,您必須:

  1. 導出一個額外的 function,DLL 的用戶在使用完它們后必須調用delete[] arrays,例如:
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. 使用操作系統提供的 memory 管理功能分配和釋放 arrays,而不是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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM