[Openmcl-devel] Allocating temporary nsstrings

Ron Garret ron at flownet.com
Sat Mar 12 18:52:41 PST 2011


This is a very common idiom:

(#/initWithString: (#/alloc ns:some-class) (objc:make-nsstring some-lisp-string))

but it leaks memory.  One way to fix this is:

(let ((nssstr (objc:make-nsstring some-lisp-string)))
  (prog1 (#/initWithString: (#/alloc ns:some-class) nsstr) (#/release nsstr)))

but that seems wasteful.  It seems like the Right Thing would be:

(with-stack-allocated-ns-string (s some-lisp-string)
  (#/initWithString: (#/alloc ns:some-class) s))

but I don't know how to stack allocate an instance of an ObjC class, or if that's even possible (or wise -- the function I'm passing the nsstring to might keep a pointer to it).

What is the received wisdom on the best way to allocate temporary nsstrings without leaking?

Thanks,
rg




More information about the Openmcl-devel mailing list