go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

calling RegisterAuthorizer multiple times leaks memory

Open anolivetree opened this issue 3 years ago • 3 comments

When RegisterAuthorizer is called multiple times, handleVal keeps callbacks in a map until Close() is called. deleteHandles() should be called every time RegisterAuthorizer is called.

func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, string) int) {
	deleteHandles(c) // delete previous callback from handleVal
	if callback == nil {
		C.sqlite3_set_authorizer(c.db, nil, nil)
	} else {
		C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), newHandle(c, callback))
	}
}

anolivetree avatar Jan 05 '22 00:01 anolivetree

Yes, when you add/remove many times any callback (authorizer, update hook , rollback hook etc.) library will leak memory. But using deleteHandles don't solve problem, because it deletes all handles, not onlyone of them.

graf0 avatar Mar 14 '25 10:03 graf0

I think that you can do bypass using separate connection each time you want to use authorizer. After closing connection all allocations are cleared, so there will be no leaks.

graf0 avatar Mar 14 '25 10:03 graf0

@graf0 Thank you for the comment. My app works with the above patch by chance because my app only uses authorizer callback. I didn't notice it's working by accident.

anolivetree avatar Mar 16 '25 00:03 anolivetree