0Pricing
Zig Academy · Lesson

Pairing Open with Close

Acquire and release symmetrically.

Pairing Open with Close is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Every Open Needs a Close

Resources come in pairs: open and close, alloc and free, lock and unlock. The skill is keeping each pair balanced.

Pair Them on the Spot

The Zig habit is to write the close immediately after the open, using defer, so the pair can never drift apart.

const f = try open();
defer f.close();

Allocate, Then Defer Free

The same rhythm applies to memory. Right after you allocate, schedule the free so the buffer is released on exit.

const buf = try a.alloc(u8, 64);
defer a.free(buf);

Locks Get Unlocked

Mutexes follow suit. Acquire the lock, then defer the unlock so the lock is always released even if you return early.

mutex.lock();
defer mutex.unlock();

The Pattern Scales

Open several things and each gets its own defer. The reverse order ensures they close in the right sequence automatically.

Guard the Return Value

When a function builds and returns a resource, use errdefer for the free, so it survives on success but is cleaned up on error.

const node = try a.create(Node);
errdefer a.destroy(node);

No Orphaned Resources

Because the close sits with the open, refactoring or adding branches never leaves a resource orphaned and leaking.

Easy to Audit

A reviewer can scan for each open and immediately spot its matching defer. Missing cleanup becomes obvious at a glance.

Symmetry Is the Goal

Think of it as symmetry: every acquiring line has a releasing line nearby, and the defer keeps them in lockstep.

A Reusable Recipe

Acquire, then defer release, then use. This three-step recipe covers files, memory, locks, and almost any resource you meet.

Confidence Under Pressure

With paired acquire and release, you can add early returns and error handling freely, trusting cleanup to stay correct.

Quick Check

Where should the matching close usually go?

Recap

Pair every open with a defer close on the next line. This symmetry keeps files, memory, and locks balanced on all paths. 🔐

Frequently asked questions

Is the “Pairing Open with Close” lesson free?

Yes — the full text of “Pairing Open with Close” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Pairing Open with Close”?

Acquire and release symmetrically. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Pairing Open with Close” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What defer Guarantees
  2. Execution Order of Multiple defers
  3. defer vs errdefer in Practice
  4. Pairing Open with Close
← Back to Zig Academy