public inbox for [email protected]  
help / color / mirror / Atom feed
From: Henrik TJ <[email protected]>
To: PostgreSQL Hackers <[email protected]>
Subject: Re: Fix memory leak in postmasterMain
Date: Tue, 21 Apr 2026 16:56:32 +0200 (CEST)
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

Hi

On Sat, 21 Feb 2026, Henrik TJ wrote:

> This is fairly inconsequential as memory leaks goes, but if -D is used when 
> starting postgres, the memory allocated by stdrup() will never be freed. 
> Found with valgrind.

Rebased version of this patch attached.

To see valgrind catch the leak:

1. Compile with valgrind.
2. Run postgres with valgrind:
    valgrind --leak-check=full ./pgrun/bin/postgres -D pgdata/

The -D argument is required, as it is the argument from there that does 
not get freed. This should yield:

==444240== 8 bytes in 1 blocks are definitely lost in loss record 29 of 849
==444240==    at 0x4840B26: malloc (vg_replace_malloc.c:447)
==444240==    by 0x5114A2E: strdup (strdup.c:42)
==444240==    by 0x6B7CE0: PostmasterMain (../src/backend/postmaster/postmaster.c:656)
==444240==    by 0x602555: main (../src/backend/main/main.c:231)


   best regards, Henrik
From 493a0756082acac48cb02ea4fbbe4c8f4c34b0b7 Mon Sep 17 00:00:00 2001
From: Henrik TJ <[email protected]>
Date: Sat, 18 Apr 2026 16:20:31 +0200
Subject: [PATCH] Fix userDoption not getting freed in postmaster

---
 src/backend/postmaster/postmaster.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 90c7c4528e8..476e87001f1 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -789,6 +789,8 @@ PostmasterMain(int argc, char *argv[])
 	 */
 	if (!SelectConfigFiles(userDoption, progname))
 		ExitPostmaster(2);
+	if (userDoption != NULL)
+		free(userDoption);
 
 	if (output_config_variable != NULL)
 	{
-- 
2.53.0



Attachments:

  [text/plain] v2-0001-Fix-userDoption-not-getting-freed-in-postmaster.patch (765B, 2-v2-0001-Fix-userDoption-not-getting-freed-in-postmaster.patch)
  download | inline diff:
From 493a0756082acac48cb02ea4fbbe4c8f4c34b0b7 Mon Sep 17 00:00:00 2001
From: Henrik TJ <[email protected]>
Date: Sat, 18 Apr 2026 16:20:31 +0200
Subject: [PATCH] Fix userDoption not getting freed in postmaster

---
 src/backend/postmaster/postmaster.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 90c7c4528e8..476e87001f1 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -789,6 +789,8 @@ PostmasterMain(int argc, char *argv[])
 	 */
 	if (!SelectConfigFiles(userDoption, progname))
 		ExitPostmaster(2);
+	if (userDoption != NULL)
+		free(userDoption);
 
 	if (output_config_variable != NULL)
 	{
-- 
2.53.0



view thread (7+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: Fix memory leak in postmasterMain
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox