Der Testing Allocator erkennt Leaks
Erkennen Sie Speicherfehler automatisch.
Der Testing Allocator erkennt Leaks ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Memory Bugs Are Sneaky
A program can pass every value check yet still leak memory by forgetting to free it. Zig gives tests a tool to catch exactly that.
Meet testing.allocator
Inside a test you get a special allocator at std.testing.allocator. Use it anywhere your code needs to allocate heap memory.
const a = std.testing.allocator;It Tracks Every Allocation
The testing.allocator remembers every block it hands out. When the test ends it checks whether each one was given back.
A Leak Fails the Test
If even one allocation is never freed, the testing.allocator reports a leak and the test fails, pointing you at the missing free.
Allocate in a Test
Ask the allocator for memory just like in real code. Here alloc reserves a buffer of ten bytes that you must release later.
const buf = try a.alloc(u8, 10);Free with defer
Pair each allocation with a defer that frees it. The cleanup is scheduled immediately, so it runs even if the test fails partway.
const buf = try a.alloc(u8, 10);
defer a.free(buf);Forget free, Catch the Bug
Drop that defer and the test fails with a leak report. This is how the allocator turns a silent bug into a loud, fixable failure.
It Also Spots Double-Frees
The same checked allocator catches freeing the same block twice or freeing memory it never gave you, which are classic crashes.
Pass It Into Your Code
Functions that allocate take an allocator parameter. In a test, hand them testing.allocator so their memory is tracked too.
const list = try buildList(std.testing.allocator);Clean Up What You Build
Whatever your function allocated, the test must release. Add a defer right after the call to free or deinit the returned data.
defer list.deinit();Free Memory Testing
Because leaks fail the build, every passing test proves its memory was balanced. That confidence is unique to testing.allocator.
Quick Check
You run a test that allocates with std.testing.allocator but never frees the buffer. What happens?
Recap
You used std.testing.allocator to catch leaks and double-frees for free. Pair each alloc with a defer free and your tests stay clean. ✅
Häufig gestellte Fragen
Ist die Lektion „Der Testing Allocator erkennt Leaks“ kostenlos?
Ja — der vollständige Text von „Der Testing Allocator erkennt Leaks“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Der Testing Allocator erkennt Leaks“?
Erkennen Sie Speicherfehler automatisch. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Zig Academy zu starten?
Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Der Testing Allocator erkennt Leaks“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?
Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- test-Blöcke schreiben
- Assertions mit std.testing
- Der Testing Allocator erkennt Leaks
- Tests ausführen und filtern