开发者

resource acquisition failure handling [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 8 years ago.

Improve this q开发者_如何学Pythonuestion

After years of programming, I havent had a situation where reasonable malloc or new would fail (maybe because my mallocs are trully reasonable), though I always check for it.

In my case, apps should gracefully (i hope) close with an appropriate log entry. What would you do in this case? Its interesting to hear your approach - do you wait for resources or close the shop?


I usually have my program shut-down as gracefully as it can, with simple logging of the error message. In C++ I do this by having a catch for std::bad_alloc in main(). By the time the catch executes, destructors called by stack unwinding should have freed some memory, so the logging itself is less likely to fail. I avoid memory allocation (for example by using char * strings rather than std::string strings) in that logging code, to further reduce the chance of the logging failing.


There's pretty much nothing you can do if dynamic allocation fails- there are pretty much no operations written to handle that situation. If it fails, then just let the app crash.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜