How to free an IntPtr? RRS feed

  • Question

  • i have following Situation. I'm working on an application, which will Display every Icon inside an arbitrary file. my Problem is , that the application will run out of Memory after i open to much files.

    Following code Displays what i've done:

    IntPtr large = new IntPtr();
    IntPtr small = new IntPtr();
    ExtractIconEx(file, count, out large, out small, 1);
    BitmapSource source =  System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(System.Drawing.Icon.FromHandle((largeiconComboBox.IsChecked == true) ? large : small).ToBitmap().GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
    return source;

    Here i use the ExtractIconEx method, which is imported as follows:

     [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);

    But if i start the prog, an unhandeld exception occurs, and the hole application crashes.

    Know i search inside the MarshalClass and there a function like  Marshal.FreeHGlobal or Marshal.FreeCoTaskMem.

    Both chrashes to and i see now way, to free my Memory.

    Is there a way ?????

    Thursday, February 13, 2014 5:02 PM


All replies