我一直在使用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;