|
From: Jeremy S. <Je...@ch...> - 2014-09-29 11:56:51
|
Hello,
I wonder if anyone else is having this problem? No doubt it's down to an error on my part, but I've tried everything I can think of and still it leaks memory.
I have a piece of code (below) that uses the iODBC dylib to connect to my SQL Server. It leaks memory according to Instruments when the SQLConnect call is made. The problem code is in a class that connects to my database using the below code, extracts data and provides answers. I'm sure there is some horrible mistake in this code but I can't see it. I've posted it here just in case I've found some problem with the library - it's highly unlikely but you never know...
Thanks in advance.
Jeremy
-(BOOL) establishConnectionWithOdbc:(NSString *)theOdbc login:(NSString *)theLogin pwd:(NSString *)thePwd error:(NSError **)err
{
char errMsg[MAX_BUFFER_LEN];
SQLSMALLINT bufLen = 0;
SQLCHAR sqlState[6];
SQLINTEGER nativeError = 0;
HENV localSqlEnv = SQL_NULL_HANDLE;
HDBC localSqlDbc = SQL_NULL_HANDLE;
SQLCHAR *localOdbc = (unsigned char *)[theOdbc cStringUsingEncoding:NSUTF8StringEncoding];
SQLCHAR *localLogin = (unsigned char *)[theLogin cStringUsingEncoding:NSUTF8StringEncoding];
SQLCHAR *localPwd = (unsigned char *)[thePwd cStringUsingEncoding:NSUTF8StringEncoding];
// Allocate environment
SQLAllocEnv(&localSqlEnv);
SQLSetEnvAttr (localSqlEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER);
// Allocate database connection
if (!SQL_SUCCEEDED(SQLAllocConnect(localSqlEnv, &localSqlDbc))) {
// database not available
SQLGetDiagRec(SQL_HANDLE_DBC, localSqlDbc, 1, sqlState, &nativeError, (SQLCHAR *) errMsg, MAX_BUFFER_LEN, &bufLen);
DLog(@"Could not allocate DB handle:%s",errMsg);
if (err) *err = [NSError errorWithDomain:NSCocoaErrorDomain
code:999
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SQL_NO_HANDLE_ERROR,
NSLocalizedDescriptionKey, nil]];
return FALSE;
}
// connect to the database MEMORY LEAK ON THIS LINE
SQLRETURN rc = SQLConnect(localSqlDbc, localOdbc, SQL_NTS, localLogin, SQL_NTS, localPwd, SQL_NTS);
if (rc == SQL_ERROR) {
SQLGetDiagRec(SQL_HANDLE_DBC, localSqlDbc, 1, sqlState, &nativeError, (SQLCHAR *) errMsg, MAX_BUFFER_LEN, &bufLen);
// database not available
DLog(@"Database connect operation failed: %s",errMsg);
if (err) *err = [NSError errorWithDomain:NSCocoaErrorDomain
code:999
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SQL_NO_CONNECTION_ERROR,
NSLocalizedDescriptionKey, nil]];
return FALSE;
}
[self setSqlEnv:localSqlEnv];
[self setSqlDbc:localSqlDbc];
return TRUE;
}
Jeremy Smith | Production Director
CSC Media Group
This message has been scanned for malware by Websense. www.websense.com
|