[freetds] Please ensure if it is a memory leak bug.

ldai ldai at accunettech.com
Mon Aug 30 04:59:13 EDT 2004


Hi,everybody:

   I found a memory leak. Please ensure if it is really a bug.

   I test my code when it can't connect database in loop. I find a memory 
leak and it leaks very fast when can't connect database.
   After I trace this memory leak , I think that dbproc wasn't be freed 
memory in tdsdbopen().
   I try to fix this bug. When I test again, I found the memory leak 
disappared.
   Could you ensure this bug(I think so)?

   Thank Michael Peppler. Thank you give me good advice.

						ldai
					    30,Aug,2004

DBPROCESS *
tdsdbopen(LOGINREC * login, char *server)
{
	DBPROCESS *dbproc;
	TDSCONNECTINFO *connect_info;
	char temp_filename[256];

	dbproc = (DBPROCESS *) malloc(sizeof(DBPROCESS));
	if (dbproc == NULL) {
		_dblib_client_msg(NULL, SYBEMEM, EXRESOURCE, "Unable to 
allocate sufficient memory.");
		return NULL;
	}
	memset(dbproc, '\0', sizeof(DBPROCESS));

	dbproc->dbopts = init_dboptions();
	if (dbproc->dbopts == NULL) {
+		dbclose(dbproc); //memory leak fix by leon, 30/08/04
		return NULL;
	}
	dbproc->dboptcmd = NULL;

	dbproc->avail_flag = TRUE;

	dbproc->command_state = DBCMDNONE;

	tds_set_server(login->tds_login, server);

	dbproc->tds_socket = tds_alloc_socket(g_dblib_ctx.tds_ctx, 512);
	tds_set_parent(dbproc->tds_socket, (void *) dbproc);
	dbproc->tds_socket->option_flag2 &= ~0x02;	/* we're not an ODBC 
driver */
	dbproc->tds_socket->env_chg_func = db_env_chg;
	dbproc->envchange_rcv = 0;
	dbproc->dbcurdb[0] = '\0';
	dbproc->servcharset[0] = '\0';

	connect_info = tds_read_config_info(NULL, login->tds_login, 
g_dblib_ctx.tds_ctx->locale);
	if (!connect_info) {
+		dbclose(dbproc); //memory leak fix by leon, 30/08/04
		return NULL;
	}

	if (g_dblib_login_timeout >= 0) {
		connect_info->connect_timeout = g_dblib_login_timeout;
	}

	dbproc->dbchkintr = NULL;
	dbproc->dbhndlintr = NULL;
	dbproc->tds_socket->chkintr = dblib_chkintr;
	dbproc->tds_socket->hndlintr = dblib_hndlintr;

	if (tds_connect(dbproc->tds_socket, connect_info) == TDS_FAIL) {
		dbproc->tds_socket = NULL;
		tds_free_connect(connect_info);
		dbclose(dbproc); //memory leak fix by leon, 30/08/04
		return NULL;
	}
	tds_free_connect(connect_info);
	dbproc->dbbuf = NULL;
	dbproc->dbbufsz = 0;

	if (dbproc->tds_socket) {
		/* tds_set_parent( dbproc->tds_socket, dbproc); */
		dblib_add_connection(&g_dblib_ctx, dbproc->tds_socket);
	} else {
		fprintf(stderr, "DB-Library: Login incorrect.\n");
+		dbclose(dbproc); //memory leak fix by leon, 30/08/04
+//		free(dbproc);	/* memory leak fix (mlilback, 11/17/01) 
*/
		return NULL;
	}

	buffer_init(&(dbproc->row_buf));

	if (g_dblib_ctx.recftos_filename != (char *) NULL) {
		/* FIXME buffer overflow wait us... */
		sprintf(temp_filename, "%s.%d", 
g_dblib_ctx.recftos_filename, g_dblib_ctx.recftos_filenum);
		dbproc->ftos = fopen(temp_filename, "w");
		if (dbproc->ftos != (FILE *) NULL) {
			fprintf(dbproc->ftos, "/* dbopen() at %s */\n", 
_dbprdate(temp_filename));
			fflush(dbproc->ftos);
			g_dblib_ctx.recftos_filenum++;
		}
	}
	return dbproc;
}


 



More information about the FreeTDS mailing list