> Is it possible to make it without additional LdapInit structure but without > ptr ptr also?
Yes, you can use `addr`. Looks like this is what they do in C according to [stackoverflow](https://stackoverflow.com/questions/16396926/binding-and-authenticating-to-an-ldap-server-over-ssl-and-tls). Something like this: ldap_initialize(addr(ld), "url") Run > Is it possible to own the ptr from Nim to make the destructor unnecessary and > Nim will free it automatically? No, Nim won't know how to free the memory. The `ldap_memfree` may end up freeing sub-elements first. Not calling it likely would result in a memory leaks or worse. > What is the best practices for it? Because the problem looks pretty trivial It's fairly straightforward, but it's best practice to add an idiomatic Nim layer on top to automate the low level C parts. I'd recommend returning a `ref object` so you only deallocate once. Otherwise you can end up copying Ldap and destroying them, which can result in calling `ldap_memfree` multiple times. Here's an example: type LdapRaw {.importc: "LDAP", header: "ldap.h", bycopy.} = object Ldap* = object raw: ptr LdapRaw LdapRef* = ref Ldap proc ldap_initialize(ldp: ptr LdapRaw, url: cstring) {.importc.} proc ldap_memfree(ldp: LdapRaw) {.importc.} proc `=destroy`*(x: var Ldap) = echo "freeing ldap object: ", repr x ldap_memfree(x.raw) proc newLdap*(url: string): LdapRef = new(result) # allocate the container for LdapRaw ldap_initialize(addr result.raw, url.cstring) block: let ldap = newLdap("some string") echo "ldap: ", repr ldap # should print "freeing ..." here echo "done" Run
