A dummy example using `strdup` as a C-function that returns an allocated `char *` that needs to be free'd: proc free(p: pointer) {.importc: "free", header: "<stdlib.h>".} proc strdup(c: cstring) : cstring {.importc: "strdup", header: "<stdlib.h>".} proc main() = var foo: cstring = "abcdefg" # strdup in C does a memory allocation # I just use this to emulate a proc that return an allocated pointer var bar = strdup(foo) echo bar free(bar) when isMainModule: main() Run
Let's check with Valgrind that there is no memory leak : * compile with `nim c -d:useMalloc --gc:arc --debugger=native simpletest.nim` * run `valgrind ./simpletest`. ==9640== Memcheck, a memory error detector ==9640== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==9640== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info ==9640== Command: ./simpletest ==9640== abcdefg ==9640== ==9640== HEAP SUMMARY: ==9640== in use at exit: 0 bytes in 0 blocks ==9640== total heap usage: 3 allocs, 3 frees, 1,048 bytes allocated ==9640== ==9640== All heap blocks were freed -- no leaks are possible ==9640== ==9640== For lists of detected and suppressed errors, rerun with: -s ==9640== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)