mailing list of musl libc
 help / color / mirror / code / Atom feed
* [RFC] malloc + threads = memleak?
@ 2018-02-09 14:33 Roman Yeryomin
  2018-02-09 15:00 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Roman Yeryomin @ 2018-02-09 14:33 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 377 bytes --]

Hello!

I have a very easy reproducible memory leak here when using
malloc/free in two threads.
Attaching a simple test.
thread() here is calling pthread_create():
https://github.com/yeryomin/liba/blob/master/liba.c#L181

Do I misunderstand something or is it a bug?
This can be avoided by wrapping malloc into locks but not sure if
that's the fix you want...


Regards,
Roman

[-- Attachment #2: musl-test.c --]
[-- Type: text/x-csrc, Size: 1426 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

#include <liba.h>

#define PIDPATH		"/tmp"
#define NAME		argv[0]
#define TIMER		30

#define SIZE		10000

int timer = TIMER;
int counter = 0;

void thread42( int *thr )
{
	int i;
	void* allocs[SIZE];

	while (1) {
		for ( i = 0; i < SIZE; i++ )
			allocs[i] = malloc(1500);
		
		syslog( LOG_INFO, "%i: %ld\n", *thr, time(NULL) );

		for ( i = 0; i < SIZE; i++ )
			free(allocs[i]);
	}
}

void reset_timer( int sig )
{
	/* temporarily ingnore interrupts */
	signal( sig, SIG_IGN );
	counter++;
	syslog( LOG_INFO, "%i HUP received, resetting timer (was %i)...",
				counter, timer );
	timer = TIMER;
	/* reenable interrupt */
	a_signal( sig, reset_timer );
}

int main( int argc, char **argv )
{
	int pid = daemonize( PIDPATH, NAME );
	int thr1 = 1;
	int thr2 = 2;

	/* register sighup handler for daemon */
	a_signal( SIGHUP, reset_timer );

	/* start two malloc threads */
	thread( "thread42-1", &thread42, &thr1 );
	thread( "thread42-2", &thread42, &thr2 );

	/* daemon stuff */
	syslog( LOG_INFO, "Working hard..." );
	while ( timer > 0 ) {
		sleep(1);
		timer--;
		if ( timer < 5 )
			syslog( LOG_INFO, "Dying in %i...", timer + 1 );
	}

	/* cleanup, if ever get here */
	syslog( LOG_INFO, "Exiting %s (%i)...", a_daemon_name, pid );
	unlink( a_daemon_pidfile );
	exit(EXIT_SUCCESS);
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-02-09 15:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-09 14:33 [RFC] malloc + threads = memleak? Roman Yeryomin
2018-02-09 15:00 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).