mercredi 17 février 2021

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

I have many export functions in a DLL that output pointers, and they allocate memory in those functions. For example:

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
}

it's used in other projects like this (I won't write LoadLibrary(), GetProcAddress() here, it's already done):

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

It's how I received the project.

It seems that obvious memory leaks are happening here. But when I tried to write delete[] somestring; after the non_local_std_string = somestring;, I've got a crash. Probably because it is different projects.

Is there a way to free those memory that have been allocated in the DLL, after it would have been copied into the std::string (non_local_std_string)? Or, does std::string move those memory?

Aucun commentaire:

Enregistrer un commentaire