Skip to content

Conversation

stavikpetr
Copy link

@stavikpetr stavikpetr commented Sep 13, 2024

When a query is prepared with ibase_prepare() and then it is freed with ibase_free_query(), the associated zend_resource object is never freed, because the function ibase_free_query() does not decrease its ref count and it stays forever at 1 (after all the variables holding reference in php are destroyed).

I was also considering to use a following fix:

uint32_t refcount = GC_REFCOUNT(Z_RES_P(query_arg));
zend_list_close(Z_RES_P(query_arg));
if (refcount > 0) {
	zend_list_delete(Z_RES_P(query_arg));
}

This solution would avoid memory access violations in cases when the refcount is already 0 (because the zend_resource would get freed in zend_list_close()), but I would argue that this situation should never occurr and it could happen only if the internal reference created during ibase_prepare() was freed by some other parts of the code. But that is not correct and instead, it should be always freed only here.

@stavikpetr stavikpetr closed this Sep 13, 2024
@stavikpetr stavikpetr reopened this Sep 13, 2024
@stavikpetr stavikpetr changed the title ibase_free_query does not decrease ref count Fix memory leak of prepared queries Sep 13, 2024
@stavikpetr stavikpetr force-pushed the zend_resource_memory_leak branch from 11a94db to 4cd4a84 Compare September 13, 2024 12:13
@MartinKoeditz
Copy link
Collaborator

@stavikpetr Thank you. I will check and merge your patch next week.

@MartinKoeditz MartinKoeditz merged commit 7dc34ae into FirebirdSQL:master Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants