Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitafd818a

Browse files
committed
Fix memory leaks
1 parent5b97415 commitafd818a

File tree

7 files changed

+48
-21
lines changed

7 files changed

+48
-21
lines changed

‎src/backend/postmaster/postmaster.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,11 @@ PostmasterMain(int argc, char *argv[])
625625
break;
626626

627627
case'C':
628-
output_config_variable=strdup(optarg);
628+
output_config_variable=top_strdup(optarg);
629629
break;
630630

631631
case'D':
632-
userDoption=strdup(optarg);
632+
userDoption=top_strdup(optarg);
633633
break;
634634

635635
case'd':
@@ -1622,7 +1622,6 @@ ServerLoop(void)
16221622
last_lockfile_recheck_time=last_touch_time=time(NULL);
16231623

16241624
nSockets=initMasks(&readmask);
1625-
16261625
for (;;)
16271626
{
16281627
fd_setrmask;
@@ -3568,8 +3567,8 @@ LogChildExit(int lev, const char *procname, pthread_t pid, thread_status_t exits
35683567
/*------
35693568
translator: %s is a noun phrase describing a child process, such as
35703569
"server process" */
3571-
(errmsg("%s (PID %ld) was terminated by signal %d: %s",
3572-
procname,pid,WTERMSIG(exitstatus),
3570+
(errmsg("%s (PID %ld) was terminated by signal %ld: %s",
3571+
procname,pid,exitstatus,
35733572
WTERMSIG(exitstatus)<NSIG ?
35743573
sys_siglist[WTERMSIG(exitstatus)] :"(unknown)"),
35753574
activity ?errdetail("Failed process was running: %s",activity) :0));
@@ -3579,8 +3578,8 @@ LogChildExit(int lev, const char *procname, pthread_t pid, thread_status_t exits
35793578
/*------
35803579
translator: %s is a noun phrase describing a child process, such as
35813580
"server process" */
3582-
(errmsg("%s (PID %d) was terminated by signal %ld",
3583-
procname,pid,WTERMSIG(exitstatus)),
3581+
(errmsg("%s (PID %ld) was terminated by signal %ld",
3582+
procname,pid,exitstatus),
35843583
activity ?errdetail("Failed process was running: %s",activity) :0));
35853584
#endif
35863585
else
@@ -3972,10 +3971,11 @@ static void thread_cleanup(void* arg)
39723971
CleanupLatchSupport();
39733972
pq_finalize();
39743973

3974+
ReleaseTimeouts();
3975+
39753976
/* Release memory context for this thread */
39763977
CurrentMemoryContext=NULL;
3977-
MemoryContextReset(TopMemoryContext);
3978-
free(TopMemoryContext);
3978+
MemoryContextDelete(TopMemoryContext);
39793979

39803980
pthread_mutex_lock(&terminated_queue_mutex);
39813981
ctx->next=terminated_queue;
@@ -3996,8 +3996,8 @@ static void* thread_trampoline(void* arg)
39963996

39973997
pthread_cleanup_push(thread_cleanup,ctx);
39983998

3999-
restore_backend_variables(ctx);
40003999
IsPostmasterEnvironment= true;
4000+
PostmasterContext=NULL;
40014001

40024002
/*
40034003
* Fire up essential subsystems: error and memory management
@@ -4008,7 +4008,8 @@ static void* thread_trampoline(void* arg)
40084008
*/
40094009
MemoryContextInit();
40104010
MemoryContextSwitchTo(TopMemoryContext);
4011-
PostmasterContext=NULL;
4011+
4012+
restore_backend_variables(ctx);
40124013

40134014
InitializeGUCOptions();
40144015

@@ -4275,8 +4276,8 @@ BackendInitialize(Port *port)
42754276
* Save remote_host and remote_port in port structure (after this, they
42764277
* will appear in log_line_prefix data for log messages).
42774278
*/
4278-
port->remote_host=strdup(remote_host);
4279-
port->remote_port=strdup(remote_port);
4279+
port->remote_host=top_strdup(remote_host);
4280+
port->remote_port=top_strdup(remote_port);
42804281

42814282
/* And now we can issue the Log_connections message, if wanted */
42824283
if (Log_connections)
@@ -4307,7 +4308,7 @@ BackendInitialize(Port *port)
43074308
ret==0&&
43084309
strspn(remote_host,"0123456789.")<strlen(remote_host)&&
43094310
strspn(remote_host,"0123456789ABCDEFabcdef:")<strlen(remote_host))
4310-
port->remote_hostname=strdup(remote_host);
4311+
port->remote_hostname=top_strdup(remote_host);
43114312

43124313
/*
43134314
* Ready to begin client interaction. We will give up and exit(1) after a

‎src/backend/utils/error/elog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ set_syslog_parameters(const char *ident, int facility)
19331933
}
19341934
if (syslog_ident)
19351935
free(syslog_ident);
1936-
syslog_ident=strdup(ident);
1936+
syslog_ident=top_strdup(ident);
19371937
/* if the strdup fails, we will cope in write_syslog() */
19381938
syslog_facility=facility;
19391939
}

‎src/backend/utils/init/miscinit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ SetDataDir(const char *dir)
100100
AssertArg(dir);
101101

102102
/* If presented path is relative, convert to absolute */
103-
new=make_absolute_path(dir);
103+
new=top_strgrab(make_absolute_path(dir));
104104

105105
if (DataDir)
106106
free(DataDir);

‎src/backend/utils/misc/timeout.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct timeout_params
4747
*/
4848
staticsession_localtimeout_paramsall_timeouts[MAX_TIMEOUTS];
4949
staticsession_localboolall_timeouts_initialized= false;
50+
staticsession_localtimer_ttimer_id;
5051

5152
/*
5253
* List of active timeouts ordered by their fin_time and priority.
@@ -195,7 +196,6 @@ schedule_alarm(TimestampTz now)
195196
structitimervaltimeval;
196197
longsecs;
197198
intusecs;
198-
staticsession_localtimer_ttimer_id;
199199
MemSet(&timeval,0,sizeof(structitimerval));
200200

201201
/* Get the time remaining till the nearest pending timeout */
@@ -245,7 +245,7 @@ schedule_alarm(TimestampTz now)
245245
enable_alarm();
246246

247247
#if1
248-
if (timer_id==0)
248+
if (!timer_id)
249249
{
250250
structsigeventse;
251251
se.sigev_notify=SIGEV_THREAD_ID;
@@ -388,6 +388,15 @@ InitializeTimeouts(void)
388388
pqsignal(SIGALRM,handle_sig_alarm);
389389
}
390390

391+
void
392+
ReleaseTimeouts(void)
393+
{
394+
if (timer_id)
395+
{
396+
timer_delete(timer_id);
397+
}
398+
}
399+
391400
/*
392401
* Register a timeout reason
393402
*

‎src/backend/utils/mmgr/mcxt.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ void
200200
MemoryContextDelete(MemoryContextcontext)
201201
{
202202
AssertArg(MemoryContextIsValid(context));
203-
/*We had better not be deleting TopMemoryContext ... */
204-
Assert(context!=TopMemoryContext);
203+
/*For pthreads we should allow deletion of top memory context */
204+
/*Assert(context != TopMemoryContext); */
205205
/* And not CurrentMemoryContext, either */
206206
Assert(context!=CurrentMemoryContext);
207207

@@ -224,7 +224,11 @@ MemoryContextDelete(MemoryContext context)
224224

225225
context->methods->delete_context(context);
226226
VALGRIND_DESTROY_MEMPOOL(context);
227-
pfree(context);
227+
if (context==TopMemoryContext) {
228+
free(context);
229+
}else {
230+
pfree(context);
231+
}
228232
}
229233

230234
/*
@@ -844,6 +848,15 @@ MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
844848
returnret;
845849
}
846850

851+
char*top_strgrab(charconst*str)
852+
{
853+
size_tlen=strlen(str)+1;
854+
char*copy=top_malloc(len);
855+
memcpy(copy,str,len);
856+
free(str);
857+
returncopy;
858+
}
859+
847860
void*
848861
palloc(Sizesize)
849862
{

‎src/include/utils/palloc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ extern void pfree(void *pointer);
9999
#definetop_free(ptr) do if (ptr) pfree(ptr); while(0)
100100
#definetop_strdup(str) MemoryContextStrdup(TopMemoryContext, str)
101101

102+
externchar*top_strgrab(charconst*str);
103+
104+
102105
/* Higher-limit allocators. */
103106
externvoid*MemoryContextAllocHuge(MemoryContextcontext,Sizesize);
104107
externvoid*repalloc_huge(void*pointer,Sizesize);

‎src/include/utils/timeout.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ typedef struct
7070
externvoidInitializeTimeouts(void);
7171
externTimeoutIdRegisterTimeout(TimeoutIdid,timeout_handler_prochandler);
7272
externvoidreschedule_timeouts(void);
73+
externvoidReleaseTimeouts(void);
7374

7475
/* timeout operation */
7576
externvoidenable_timeout_after(TimeoutIdid,intdelay_ms);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp