Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

Glib Debugging Macros

Posted on April 8, 2012

glib has a nice set of macros you can use to enforce invariants and preconditions in your code. GTK+ uses these liberally—one of the reasons it’s so stable and easy to use. They all disappear when you define G_DISABLE_CHECKS or G_DISABLE_ASSERT, so there’s no performance penalty in production code. Using these liberally is a very, very good idea. You’ll find bugs much faster if you do. You can even add assertions and checks whenever you find a bug to be sure the bug doesn’t reappear in future versions—this complements a regression suite. Checks are especially useful when the code you’re writing will be used as a black box by other programmers; users will immediately know when and how they’ve misused your code.

Of course you should be very careful to ensure your code isn’t subtly dependent on debug-only statements to function correctly. Statements that will disappear in pro- duction code should never have side effects.

[code lang=”cpp”]
#include <glib.h>

g_return_if_fail(condition);
g_return_val_if_fail(condition, retval);
[/code]
Figure 2-3. Precondition Checks

Figure 2-3 shows glib’s precondition checks. g_return_if_fail() prints a warning
and immediately returns from the current function if condition is FALSE. g_return_val_if_fail()

is similar but allows you to return some retval. These macros are incredibly useful—if
you use them liberally, especially in combination with GTK+’s runtime type check- ing, you’ll halve the time you spend looking for bad pointers and type errors.

Using these functions is simple; here’s an example from the glib hash table imple- mentation:
[code lang=”cpp”]
void
g_hash_table_foreach (GHashTable *hash_table,
GHFunc func,
gpointer user_data)
{
GHashNode *node;
gint i;

g_return_if_fail (hash_table != NULL);
g_return_if_fail (func != NULL);

for (i = 0; i < hash_table->size; i++)
for (node = hash_table->nodes[i]; node; node = node->next)
(* func) (node->key, node->value, user_data);
}
[/code]

Without the checks, passing NULL as a parameter to this function would result in a mysterious segmentation fault. The person using the library would have to figure out where the error occurred with a debugger, and maybe even dig in to the glib code to see what was wrong. With the checks, they’ll get a nice error message telling them that NULL arguments are not allowed.
[code lang=”cpp”]
#include <glib.h>

g_assert(condition);
g_assert_not_reached(void);
[/code]
Figure 2-4. Assertions

glib also has more traditional assertion macros, shown in Figure 2-4. g_assert() is basically identical to assert(), but responds to G_DISABLE_ASSERT and behaves consistently across all platforms. g_assert_not_reached() is also provided; this is

an assertion which always fails. Assertions call abort() to exit the program and (if your environment supports it) dump a core file for debugging purposes.
Fatal assertions should be used to check internal consistency of a function or library, while g_return_if_fail() is intended to ensure sane values are passed to the pub- lic interfaces of a program module. That is, if an assertion fails, you typically look for a bug in the module containing the assertion; if a g_return_if_fail() check fails, you typically look for the bug in the code which invokes the module.

This code from glib’s calendrical calculations module shows the difference:
[code lang=”cpp”]
GDate*
g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
{
GDate *d;
g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);

d = g_new (GDate, 1);

d->julian = FALSE;
d->dmy = TRUE;

d->month = m;
d->day = day;
d->year = y;

g_assert (g_date_valid (d));

return d;
}
[/code]

The precondition check at the beginning ensures the user passes in reasonable values for the day, month and year; the assertion at the end ensures that glib constructed a sane object, given sane values.

g_assert_not_reached() should be used to mark “impossible” situations; a com- mon use is to detect switch statements that don’t handle all possible values of an enumeration:
[code lang=”cpp”]
switch (val)
{
case FOO_ONE:
break;
case FOO_TWO:
break;
default:
/* Invalid enumeration value */
g_assert_not_reached();
break;
}
[/code]

All of the debugging macros print a warning using glib’s g_log() facility, which means the warning includes the name of the originating application or library, and you can optionally install a replacement warning-printing routine. For example, you might send all warnings to a dialog box or log file instead of printing them on the console.

Source: Havoc Pennington. 1999. GTK Gnome Application Development. New York: New Riders Publishing

Terbaru

  • Bingung Pilih Paket? Inilah Perbedaan Telkomsel Data dan Telkomsel Data Flash yang Wajib Kalian Tahu!
  • Ini Alasan Pohon adalah Mahluk Hidup Terbesar di Dunia
  • Sempat Panas! Kronologi Perseteruan Cak Ji vs Madas di Surabaya, Gini Endingnya
  • Gila! Norwegia Bikin Terowongan Melayang di Bawah Laut
  • Cuma Terpisah 20 Mil, Kenapa Hewan di Bali dan Lombok Beda Total? Ternyata Ini Alasannya
  • Heboh Video Umari Viral 7 Menit 11 Detik dari Pakistan, Isinya Beneran Ada atau Cuma Jebakan Link? Cek Faktanya!
  • Tertipu Online atau HP Hilang? Ini Cara Melacak Nomor HP yang Nggak Tipu-Tipu!
  • Apakah Aplikasi Labora Bisa Hasilkan Uang Jutaan atau Cuma Tipu-Tipu?
  • Apakah APK Cashcash Pro Penipu? Cek Reviewnya Dulu
  • Google Kliks Communicator: Pengalaman Awal yang Mengejutkan – Apa yang Kami Rasakan?
  • Belum Tahu? Ini Ukuran Banner YouTube yang Pas Biar Channel Kalian Makin Kece!
  • Inilah Rencana Gila China Balap AS ke Bulan Sebelum 2030
  • Inilah Spesifikasi Resmi Macbook Pro M5 yang Masuk Indonesia
  • Nonton Drama Bisa Dapat Cuan? Simak Cara Main Yudia APK Biar Nggak Salah Langkah!
  • Cuma Nonton Drama Pendek Bisa Cair Saldo DANA? Cobain Aplikasi Melolo, Begini Caranya!
  • Nonton Drama Bisa Dapat Cuan? Jangan Senang Dulu, Simak Fakta Mengejutkan Aplikasi Cash Drama Ini!
  • Ini Video Asli Bocil Block Blast 1 VS 3 di TikTok, Awas Jangan Asal Klik Link Sembarangan!
  • Instapop Terbukti Membayar atau Cuma Tipuan? Cek Faktanya Sebelum Kalian Buang Waktu Main Game!
  • Geger Video Botol Coca Cola 24 Detik di TikTok, Jangan Asal Klik! Ini Fakta Sebenarnya
  • Apa itu Video Botol Makarizo Viral? Ini Faktanya
  • Apa itu Merge Fruit Master? Ini Pengertian dan Cara Main Game yang Katanya Bisa Menghasilkan Saldo
  • Apa itu Shortbox? Ini Pengertian dan Cara Menghasilkan Uang di Aplikasinya
  • Masih Bingung Bedanya CRM dan ERP? Ini Tips Cari Software CRM dan ERP Terbaik
  • Begini Cara Hapus Akun DANA Premium Secara Permanen!
  • Bukan Cuma Mitos! Ternyata Dinasti Song Punya Tempat Nongkrong Mirip PIK, Begini Isinya
  • Bukan Fiksi! Ini Bukti Manusia Punya ‘Kekuatan Super’ Akibat Mutasi Genetik dan Lingkungan
  • Dikelilingi Cincin Api, Kok Kalimantan Malah Nggak Punya Gunung Berapi? Ini Jawabannya!
  • Misteri Newgrange: Kuburan Batu 5.000 Tahun yang Menyimpan Peta Galaksi, Kok Bisa?
  • Transformasi Ekstrem Pesawat IL-96: Ganti 4 Mesin Jadi 2 Mesin Raksasa PD-35, Emang Bisa?
  • Apakah Aplikasi Cash Reels Penipu?
  • Apa itu Cosmic Desktop: Pengertian dan Cara Pasangnya di Ubuntu 26.04?
  • Apa Itu Auvidea X242? Pengertian Carrier Board Jetson T5000 dengan Dual 10Gbe
  • Elementary OS 8.1 Resmi Rilis: Kini Pakai Wayland Secara Standar!
  • Apa Itu Raspberry Pi Imager? Pengertian dan Pembaruan Versi 2.0.3 yang Wajib Kalian Tahu
  • Performa Maksimal! Ini Cara Manual Update Ubuntu ke Linux Kernel 6.18 LTS
  • Inilah Cara Melatih AI Agent Agar Bisa Belajar Sendiri Menggunakan Microsoft Agent Lightning
  • Tutorial Optimasi LangGraph dengan Node-Level Caching untuk Performa Lebih Cepat
  • Tutorial Membuat Game Dengan LangChain
  • X Terancam Sanksi Eropa Gara-Gara AI Grok Bikin Deepfake Anak Kecil
  • Nano Banana 2 Flash Itu Apa Sih? Ini Penjelasan Lengkapnya
  • Inilah Risiko Fatal yang Mengintai Kreator OnlyFans, Dari Doxxing sampai Penipuan!
  • Apa itu Kerentanan FortiCloud SSO? Ini Pengertian dan Bahayanya
  • Apa itu Covenant Health Data Breach? Ini Pengertian dan Kronologi Lengkapnya
  • Apa Itu Integrasi Criminal IP dan Cortex XSOAR? Ini Definisinya
  • Gawat! VVS Stealer Cuma Seharga Makan Siang Tapi Bisa Habisi Akun Discord Kalian
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

©2026 emka.web.id | Design: Newspaperly WordPress Theme