System.Drawing中内存溢出异常 [英] System.Drawing Out of Memory Exception

查看:1198
本文介绍了System.Drawing中内存溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序图形引擎抛出这些异常。它们都是由一个空的catch块消耗。在创业初期,我发现这就是一个不困(我记得用钢笔扩大有关)。我尝试和一个空的catch块包围它。似乎这些例外具有产生在图上没有影响。我已经做了一些这方面的阅读没有真正理解或获取到它的底部



所以,我的问题:




  1. 为什么这些被抛出,如果它们可以安全食用?和

  2. 它是安全的忽略它们?我担心,每个人是有一些隐藏的效果。我有我从来没有发现例如内存泄漏。


解决方案

我见过系统.Drawing抛OutOfMemoryExceptions,即使它不是内存不足。某些GDI +功能显然是刚刚返回一个愚蠢的错误代码。



IIRC,如果您尝试使用一个LinearGradientBrush填充矩形的宽度或高度,你会得到一个OutOfMemoryException零。可能还有其他情况也一样,但是这是我们遇到的主要之一。



在这种情况下,没有必要为一个try / catch。只需添加一个如果语句来绘制代码,不填充矩形如果宽度或高度为零。



更新:据对这个答案,它也可以,如果你尝试加载损坏图片文件出现。对于这一点,你就别无选择,只能做的try / catch。



您是从GDI +可能是安全的醒目OutOfMemoryExceptions,但保留try块尽可能小。考虑记录异常,这样你就可以分析日志,并尽可能增加防御性代码。你不想掩盖的真正的OutOfMemoryException异常,但你不希望一个愚蠢的GDI +错误代码,要么崩溃您的应用程序。


My application graphics engine throws these exceptions. They are all consumed by an empty catch block. In the early days I found one that was not trapped (associated with pen widening as I recall). I surrounded it with try and an empty catch block. It seems that these exceptions have no effect on the drawing produced. I have done some reading on this without really understanding or getting to the bottom of it.

So to my questions:

  1. Why would these be thrown if they can be consumed safely? and
  2. Is it safe to ignore them? I worry that each one is having some hidden effect. I have memory leaks which I have never found for example.

解决方案

I've seen System.Drawing throw OutOfMemoryExceptions even when it's not out of memory. Some GDI+ function is apparently just returning a stupid error code.

IIRC, you will get an OutOfMemoryException if you try to use a LinearGradientBrush to fill a rectangle whose width or height is zero. There may be other conditions too, but this is the main one we ran into.

In that case, there's no need for a try/catch. Just add an if statement to your drawing code, and don't fill the rectangle if the width or height is zero.

Update: According to the comments on this answer, it can also occur if you try to load a corrupted image file. For that, you would have no choice but to do try/catch.

You're probably safe catching OutOfMemoryExceptions from GDI+, but keep the try blocks as small as possible. Consider logging the exceptions, so you can analyze the logs and add defensive code where possible. You don't want to mask a real OutOfMemoryException, but you don't want a stupid GDI+ error code to crash your app either.

这篇关于System.Drawing中内存溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆