memory managment rules for assign property
I have been using libmysqlclient. Here are my declarations:
MYSQL *sql;
MYSQL *sqlPut;
MYSQL_RES *qResult;
@property(assign) MYSQL *sql;
@property(assign) MYSQL *sqlPut;
@property(assign) MYSQL_RES *qResult;
This is the code where I assign properties:
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 *)&connecti开发者_开发问答onTimeout);
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];
What is the correct way to cleanup this property? I have a leaks which I can't find in my code so I'm thinking that it might be leaked in this part of code. Currently I do:
self.sql = nil;
self.sqlPut = nil;
self.qResult = nil;
mysql_init allocates new object which is freed when you close the connection. You will have to clean up the memory in case your code flow doesn't go the place where you close the connection.
So i would say call mysql_close once you are done using the connection handle.
精彩评论