Always verify pointers are valid before dereferencing to prevent crashes and undefined behavior. This applies to regular pointers, weak pointers, and return values from C functions that may return null.
Always verify pointers are valid before dereferencing to prevent crashes and undefined behavior. This applies to regular pointers, weak pointers, and return values from C functions that may return null.
Use implicit bool conversion instead of explicit null comparisons for cleaner code:
// Preferred
if (ptr) {
ptr->doSomething();
}
// Avoid
if (ptr != nullptr) {
ptr->doSomething();
}
For weak pointers, use the appropriate check method:
operator bool
or .expired()
when you only need to test validity.lock()
only when you need the actual shared pointerApply guard patterns with early returns to reduce nesting:
if (!ptr)
return;
ptr->doSomething();
Pay special attention to C function returns that may be null:
const char* env = getenv("VAR_NAME");
if (!env)
return defaultValue;
Critical areas requiring null checks include: pointer dereferences in event handlers, accessing monitor/window objects that may be destroyed, and any pointer obtained from external APIs or lookups.
Enter the URL of a public GitHub repository