Message-ID: From: "ColinKYuen (@ColinKYuen)" To: "postgresql-interfaces/psqlodbc" Date: Thu, 24 Apr 2025 22:09:27 +0000 Subject: [postgresql-interfaces/psqlodbc] PR #114: fix: use pgfree for libpq mallocs List-Id: X-GitHub-Author-Id: 70862826 X-GitHub-Author-Login: ColinKYuen X-GitHub-Issue: 114 X-GitHub-Repo: postgresql-interfaces/psqlodbc X-GitHub-State: merged X-GitHub-Type: pull_request X-GitHub-Url: https://github.com/postgresql-interfaces/psqlodbc/pull/114 Content-Type: text/plain; charset=utf-8 ## Summary Uses libqpq's Free for libqpq malloc'd String ## Description Changes the generic `free()` function for the error message string allocated by `PQconninfoParse(..)` to use libpq's `PQfreemem(..)` As per [PostgreSQL's libqpq documents](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNINFOPARSE), when using `PQconninfoParse(..)`: > ... After processing the options array, free it by passing it to [PQconninfoFree](https://www.postgresql.org/docs/current/libpq-misc.html#LIBPQ-PQCONNINFOFREE). If this is not done, some memory is leaked for each call to [PQconninfoParse](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNINFOPARSE). **Conversely, if an error occurs and errmsg is not NULL, be sure to free the error string using [PQfreemem](https://www.postgresql.org/docs/current/libpq-misc.html#LIBPQ-PQFREEMEM).** Additional information on [PQfreemem](https://www.postgresql.org/docs/current/libpq-misc.html#LIBPQ-PQFREEMEM), this change is only needed for Windows > Frees memory allocated by libpq, particularly [PQescapeByteaConn](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQESCAPEBYTEACONN), [PQescapeBytea](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQESCAPEBYTEA), [PQunescapeBytea](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQUNESCAPEBYTEA), and PQnotifies. **It is particularly important that this function, rather than free(), be used on Microsoft Windows**. This is because allocating memory in a DLL and releasing it in the application works only if multithreaded/single-threaded, release/debug, and static/dynamic flags are the same for the DLL and the application. **On non-Microsoft Windows platforms, this function is the same as the standard library function free().**