iphone – 使用SQLite3 API时Xcode内存泄漏
发布时间:2020-12-14 17:26:58 所属栏目:百科 来源:网络整理
导读:我正在使用以下代码片段将新的元素/行添加到Sqlite3数据库中,每一件事都运行正常,但每次都会给内存泄漏,当我调用此函数时,任何人都可以帮我解决这个问题吗? { sqlite3 *database;sqlite3_stmt *addStmt; NSString *localdescription=@"Enter your Notes her
我正在使用以下代码片段将新的元素/行添加到Sqlite3数据库中,每一件事都运行正常,但每次都会给内存泄漏,当我调用此函数时,任何人都可以帮我解决这个问题吗?
{ sqlite3 *database; sqlite3_stmt *addStmt; NSString *localdescription=@"Enter your Notes here"; if(sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK) { const char *sql = "insert into database(name) Values(?)"; if(sqlite3_prepare_v2(database,sql,-1,&addStmt,NULL) == SQLITE_OK) { sqlite3_bind_text(addStmt,1,[localName UTF8String],SQLITE_TRANSIENT); if(SQLITE_DONE != sqlite3_step(addStmt)) NSAssert1(0,@"Error while inserting data. '%s'",sqlite3_errmsg(database)); else //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid NSLog(@"id=====%d",sqlite3_last_insert_rowid(database)); //Reset the add statement. sqlite3_reset(addStmt); } } sqlite3_close(database); } 解决方法
您应该最终确定不再使用的任何准备好的声明:
sqlite3_finalize(addStmt),addStmt = nil; 没有必要将指针设置为nil我只是真的喜欢它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |