Returning a malloc pointer-Collection of common programming errors
As a rule, everything you malloc you have to also free. As another rule, if your api does the allocation, your api should ALSO do the deallocation, since at some future point down the road you may change the underlying mechanism. Also, in Windows, if memory is allocated by a DLL, it has to be freed by the same DLL or crashy things happen.
So, in general, the pattern looks like this:
MyType* foo = myapi_dosomething(x,y,z);
if (!foo) die("No Foo!");
use_object(foo);
myapi_free(foo);
foo=NULL; // just in case