re-use of dbprocess structure (was: Re: [freetds] state of dbrpcsend())

liam at inodes.org liam at inodes.org
Wed Jul 7 03:45:45 EDT 2004


On Thu, Jun 10, 2004 at 09:26:14AM +0100, Thompson, Bill D (London) wrote:

> I've tried this out, and it works. 

Thanks for this. I've started coding and so far things are working fine.

However, I'm now trying to re-use an initially allocated DBPROCESS 
structure with multiple calls to dbrpcproc() and associated functions.

I do this for performance reasons as I do not want to reconnect to the
remote SQL server every time I want to call a sp, etc. Unfortunately, 
the memory usage of the process is growing without bound.

I wrote a small test program that does not have malloc or any other
memory allocation functions used in a loop at all so I am positive that 
the memory growth is in the dblib APIs. Either through a memory leak or
my misuse of them.

The code looks something like this:

	tyepdef struct {
		DBPROCESS *dbproc;
		DBLOGIN *login;
	} db_ctx;

	db_ctx *connect(...) {
		db_ctx *ctx;
		dbinit();
		ctx->login = dblogin();
		ctx->dbproc = dbopen();
		... 
		dbuse();
		return ctx;
	}

	void call_sp(db_ctx *ctx, int blah) {

		/* no malloc/strdup/etc in this function */

		dbrpcinit(ctx->dbproc, ..., 0);
		dbrpcparam();
		...
		dbrpcsend();
		dbsqlok();
		while (dbresults() != NO_MORE_RESULTS) {
			/* dbbind points to stack variables only */
			dbbind();
			....
			while (dbnextrow() != NO_MORE_ROWS) {
				/* process bound variables */
			}
		}
		if (dbhasretstat() == TRUE) {
			ret = dbrestatus(ctx->dbproc)	
			/* do stuff here */
		}
		dbfreebuf(ctx->dbproc);
		return;	
	}

	void disconnect(db_ctx **ctx) {
		dbclose((*ctx)->dbproc);	
		dbloginfree((*ctx)->login);	
		dbexit();
		free(*ctx);
	}

	int main(void) {
		db_ctx *ctx;
		ctx = connect();
		for (x = 0; x < 65535; x++) {
			call_sp(ctx, x);
		}
		disconnect(&ctx);
	}	

The process grows without bound at the rate of approximately 1Mbyte/s. Does 
anyone have any suggestions if I'm doing something wrong with my API use?

Note: this is single-threaded. No POSIX threads (yet).

Thanks.



More information about the FreeTDS mailing list