-2

I created implementation in MVS without using CRT. I use HeapAlloc() and HeapFree() for allocating memory. My example should work without memory leak.
Here is my code:

LPCSTR byte2ch(BYTE* data, int size) {
    char* datas = (char*)HeapAlloc(GetProcessHeap(), NULL, size);
    LPCSTR temp = (reinterpret_cast<char const*>(data));
    for (int i = 0; i < size; i++) {
        datas[i] = temp[i];
    }
    LPSTR tempo = datas;
    HeapFree(GetProcessHeap(), NULL, (LPVOID)&size);
    return tempo;
}

int EntryPoint()
{
    BYTE* buffer = 0;

    HANDLE hFile;
    DWORD dwBytesRead, dwBytesWritten, dwPos;

    if (hFile = CreateFileW(L"MinerFinder.exe", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL))
    {
        LARGE_INTEGER size;
        GetFileSizeEx(hFile, &size);

        buffer = (BYTE*)HeapAlloc(GetProcessHeap(), NULL, size.QuadPart);
        ReadFile(hFile, buffer, size.QuadPart, &dwBytesRead, NULL);

        MessageBoxA(NULL, byte2ch(buffer, size.QuadPart), NULL, SW_SHOW);
        HeapFree(GetProcessHeap(), NULL, (LPVOID)&size.QuadPart);
        MessageBoxA(NULL, "", NULL, SW_SHOW); // there I can see, that memory is leaking...
        CloseHandle(hFile);
    }

    ExitProcess(0);
}  

Where is my mistake?

EDIT 1:

LPCSTR byte2ch(BYTE* data, int size) {
        char* datas = (char*)HeapAlloc(GetProcessHeap(), NULL, size);
        LPCSTR temp = (reinterpret_cast<char const*>(data));
        for (int i = 0; i < size; i++) {
            datas[i] = temp[i];
        }
        LPSTR tempo = datas;
        HeapFree(GetProcessHeap(), NULL, datas);
        return tempo;
    }

there when I HeapFree() program suddenly crashes. What?

korozya
  • 17
  • 6
  • 2
    `HeapFree` requires a memory block allocated with `HeapAlloc` but you are passing block size casted to pointer instead everywhere. – dewaffled Feb 15 '20 at 07:58
  • 2
    Also you are returning pointer to a memory block right after you are freeing it. This will not work. – dewaffled Feb 15 '20 at 08:02
  • Each and every one of those casts is bad. Some are unavoidable, but at least they should be replaced with `static_cast`. That said, you are supposed to extract a [mcve] from your code and provide that along with your question. As a new user, please also take the [tour] and read [ask]. – Ulrich Eckhardt Feb 15 '20 at 08:08
  • 1
    Concerning your edit, your code still has a terrible bug. You are returning a pointer to memory that's been freed. You should take a step back and learn about object lifetimes and memory management. – Blastfurnace Feb 15 '20 at 09:29

1 Answers1

1

Looking at HeapFree

BOOL HeapFree( HANDLE hHeap, DWORD dwFlags, _Frees_ptr_opt_ LPVOID lpMem );

lpMem

A pointer to the memory block to be freed. This pointer is returned by the HeapAlloc or HeapReAlloc function. If this pointer is NULL, the behavior is undefined.


In the code

HeapFree(GetProcessHeap(), NULL, (LPVOID)&size);
HeapFree(GetProcessHeap(), NULL, (LPVOID)&size.QuadPart);

You don't give an allocated pointer to HeapFree, but a pointer to the address of some unrelated (non-heap) memory.


The proper call would be

HeapFree(GetProcessHeap(), NULL, datas);

or

HeapFree(GetProcessHeap(), NULL, buffer);
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198