I am developing a BlackJack program that shows a BlackJack table, maps, etc. The plan is that he will play thousands of hands one after another with an automated strategy.
I have a UserSontrol UserControl that contains an ItemsControl element associated with an ObservableCollection. This CardInHand class contains a BitmapSource named CardImage. When the instance is broken, it loads the map image from the resources using the following code:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
private BitmapSource GenerateCardImage() {
Stream TempStream = this.GetType().Assembly.GetManifestResourceStream("BlackJack.Resources.CardImages.Card_" + m_Card.ShortTitle + ".gif");
System.Drawing.Bitmap sourceBMP = new System.Drawing.Bitmap(TempStream);
BitmapSource tempBitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
sourceBMP.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(sourceBMP.Width, sourceBMP.Height)
);
TempStream.Dispose();
DeleteObject(sourceBMP.GetHbitmap());
return tempBitmapSource;
}
The problem is that after I go through ~ 500 rounds (~ 4,000 hands or ~ 10,000 cards), I get a GDI + error, and the application takes ~ 400 MB of RAM. This is growing rapidly and is related to the number of hands that have been played.
DeleteObject() - , , , Bitmap. , , . Dispose().
, ItemsSource. , . , , . 40 000 (, + 20 40 , ).
Clear() . , CardInHand BitmapSource, .
, , , ?
.