Fundamentals 7 min read

How to Spot and Prevent C Memory Leaks Before They Crash Your System

This article explains why C heap memory leaks cause board resets after months of operation, describes heap allocation methods, the three key leak elements, common release misconceptions, and provides practical code‑review techniques to detect and eliminate leaks.

Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
How to Spot and Prevent C Memory Leaks Before They Crash Your System

Memory Leak Fundamentals

Many developers have encountered board resets after months of online operation caused by memory exhaustion due to leaks. This article introduces the principle of memory leaks and inspection methods, aiming to prevent such issues during the coding review stage.

Heap Memory in C

Memory leaks only occur when using heap memory; stack memory is automatically allocated and freed. In C, heap memory is obtained with malloc. A typical allocation pattern is:

char *info = NULL;    /* converted string */
info = (char*)malloc(NB_MEM_SPD_INFO_MAX_SIZE);
if (NULL == info) {
    (void)tdm_error("malloc error!
");
    return NB_SA_ERR_HPI_OUT_OF_MEMORY;
}

The variable that stores the heap address must be a pointer, which can be a single or multiple‑level pointer.

Methods to Obtain Heap Memory

There are two common ways to get heap memory:

Method 1: Assign the function’s return value directly to a pointer.

char *local_pointer_xx = NULL;
local_pointer_xx = (char*)function_xx(param_xx, ...);

Method 2: Pass a pointer‑to‑pointer as an output parameter.

int ret;
char *local_pointer_xx = NULL;
ret = (char*)function_xx(..., &local_pointer_xx, ...);

Both methods ultimately let the function allocate memory internally; the difference lies in how the pointer is returned.

Three Elements of a Memory Leak

Element 1: A local pointer variable is defined inside a function.

Element 2: The pointer obtains memory via one of the two heap‑allocation methods.

Element 3: Before the function returns (including all normal and error branches), the allocated memory is neither freed nor transferred to a global variable or caller.

Common Misconceptions About Memory Release

Even experienced C programmers may overlook releases because they only consider freeing memory that they allocated directly (e.g., via malloc, g_malloc) or memory returned by familiar APIs. When using unfamiliar interfaces, the need to free the memory is often missed, leading to leaks.

Example of an API that requires the caller to free a list:

dfl_get_object_list(const char *class_name, GSList **list)

Inspection Methods for Memory Leaks

Effective inspection relies on good coding review habits aligned with the three leak elements:

When a local pointer appears, be alert to a possible leak and investigate further.

Check how the pointer is assigned—whether it follows one of the two heap‑allocation methods. Determine if the pointer points to global/static data, heap memory, or something else. Consult documentation or source code for unfamiliar APIs and examine other usages for proper release.

If allocation is confirmed, trace the pointer’s fate: is it stored globally, returned, or left dangling? Ensure every return path either frees the memory or transfers ownership appropriately.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingCode reviewmemory leakC programmingHeap Allocation
Huawei Cloud Developer Alliance
Written by

Huawei Cloud Developer Alliance

The Huawei Cloud Developer Alliance creates a tech sharing platform for developers and partners, gathering Huawei Cloud product knowledge, event updates, expert talks, and more. Together we continuously innovate to build the cloud foundation of an intelligent world.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.