0

In dotNET we could write unmanaged code, where can I allocate memory dynamically (by keywords: stackalloc, new), use pointers etc.
It's possible to free memory (for example by something like delete in C++)?

Jacek
  • 11,661
  • 23
  • 69
  • 123
  • 5
    Please show an example. This is kind of vague at the moment. – Yuck Aug 01 '12 at 13:33
  • http://stackoverflow.com/questions/2648560/allocating-unmanaged-memory-in-c-sharp the mashler will free memory when it leaves scope. – Travis Aug 01 '12 at 13:36

2 Answers2

7

It depends how you allocate memory.

For example, if you allocate memory with AllocHGlobal :

double* vertices = (double*)Marshal.AllocHGlobal(
                          3 * count *  Marshal.SizeOf(typeof(double)));

You allocate the array of doubles of a given size.

To free that memory after you have to call FreeHGlobal

 Marshal.FreeHGlobal((IntPtr)vertices);

There are also other functions for allocating COM task memory, like

AllocCoTaskMem and relative FreeCoTaskMem

Tigran
  • 61,654
  • 8
  • 86
  • 123
-1

try with this code in order to free

Marshal.FreeHGlobal((IntPtr)vertices);
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51