GCC 9.4 编译error: catching polymorphic type ‘class std::bad_alloc’ by value [-Werror=catch-value=]

环境

lm@lm:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal



lm@lm:~$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.1' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-Av3uEd/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)

编译问题

Control.cc: In member function ‘bool Control::readFromDiskData(std::string, Uint32, std::string, std::string, Uint32, std::string, bool, Uint16, std::string, Uint32, Uint32, std::string, std::string, Uint32, Uint32, Bool, std::string)’:
./xxx/Control.cc:605:15: error: catching polymorphic type ‘class std::bad_alloc’ by value [-Werror=catch-value=]
  605 |    catch(std::bad_alloc)
      |               ^~~~~~~~~

关键源码

部分关键源码如下:

499   try()
500   {
          ....
604   }
605   catch(std::bad_alloc)
606   {
607      SWERR("Memory Allocation Failed");
608   
609      status = memAllocFail_c;
610   
611      if(templetData_p == NULL )
612      {
613         TRACE("Memory allocation failed for templetData_p array");
614      }
615   }
616   return True;

从编译错误可以很清晰的看出就是 catch(std::bad_alloc) 这一行有问题。

寻找问题

查看gcc官网的编译选项 3.5 Options Controlling C++ Dialect 原文摘录如下:

-Wcatch-value
-Wcatch-value=n (C++ and Objective-C++ only) Warn about catch handlers that do not catch via reference. With -Wcatch-value=1 (or
-Wcatch-value for short) warn about polymorphic class types that are caught by value. With -Wcatch-value=2 warn about all class types that
are caught by value. With -Wcatch-value=3 warn about all types that
are not caught by reference. -Wcatch-value is enabled by -Wall.

文中第一句说的就很明显了,必须是引用

解决方案

知道了问题所在,解决就很简单了。

499   try()
500   {
          ....
604   }
605   catch(std::bad_alloc&)
606   {
607      SWERR("Memory Allocation Failed");
608   
609      status = memAllocFail_c;
610   
611      if(templetData_p == NULL )
612      {
613         TRACE("Memory allocation failed for templetData_p array");
614      }
615   }
616   return True;

没错,就只是添加一个 & 符号即可。

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值