free memory not clears the memory block

前端 未结 3 842
迷失自我
迷失自我 2021-01-16 06:19

I am using DllImport to call method in c wrapper library from my own .net class. This method in c dll creates a string variable and returns the pointer of the string.

<
3条回答
  •  盖世英雄少女心
    2021-01-16 07:06

    Freeing memory doesn't clear it, it just frees it up so it can be re-used. Some debug builds will write over the memory for you to make it easier to find problems with values such as 0xBAADFOOD

    Callers should allocate memory, never pass back allocated memory:

    _declspec(dllexport) int ReturnString(char*buffer, int bufferSize)
    {
        if (bufferSize < 125) {
            return 125;
        } else {
            strcat(buffer, "SOMETEXT");
            strcat(buffer, "SOMETEXT MORE");
            return 0;
        }
    }
    

提交回复
热议问题