avoid thread memory leak.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25098 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2007-04-30 05:08:17 +00:00
parent 4c875823d5
commit 1621b5cf84
2 changed files with 30 additions and 5 deletions

View file

@ -1,3 +1,8 @@
2007-04-28 Richard Frith-Macdonald <rfm@gnu.org>
* Source/thr-pthread.m: Set thread attributes detached to avoid memory
leak as suggested by Chris Ball <cball@borderlinetech.ca>
2007-04-27 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSBundle.m: Reformat for conding standards. Alter library

View file

@ -32,6 +32,7 @@ Boston, MA 02110-1301, USA. */
/* Key structure for maintaining thread specific storage */
static pthread_key_t _objc_thread_storage;
static pthread_attr_t _objc_thread_attribs;
/* Global exit status. */
int __objc_thread_exit_status = 0;
@ -188,7 +189,21 @@ int
__objc_init_thread_system(void)
{
/* Initialize the thread storage key */
return pthread_key_create(&_objc_thread_storage, NULL);
if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
{
/*
* The normal default detach state for threads is PTHREAD_CREATE_JOINABLE
* which causes threads to not die when you think they should.
*/
if (pthread_attr_init(&_objc_thread_attribs) == 0)
{
if (pthread_attr_setdetachstate(&_objc_thread_attribs,
PTHREAD_CREATE_DETACHED) == 0)
return 0;
}
}
return -1;
}
/* Close the threads subsystem. */
@ -196,9 +211,13 @@ int
__objc_close_thread_system(void)
{
/* Destroy the thread storage key */
/* Not implemented yet */
/* return pthread_key_delete(&_objc_thread_storage); */
return 0;
if (pthread_key_delete(_objc_thread_storage) == 0)
{
if (pthread_attr_destroy(&_objc_thread_attribs) == 0)
return 0;
}
return -1;
}
/* Backend thread functions */
@ -210,7 +229,8 @@ __objc_thread_detach(void (*func)(void *arg), void *arg)
objc_thread_t thread_id;
pthread_t new_thread_handle;
if (!(pthread_create(&new_thread_handle, NULL, (void *)func, arg)))
if (!(pthread_create(&new_thread_handle, &_objc_thread_attribs,
(void *)func, arg)))
thread_id = *(objc_thread_t *)&new_thread_handle;
else
thread_id = NULL;