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=focallm@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;
没错,就只是添加一个 &
符号即可。
GCC 9.4 编译error: catching polymorphic type ‘class std::bad_alloc’ by value [-Werror=catch-value=]相关推荐
- c++:警告:warning: catching polymorphic type ‘class std::exception’ by value
文章目录 警告 gcc 选项 原因 警告 exception.cpp: In function 'int main()': exception.cpp:14:17: warning: catching ...
- 编译错误syntax error : missing ';' before 'type'原因探寻
在VC6中运行以下代码 //main.c #include <stdio.h> int main() { chara[100]; memset(&a, 0, 100); charb ...
- WPF工程出现编译错误:error CS0426: The type name 'Views' does not exist in the type 'Calibration'
本人新建了一个Calibration工程,并添加了用户控件和Window窗体,但是当我在Window窗体的xml文件中设置关闭事件时: Closing="Window_Closing&quo ...
- urllib.error.URLError:urlopen error unknown url type:https 解决办法
前言: 在用Python3做爬虫实验的时候报了这个错误:urllib.error.URLError: <urlopen error unknown url type: https> 原因是 ...
- CUDA编译器nvcc可能遇到:error: asm operand type size(8) does not match type/size implied by constraint ‘r‘
0. 问题描述 该问题产生于我在ptcharm中试图用cupy加速numpy计算,进行一些简单但是繁琐的数据处理,这个问题和cupy关系不大,cupy只要照着nvcc -v给出的版本号对应着下载就ok ...
- 问题记录:multiple definition of `xxxx` 问题解决 struct定义类的error:“unknown type name“
一.multiple definition of xxxx 问题解决 问题背景 我在一个头文件里面定义了一个变量,并赋予初值,然后再两个.c 文件里引入了这个头文件,结果就报错 multiple de ...
- linaro gcc 交叉编译链编译
linaro gcc 交叉编译链编译全解析 在此编译的是4.8.3版本的gcc,依赖2.18的glibc,2.24的binutils 参考http://blog.chinaunix.net/uid-2 ...
- [error] Keil:..\SYSTEM\usart\usart.c(38): error: #260-D: explicit type is missing (“int“ assumed)
问题描述 在学习正点原子寄存器建立工程的时候,在对最终的文件进行编译,但是出现了:..\SYSTEM\usart\usart.c(38): error: #260-D: explicit type i ...
- 编译器:GNU工具链GCC编译器的编译方法和编译步骤
https://www.runoob.com/w3cnote/working-process-of-the-compiler.html https://www.runoob.com/w3cnote/g ...
最新文章
- Android新浪微博登陆和获取个人信息
- Realm Mobile Platform添加水平可扩展性,支持遗留数据源和复制
- PHP: How to print a debug log?
- 如何将 SAP Business Application Studio 里开发的 Java 应用部署到 SAP BTP 上
- java8 streams_另一个Java 8 Lamdbas和Streams示例
- Joi验证模块的使用
- 用dynamic增强C#泛型表达力
- 非常有用的101道算法部分常见面试题
- 算法题:(1) 有一个集合R = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, ],....请写出求解这样的一个子集的通用算法。
- Java面试题(三)TCPIP协议
- HDTunePro v5.00 硬盘专业工具(绿色版)
- Linux7网卡绑定后mac一样,如何解决双网卡bond0绑定模式物理成员口的mac地址和bonding接口mac地址不一致......
- 这个开源项目有点强,无需编码,可一键生成前后端代码
- 金融(银行)有关业务总结
- 专题论坛:云计算安全论坛
- 异常问题解决方案经验总结
- 3.线程安全之可见性、有序性、原子性是什么?
- SAP中外协加工BOM物料消耗问题处理实例
- EDG一追一追平LGD,四保一阵容的Iboy从来不会让我们失望!
- node pkg 打包一个为一个可执行程序(linux、windows、mac)