Here is an example how to sqlite_mallo: /** \brief Initialize the flag function extension * * \param db a pointer to the database * \param a point to a point for the error messages * \param a pointer to the SQLITE_API * \return SQLITE_OK * */ int sqlite3_extension_init(sqlite3* db, char **err, const sqlite3_api_routines *api) { int rc = SQLITE_OK; int RegisterFlagFunctions(sqlite3 *); SQLITE_EXTENSION_INIT2(api) /**< Registers all flag functions */ rc = RegisterFlagFunctions(db); if (rc != SQLITE_OK) { /* Error! */ return rc; } /**< Need so memory from SQLite to store the flags */ sptr = sqlite3_malloc(sizeof(Semaphore)); ClearFlags(); return rc; } By the way the extension is not in control of the memory block. SQLite is in control and therefor I am not deleting anything. You should check sptr not being NULL.