ImageList capacity bug in .NET ImageList implementation?

廉价感情. 提交于 2019-12-06 04:27:20

I can get no repro whatsoever with the posted code. It is however important to realize that ImageList makes a copy of the bitmap. So you should call the bitmap's Dispose() method after adding it. If you don't then there's a serious risk that you'll run out of unmanaged memory, consumed by the bitmap data and fatal when the garbage collector doesn't run often enough to clean up after you. This doesn't apply to the snippet since there are only 10 bitmaps.

When you use Reflector to look what your last line of code does, you'll see, that setter of ImageList is actually failing on this line:

bool flag2 = SafeNativeMethods.ImageList_Replace(new HandleRef(this.owner, this.owner.Handle), index, new HandleRef(null, handle), new HandleRef(null, monochromeMask));

It involves working with handles, which are in Windows limited (10000 per process if I recall correctly). So although you have a lot of free memory, you have probably reached limit of handles.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!