T
T
Ternick2020-12-03 13:53:04
C++ / C#
Ternick, 2020-12-03 13:53:04

How to solve a memory leak issue?

The crux of the matter is what are the options to avoid memory leaks.
As an example, here is an implementation of xor with a memory leak:

TYK
wchar_t* xorCipher(const wchar_t* toEncrypt) {
  HANDLE hHeap = GetProcessHeap();
  if (hHeap != NULL) {
    const int size = _wcslen(toEncrypt);
    wchar_t* result = (wchar_t*)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(wchar_t) * size);
    if (result != NULL) {
      for (int i = 0; i != _wcslen(toEncrypt) + 1; i++) {
        result[i] = toEncrypt[i] ^ (wchar_t)key;
      }
    }
    return result;
  }
}

The problem is that we need to give a pointer to an array of characters, I know that this is generally not correct and everything else is of interest to solutions to this issue.

PS wstring and more is not an option because of NODEFAULTLIB. The first thing that came to mind was to write a class, a wrapper over wchar__t*, are there any other options?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2020-12-03
@gbg

Read about smart pointers

6
6db, 2020-12-03
@6db

I agree with the comment above.

#include <memory>
shared_ptr
unique_ptr

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question