Archived Forum Post

Index of archived forum posts

Question:

C Library Question: Should I Free memory returned by Chilkat methods?

Aug 08 '16 at 11:05

I have a question about your library. I’m using the http, httpResponse libraries for C (Linux 32 bit)

After a call to the method CkHttpResponse_bodyStr(), should I free the memory allocated for the returned string?


Answer

The following rule applies for all Chilkat methods/properties across all classes (in C and C++).

Anything that returns a "const char *" is a pointer to internal memory that is owned by Chilkat and not guaranteed to remain valid. An application should use the string immediately. If the string needs to be saved, it should be copied to a buffer owned by the application. A "const char *" should not be deleted. (The "const" is an indicator that the caller does not own the memory.)

Any Chilkat method that returns a new Chilkat object (in C++) or a handle to an object (in C) must be deleted by the application. (Note: Chilkat never returns a "const" pointer to a Chilkat object, and Chilkat never returns a non-const pointer to a "char *". Therefore, objects are always deleted by the caller, but strings are not.)

In C++, you use the delete operator. In C, you pass the handle to the "Dispose" function. For example:

CkCert_Dispose(hCert);