Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to free an IntPtr?

$
0
0

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());

LocalFree(large);
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 ?????

 

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>