# Maapi\_query\_free\_result mem leak

**URL:** <https://dmap-community.ductus.global/t/maapi-query-free-result-mem-leak/4978>\
**Category:** General\
**Created:** [September 2, 2025, 12:17pm UTC](https://dmap-community.ductus.global/t/maapi-query-free-result-mem-leak/4978 "2025-09-02T12:17:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yogevc](https://avatars.discourse-cdn.com/v4/letter/y/8491ac/32.png) [@yogevc](https://dmap-community.ductus.global/u/yogevc)\
**Post date:** [September 2, 2025, 12:17pm UTC](https://dmap-community.ductus.global/t/maapi-query-free-result-mem-leak/4978/1 "2025-09-02T12:17:14Z")

</div>

Hi,  
There’s a memory leak at `maapi_query_free_result():`

The below block should happen regardless if there are results or not, at `if (qr->nresults > 0) {`

```auto
if (qr->__internal) {
    ETERM *e = qr->__internal;
    erl_free_compound(e);
}

```

Found using `valgrind`.

---

<div class="post-metadata">

**Author:** ![ittaloss](https://avatars.discourse-cdn.com/v4/letter/i/8797f3/32.png) [@ittaloss](https://dmap-community.ductus.global/u/ittaloss)\
**Post date:** [July 9, 2026, 1:42pm UTC](https://dmap-community.ductus.global/t/maapi-query-free-result-mem-leak/4978/2 "2026-07-09T13:42:46Z")

</div>

Yeah, I’ve got this issue on ConfD 8.0.6 as well.

`maapi_query_free_result()` only frees the reply `ETERM` stored in  
`qr->__internal` when the result chunk is non-empty. When a query chunk  
comes back with zero results, the reply term is never freed, leaking the  
whole 5-element tuple plus its children and the `erl_malloc`’ed element  
block (about 500 bytes per call).

In my point of view, this should be:

```c
        if (qr->nresults > 0) {
            ...
            free(qr->results[0].str); /* the block */
            free(qr->results); /* the result array */
        }
        /* Free the reply term even when the query returned no results,
           otherwise every empty maapi_query_result() chunk leaks it. */
        if (qr->__internal) {
            ETERM *e = qr->__internal;
            erl_free_compound(e);
        }
        free(qr);

```

Any thoughts on this? Is there a ConfD version where this issue is already fixed?

Best regards,  
Ittalo

---

<div class="post-metadata">

**Author:** ![sbarvick](https://yyz2.discourse-cdn.com/flex010/user_avatar/dmap-community.ductus.global/sbarvick/32/440_2.png) [@sbarvick](https://dmap-community.ductus.global/u/sbarvick)\
**Post date:** [July 14, 2026, 6:37pm UTC](https://dmap-community.ductus.global/t/maapi-query-free-result-mem-leak/4978/3 "2026-07-14T18:37:51Z")

</div>

Hi,

This code does not appear to have been changed at all from 8.0.6 so it seems safe to say it is behaving the same way. We will look into it.

Regards,

Scott
