0
我一直在使用libmysqlclient。這裏是我的聲明:分配屬性的內存管理規則
MYSQL *sql;
MYSQL *sqlPut;
MYSQL_RES *qResult;
@property(assign) MYSQL *sql;
@property(assign) MYSQL *sqlPut;
@property(assign) MYSQL_RES *qResult;
這是我給你的屬性代碼:
MYSQL *newSql = mysql_init(NULL);
if (newSql == NULL) NSLog(@"MYSQL: Failed to initate connection");
my_bool reconnect = 1;
mysql_options(newSql, MYSQL_OPT_RECONNECT, &reconnect);
NSInteger connectionTimeout = 300;
mysql_options(newSql, MYSQL_OPT_CONNECT_TIMEOUT, (const void *)&connectionTimeout);
mysql_options(newSql, MYSQL_OPT_WRITE_TIMEOUT, (const void *)&connectionTimeout);
mysql_options(newSql, MYSQL_OPT_READ_TIMEOUT, (const void *)&connectionTimeout);
//mysql_options(newSql, CLIENT_INTERACTIVE, &reconnect);
NSNumberFormatter *portTransfer = [[NSNumberFormatter alloc] init];
newSql = mysql_real_connect(newSql, [[connection valueForKey:@"ip"] UTF8String] , [[connection valueForKey:@"login"] UTF8String], [[connection valueForKey:@"password"] UTF8String], [[connection valueForKey:@"database"] UTF8String], [[portTransfer numberFromString:[connection valueForKey:@"port"]] unsignedIntValue], NULL, 0);
if (newSql == NULL) { NSLog(@"MYSQL: Failed to connect database with error:%s\n for connection:%@",mysql_error(newSql),connection); return NO ;}
//else NSLog(@"MYSQL: Carrier:%@ connect database DONE",carrierName);
if ([[connection valueForKey:@"selectionDirections"] intValue] == 0) if (!self.sql) self.sql = newSql; else mysql_ping(self.sql);
else {
//NSInteger connectionTimeout = 10;
//mysql_options(newSql, MYSQL_OPT_CONNECT_TIMEOUT, (const void *)&connectionTimeout);
//self.sqlPut = newSql;
if (!self.sqlPut) self.sqlPut = newSql; else mysql_ping(self.sqlPut);
}
[portTransfer release];
什麼正確的方法來清除這種特性?我有一個在代碼中找不到的泄漏,所以我認爲它可能會泄漏到這部分代碼中。目前我這樣做:
self.sql = nil;
self.sqlPut = nil;
self.qResult = nil;
您不需要通過'assign'指令釋放屬性,只需要釋放具有'copy'和'retain'指令的屬性。 –
你確定泄漏是由這段代碼引起的嗎? – lbrndnr
不,我不確定,但目前我正在檢查一切,但應用程序仍在內存中增長,直到同步進行 – Alex