findbugs 网络收集

[hyddd的FindBugs分析记录][M X OBL] Method may fail to clean up stream or resource

[M X OBL] Method may fail to clean up stream or resource [OBL_UNSATISFIED_OBLIGATION]

This method may fail to clean up (close, dispose of) a stream, database object, or other resource requiring an explicit cleanup operation.

In general, if a method opens a stream or other resource, the method should use a try/finally block to ensure that the stream or resource is cleaned up before the method returns.

This bug pattern is essentially the same as the OS_OPEN_STREAM and ODR_OPEN_DATABASE_RESOURCE bug patterns, but is based on a different (and hopefully better) static analysis technique. We are interested is getting feedback about the usefulness of this bug pattern. To send feedback, either:

In particular, the false-positive suppression heuristics for this bug pattern have not been extensively tuned, so reports about false positives are helpful to us.

See Weimer and Necula, Finding and Preventing Run-Time Error Handling Mistakes , for a description of the analysis technique.

 

这个BUG想说明的是:有些资源打开了,但在函数结束的时候却没有关闭。比如:数据库连接......

虽然JAVA有垃圾回收机制,但是,自己打开的资源需要自己手动关闭,否则有可能直到程序退出,JRE才会清理你打开的资源。这里FindBugs建议在try/finally里面关闭打开的资源,在关闭之前,还要判断资源是否为Null,或者再加一层异常捕获处理噢~

以下是一些可能关闭资源失败的例子:

-----------------------------------------------情况1-----------------------------------------

 

复制代码
//
FileOutputStream fs  =   null ;
try {
  fs 
=   new  FileOutputStream(clearTableFile);
  fs.close();    
// 当出异常时候fs关闭失败,所以应该在finally中关闭
}
catch ( ){
//
}
复制代码

-----------------------------------------------情况2-----------------------------------------

 

复制代码
//
Properties props  =   new  Properties();
try{
  props.store(
new  FileOutputStream(configFile), configFile);   // new FileOutputStream(configFile)没有释放。
}
catch( ){
  //
}
复制代码

-----------------------------------------------情况3-----------------------------------------

 

  //
  FileOutputStream fs  =   new  FileOutputStream(clearTableFile);
  //     (里没有做异常处理,如果中间出异常了,异常会抛往上层,这时fs就没能释放了。
  fs.close();    
  //


http://www.ibm.com/developerworks/cn/java/j-dyn0414/index.html

http://bcel.sourceforge.net/docs/index.html

http://findbugs.sourceforge.net/api/edu/umd/cs/findbugs/ba/BlockType.html

http://code.google.com/p/findbugs/wiki/DetectorPluginTutorial

http://www.ibm.com/developerworks/java/library/j-findbug1/#resources
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值