How to free allocated memory in C++ DLL

Action Heinz

i've got following code to encrypt a string in a C++ DLL

EXPORT WCHAR* EncryptString(WCHAR* stringToEncrypt) {
    aes_context ctx;

    WCHAR* in = stringToEncrypt;
    WCHAR* out;
    WCHAR* key = L"TestKey";

    BYTE* buffEnc = (BYTE*)malloc(16);
    BYTE* keyBuffEnc = (BYTE*)malloc(32);

    memset(buffEnc, 0, 16);
    memset(keyBuffEnc, 0, 32);

    memcpy(buffEnc, in, wcslen(in) * 2);
    memcpy(keyBuffEnc, key, wcslen(key) * 2);
    aes_set_key(&ctx, keyBuffEnc, 256);

    aes_encrypt(&ctx, buffEnc, buffEnc);
    out = (WCHAR*)buffEnc;

    // free(buffEnc);   
    // free(keyBuffEnc);

    return out;
}

My problem is that i can not free the allocated memory because otherwise the result is broken. I wonder how can i free the used memory without losing the result? Have i to change the type of return value?

Thanks in advance for your help. Greets Heinz

sharptooth

This is indeed a problematic situation - you return a pointer to allocated memory and it's unclear who should free the memory. You have the following options:

  1. tell the caller free the memory using free() - this will only work if they use the same heap which is hard to guarantee. This is very unreliable and not really recommended.
  2. introduce a memory management interface (such as freeEncrypted() function that is implemented in your library) and tell the caller use it - then memory will be returned to the right heap.
  3. use something well known like CoTaskMemAlloc() for allocation and tell the caller to use the matching function such as CoTaskMemFree() for freeing memory. This is similar to point 2, just uses a well known common memory manager.
  4. change the interface such that it accepts pointer to already allocated data and its size so that the caller both allocates and frees the memory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Free allocated memory in C

From Dev

how to free memory in C that was allocated using C++ "new" operator

From Dev

How to free memory in C# that is allocated in C++

From Dev

How to free allocated memory in a recursive function in C++

From Dev

Free all the memory allocated by malloc(), realloc() in C

From Dev

C linked list - when to free allocated memory

From Dev

Could not Free Realloc Allocated Memory in C

From Dev

How to free memory allocated to local static pointer

From Dev

How to free the memory allocated by Gdiplus::Bitmap::FromFile

From Dev

How to free the memory allocated by cdev_alloc?

From Dev

How to free the memory allocated by cdev_alloc?

From Dev

How to 'free' memory allocated to a variable in shell

From Dev

free with dynamically allocated memory

From Dev

How memory is allocated to macros in c?

From Dev

How to properly use memory dynamically allocated in C++ dll within Matlab

From Dev

How free up memory allocated by lua_newuserdata with delete operator?

From Dev

How to free memory allocated by native method on Java side?

From Dev

how to free dynamic memory allocated inside function after finishing execution

From Dev

Is it safe to free() memory allocated by new?

From Dev

Is it safe to free() memory allocated by new?

From Dev

free dynamically allocated memory in QT

From Dev

How are C# const members allocated in memory?

From Dev

How to free memory to a pointer in c

From Dev

How to determine free memory in Mono/C# in Linux (without external DLL)

From Dev

dynamic memory allocation in c , free some part of memory that is allocated before using malloc()

From Dev

dynamic memory allocation in c , free some part of memory that is allocated before using malloc()

From Dev

How should I free a C# byte[] allocated in Rust?

From Java

Is it ever OK to *not* use free() on allocated memory?

From Dev

What is the scope of free() in dynamically allocated memory?

Related Related

  1. 1

    Free allocated memory in C

  2. 2

    how to free memory in C that was allocated using C++ "new" operator

  3. 3

    How to free memory in C# that is allocated in C++

  4. 4

    How to free allocated memory in a recursive function in C++

  5. 5

    Free all the memory allocated by malloc(), realloc() in C

  6. 6

    C linked list - when to free allocated memory

  7. 7

    Could not Free Realloc Allocated Memory in C

  8. 8

    How to free memory allocated to local static pointer

  9. 9

    How to free the memory allocated by Gdiplus::Bitmap::FromFile

  10. 10

    How to free the memory allocated by cdev_alloc?

  11. 11

    How to free the memory allocated by cdev_alloc?

  12. 12

    How to 'free' memory allocated to a variable in shell

  13. 13

    free with dynamically allocated memory

  14. 14

    How memory is allocated to macros in c?

  15. 15

    How to properly use memory dynamically allocated in C++ dll within Matlab

  16. 16

    How free up memory allocated by lua_newuserdata with delete operator?

  17. 17

    How to free memory allocated by native method on Java side?

  18. 18

    how to free dynamic memory allocated inside function after finishing execution

  19. 19

    Is it safe to free() memory allocated by new?

  20. 20

    Is it safe to free() memory allocated by new?

  21. 21

    free dynamically allocated memory in QT

  22. 22

    How are C# const members allocated in memory?

  23. 23

    How to free memory to a pointer in c

  24. 24

    How to determine free memory in Mono/C# in Linux (without external DLL)

  25. 25

    dynamic memory allocation in c , free some part of memory that is allocated before using malloc()

  26. 26

    dynamic memory allocation in c , free some part of memory that is allocated before using malloc()

  27. 27

    How should I free a C# byte[] allocated in Rust?

  28. 28

    Is it ever OK to *not* use free() on allocated memory?

  29. 29

    What is the scope of free() in dynamically allocated memory?

HotTag

Archive