Dynamic Array Question

Dax dax at mailinator.com
Tue Sep 20 11:06:34 PDT 2011


Hi!
I'm working on a library written in D.
After some tests I have discovered that my library leaks memory, those leaks are caused by dynamics array that I use in my library.

My question is:
Should dynamics array be deallocated automatically when a procedure returns? There is another way to acomplish this?

Maybe I'm doing something wrong, so, I post the function that causes the leak:

public string getWindowText(HWND hWnd)
{
  int len = GetWindowTextLengthW(hWnd);
  wchar[] t = new wchar[len + 1]; // How to deallocate this?

  GetWindowTextW(hWnd, t.ptr, len);

  /*
   * I'm converting the wchar[] to char[],
   * the variable 't' should be deallocated
   * because I not need it anymore.
   */
  return to!(string)(t[0..len]);
}

Thanks,
Dax


More information about the Digitalmars-d-learn mailing list