public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* Memory leak using detaching over Thread-Attributes with example
@ 2004-03-18 15:29 Rainer.Schiele
  0 siblings, 0 replies; only message in thread
From: Rainer.Schiele @ 2004-03-18 15:29 UTC (permalink / raw)
  To: pthreads-win32

Hello,

I'm encounter an problem that an memory leak occur (and also the Windows Handles
run out), when I detach an Thread with the Thread-Attributes on pthread_create
using pthread_attr_init and pthread_setdetachstate.

Using another approach creating an Thread with pthread_create (attr == NULL) and
calls then pthread_detach on the Thread-ID no leak occur.

I'm using Windows XP, MinGW-3.1.0-1, MSYS-1.0.9.

Looking in the code i found nothing special.

Any Hints?

Thanks Rainer


#include <stdio.h>
#include <pthread.h>
#include <winsock.h>


void *ThreadFunc(void *ptr);

int main(int argc, char *argv[]) {
    pthread_t tid;
    pthread_attr_t attr;
    int rc, i;
    int detachstate;


/*
    rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    if (rc != 0) {
        fprintf(stderr,"pthread_attr_setdetachstate rc=%d\n", rc);
        return 1;
    }
*/
/*
    detachstate = 1;
    rc = pthread_attr_setdetachstate(&attr, detachstate);
    if (rc != 0) {
        fprintf(stderr,"pthread_attr_setdetachstate rc=%d\n", rc);
        return 1;
    }
*/

    fprintf(stderr,"Threadtest started\n");

    for(i=0; i<10 ;i++) {
        /*rc = pthread_create(&tid, &attr, ThreadFunc, (void*) i);*/
        Sleep(5000);
        rc = pthread_create(&tid, NULL, ThreadFunc, (void*) i);
        if (rc != 0) {
            fprintf(stderr,"pthread_create(%d) rc=%d\n", i, rc);
            return 1;
        }

        rc = pthread_detach(tid);
        if (rc != 0) {
            fprintf(stderr,"pthread_detach(%d) rc=%d\n", i, rc);
            return 1;
        }

    }

    fprintf(stderr,"Threadtest ended\n");

    Sleep(100000);

}

void *ThreadFunc(void *ptr) {
    int j = (int) ptr;

    fprintf(stderr,"Thread %d started\n", j);

    Sleep(10000);

    fprintf(stderr,"Thread %d ended\n", j);

    return (NULL);
}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-03-18 15:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-18 15:29 Memory leak using detaching over Thread-Attributes with example Rainer.Schiele

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 read-only IMAP folder(s) and NNTP newsgroup(s).