Bfd中的内存泄漏

 

快乐虾

http://blog.csdn.net/lights_joy/

lights@hb165.com

   

本文适用于

bfinutils-2.19

vs2008

 

欢迎转载,但请保留作者信息

 

bfd读取静态库文件时,有一个内存泄漏,以下是简单的测试代码:

     bfd* f = ::bfd_openr("e://etools//debug//libcommon.a", NULL);

     ::bfd_check_format(f, ::bfd_archive);

     ::bfd_close(f);

这个时候有一堆的内存块没有释放,用_CrtMemDifference检查可以发现:

0 bytes in 0 Free Blocks.

28800 bytes in 9 Normal Blocks.

48 bytes in 2 CRT Blocks.

0 bytes in 0 Ignore Blocks.

0 bytes in 0 Client Blocks.

Largest number used: 52500 bytes.

Total allocations: 52604 bytes.

也就是有28800个字节的内存泄漏!查一下bfd读取静态库的代码,在bfd_generic_archive_p函数中有这样的代码段:

 

const bfd_target *

bfd_generic_archive_p (bfd *abfd)

{

………………………………..

 

  if (bfd_has_map (abfd))

    {

      bfd *first;

 

      /* This archive has a map, so we may presume that the contents

      are object files.  Make sure that if the first file in the

      archive can be recognized as an object file, it is for this

      target.  If not, assume that this is the wrong format.  If

      the first file is not an object file, somebody is doing

      something weird, and we permit it so that ar -t will work.

 

      This is done because any normal format will recognize any

      normal archive, regardless of the format of the object files.

      We do accept an empty archive.  */

 

      first = bfd_openr_next_archived_file (abfd, NULL);

      if (first != NULL)

     {

       first->target_defaulted = FALSE;

       if (bfd_check_format (first, bfd_object)

           && first->xvec != abfd->xvec)

         {

           bfd_set_error (bfd_error_wrong_object_format);

           bfd_ardata (abfd) = tdata_hold;

           return NULL;

         }

       /* And we ought to close `first' here too.  */

     }

    }

 

  return abfd->xvec;

}

在这里,bfd先从文件头判断是否一个静态库文件,在得到肯定的结果后它将读取库中的第一个文件,用first这个变量来保存结果,但是在这里bfd并没有释放这个读取结果,这也是造成内存泄漏的原因!

按它的注释,在这里加上

       bfd_close(first);

内存泄漏检测结果变成了:

0 bytes in 0 Free Blocks.

184 bytes in 2 Normal Blocks.

48 bytes in 2 CRT Blocks.

0 bytes in 0 Ignore Blocks.

0 bytes in 0 Client Blocks.

Largest number used: 52500 bytes.

Total allocations: 52604 bytes.

还有184个字节的内存泄漏!估计是bfd_openr_next_archived_file这个函数仍然有问题!

呵呵,代码决定一切,还是别迷信的好!

 

 

 

参考资料

bfd对多目标的支持 2008-9-25

bfdelf32格式的支持( 2008-11-7 )

objdumpreadelf的区别( 2008-11-10 )

objdump代码分析( 2008-11-10 )

bfd的文件格式识别 2008-11-10

bfd的后端数据表示( 2008-11-10 )

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌云阁主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值