Christian Smith <[EMAIL PROTECTED]> wrote:
> 
> My own personal opinion on these coding style issues is if the API 
> requires special handling of cleanup, then the API should do the cleanup. 
> Returning an allocated string that requires special cleanup results in a 
> potentially generic operation now being special cased by the API client.
> 

If all the world was Unix, this would work great.  But sadly,
it is not.  We also have to support windows.  See

   http://www.sqlite.org/cvstrac/tktview?tn=444

The sqlite_freemem() API is an old SQLite version 2 API that was
added to work around the fact that memory allocated using malloc()
in a DLL cannot be passed to free() in the main program.

So I do have to provides sqlite3_free() at least, if for no
other purpose than to support windows users.  And notice too
that if you want to your code to be portable to windows, then
you have to use sqlite3_free() to release memory coming back
out of sqlite3_mprintf() or sqlite3_exec().  There appears to
be no way to avoid this due to the limitations of windows DLLs.

The other thing is that since version 3.3.0, SQLite has allowed
an implementation to define its own malloc/free implementation
by overloading the sqlite3OsMalloc() and sqlite3OsFree() interfaces
on the OS-layer.  This is sometimes a useful thing to do.  The
sqlite3_malloc() and sqlite3_free() APIs provide a portable way
to get the application-preferred memory allocator.

The default implementation of sqlite3OsMalloc() is not compatible
with malloc(), however, since it needs to be extended to support
sqlite3AllocationSize() (a capability that is tragically missing
from the standard unix-style malloc()).  

I could continue to provide an sqlite3_free() that is compatible
with free() on unix systems and then provide another set of
routines, sqlite3_osfree(), sqlite3_osmalloc(), sqlite3_osrealloc(),
etc., that provide access to the OS-layer memory allocator.  But
then we would have two separate memory allocation systems which
seems even more confusing than requiring the use of sqlite3_free().
And, notice also that the added complication only allows you to
avoid using sqlite3_free() on Unix platforms - it is of no help
on windows.

--
D. Richard Hipp   <[EMAIL PROTECTED]>

Reply via email to