使用glib时,未知位置warning的处理

本文介绍如何通过调整G_DEBUG环境变量,利用glib的assert机制提高程序调试效率,特别是在面对非致命性错误时,通过设置G_DEBUG为fatal-warnings或fatal-criticals,使程序在遇到特定警告或错误时中断,便于使用如gdb等调试工具定位问题根源。

开发基于glib的程序时,经常会出现类似如下的log:

** (process:6437): CRITICAL **: xxxxxxxxx: assertion '( func != NULL ) && ( xxx != NULL ) && ( count > 0 )' failed

这时因为某些非致命性的assert失败,从而输出的log,但程序往往仍在运行,该log提供的信息有时又非常有限,无法定位出问题位置。

glib中提供了G_DEBUG这个环境变量,可以通过设置这个环境变量使得该类warning会导致程序trap,使得如gdb一类的调试器能够捕获到程序中断的位置,来加速问题解决。

G_DEBUG的使用如下:

This environment variable can be set to a list of debug options, which cause GLib to print out different types of debugging information.
fatal-warnings        Causes GLib to abort the program at the first call to g_warning() or g_critical().
fatal-criticals          Causes GLib to abort the program at the first call to g_critical().
gc-friendly             Newly allocated memory that isn't directly initialized, as well as memory being freed will be reset to 0. The point here is to allow memory checkers and similar programs that use Boehm GC alike algorithms to produce more accurate results.
resident-modules   All modules loaded by GModule will be made resident. This can be useful for tracking memory leaks in modules which are later unloaded; but it can also hide bugs where code is accessed after the module would have normally been unloaded.
bind-now-modules All modules loaded by GModule will bind their symbols at load time, even when the code uses %G_MODULE_BIND_LAZY.

例如,以如下方式运行程序:

G_DEBUG=fatal-warnings gdb ./a.out

cargo run -- --force-gtk4 Using GTK version: gtk4 AppStateManager 正在运行... 当前应用状态: AppState { counter: 0, data: [], config: Config { theme: "", language: "", auto_save: false } } 性能统计 - 平均读取: 51.11μs, 平均写入: 0.00μs, 读写比例: 1.0:1 检测到写操作增加,建议使用 Mutex 性能统计 - 平均读取: 0.00μs, 平均写入: 0.00μs, 读写比例: 0.0:1 Running main application with multi-threading support and window management. SHA256 digest: SHA256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 Loading user: 1: Alice Loading user: 2: Bob Loading user: 3: Charlie Loaded 3 users from database Database setup completed successfully Starting Linux GTK4 application... Linux GTK4 application activating... Message system initialized ✓ Added cell at row 0, col 0, text: 'Cell 0-0' ✓ Added cell at row 0, col 1, text: 'Cell 0-1' ✓ Added cell at row 0, col 2, text: 'Cell 0-2' ✓ Added cell at row 0, col 3, text: 'Cell 0-3' ✓ Added cell at row 0, col 4, text: 'Cell 0-4' ✓ Added cell at row 1, col 0, text: 'Cell 1-0' ✓ Added cell at row 1, col 1, text: 'Cell 1-1' ✓ Added cell at row 1, col 2, text: 'Cell 1-2' ✓ Added cell at row 1, col 3, text: 'Cell 1-3' ✓ Added cell at row 1, col 4, text: 'Cell 1-4' ✓ Added cell at row 2, col 0, text: 'Cell 2-0' ✓ Added cell at row 2, col 1, text: 'Cell 2-1' ✓ Added cell at row 2, col 2, text: 'Cell 2-2' ✓ Added cell at row 2, col 3, text: 'Cell 2-3' ✓ Added cell at row 2, col 4, text: 'Cell 2-4' ✓ Added cell at row 3, col 0, text: 'Cell 3-0' ✓ Added cell at row 3, col 1, text: 'Cell 3-1' ✓ Added cell at row 3, col 2, text: 'Cell 3-2' ✓ Added cell at row 3, col 3, text: 'Cell 3-3' ✓ Added cell at row 3, col 4, text: 'Cell 3-4' ✓ Added cell at row 4, col 0, text: 'Cell 4-0' ✓ Added cell at row 4, col 1, text: 'Cell 4-1' ✓ Added cell at row 4, col 2, text: 'Cell 4-2' ✓ Added cell at row 4, col 3, text: 'Cell 4-3' ✓ Added cell at row 4, col 4, text: 'Cell 4-4' (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:38:21-23: Percentages are not allowed here (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:65:13-21: No property named "position" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:69:13-16: No property named "top" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:70:13-17: No property named "left" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:71:13-18: No property named "right" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:75:13-19: No property named "bottom" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:76:13-17: No property named "left" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:77:13-18: No property named "right" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:81:13-16: No property named "top" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:82:13-19: No property named "bottom" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:83:13-17: No property named "left" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:87:13-16: No property named "top" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:88:13-19: No property named "bottom" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.778: Theme parser error: <data>:89:13-18: No property named "right" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.779: Theme parser error: <data>:101:13-28: No property named "backdrop-filter" (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.779: Theme parser error: <data>:105:35-40: Percentages are not allowed here (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.779: Theme parser error: <data>:109:35-39: Percentages are not allowed here (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.779: Theme parser error: <data>:113:35-40: Percentages are not allowed here (baota-gtk4:33879): Gtk-WARNING **: 09:31:23.779: Theme parser error: <data>:117:35-39: Percentages are not allowed here (baota-gtk4:33879): GLib-GObject-CRITICAL **: 09:31:23.779: cannot register existing type 'GdkDisplayManager' (baota-gtk4:33879): GLib-CRITICAL **: 09:31:23.779: g_once_init_leave_pointer: assertion 'result != 0' failed (baota-gtk4:33879): GLib-GObject-CRITICAL **: 09:31:23.779: g_object_new_with_properties: assertion 'G_TYPE_IS_OBJECT (object_type)' failed 段错误 (核心已转储) cargo run -- --flutter --force-linux Running `/home/kingjeeps/target/debug/baota-gtk4 --flutter --force-linux` === 运行 Cairo 功能测试 === 当前工作目录: "/media/kingjeeps/stc3/baota-gtk4" 检查路径: Pictures/test_image.png -> 存在: true 检查路径: ../Pictures/test_image.png -> 存在: false 检查路径: ./Pictures/test_image.png -> 存在: true 检查路径: /media/kingjeeps/stc3/baota-gtk4/Pictures/test_image.png -> 存在: true IoError(Custom { kind: Other, error: "测试 IO 错误" }) Image dimensions: 100x100 图像加载成功,尺寸: 100x100 图像颜色类型: Rgb8 Read integer: 1196314761 图像信息: 颜色类型: Rgb8 宽度: 800 高度: 600 Context 绘图操作完成 === 启动 GTK Cairo 应用程序 === 未知选项 --flutter error: unexpected argument '--flutter' found 这个在cargo run无错误,在cargo run -- --flutter --force-linux无法识别:未知选项 --flutter ,然后退出,为什么?
最新发布
10-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值