Changeset 1476:fdb1826f79da


Ignore:
Timestamp:
08/28/10 14:26:05 (18 months ago)
Author:
niam
Branch:
default
Message:

[pc::execution::thread] fix memory leak in case of detached/canceled thread

Location:
sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sources/examples/pc_thread/test.cc

    r1464 r1476  
    3434    } 
    3535 
    36     // throwing *eception 
     36    // throwing exception 
    3737    tools::os::os::setWorkingDir("./dir/"); 
    3838 
     
    4848 
    4949        ::data.set((void *)data); 
     50 
     51        execution::thread(thread, (void *)"detached", execution::ON_DESTRUCTION_KEEP_ALIVE, true).run(); 
    5052 
    5153        execution::manager manager; 
  • sources/src/pcExecutionThread.cc

    r1471 r1476  
    130130    } 
    131131 
     132    if (ti->detached) { 
     133#ifdef DL_EXT 
     134        if (ti->handle != NULL) { 
     135            thread::deinitModule deinit = (thread::deinitModule)dlsym(ti->handle, "deinitPcExecutionThreadModule"); 
     136            if (deinit != NULL) 
     137                deinit(ti->cookie); 
     138 
     139#ifndef DL_FAST 
     140            dlclose(ti->handle); 
     141#endif 
     142        } 
     143#endif 
     144 
     145        delete ti->ex; 
     146        delete ti; 
     147    } 
     148 
    132149    return NULL; 
    133150} 
     
    155172    handle->func = (void *)func; 
    156173    handle->action = action; 
    157 #ifdef DL_EXT 
    158     handle->handle = NULL; 
    159 #endif 
    160174 
    161175#ifdef PTHREAD_EXT 
     
    244258{ 
    245259    if (!cloned) { 
    246 #ifdef DL_EXT 
    247         deinitModule deinit; 
    248 #endif 
    249  
    250260#ifdef PTHREAD_EXT 
    251261        pthread_attr_destroy(&handle->attr); 
    252262#endif 
    253263 
    254         if (!isRunning() || handle->detached) 
    255             return; 
    256  
    257         switch (handle->action) { 
    258             case ON_DESTRUCTION_KEEP_ALIVE: 
    259  
    260 #ifdef PTHREAD_EXT 
    261                 pthread_detach(handle->thread); 
    262 #endif 
    263  
    264                 break; 
    265  
    266             case ON_DESTRUCTION_STOP: 
    267  
    268 #ifdef PTHREAD_EXT 
    269                 pthread_cancel(handle->thread); 
    270 #endif 
    271  
    272                 break; 
    273  
    274             case ON_DESTRUCTION_WAIT: 
    275             default: 
    276  
    277 #ifdef PTHREAD_EXT 
    278                 pthread_join(handle->thread, NULL); 
    279 #endif 
    280  
    281                 break; 
     264        if (isRunning()) { 
     265            if (handle->detached) 
     266                return; 
     267 
     268            switch (handle->action) { 
     269                case ON_DESTRUCTION_KEEP_ALIVE: 
     270                    handle->detached = true; 
     271#ifdef PTHREAD_EXT 
     272                    pthread_detach(handle->thread); 
     273#endif 
     274                    return; 
     275 
     276                case ON_DESTRUCTION_STOP: 
     277#ifdef PTHREAD_EXT 
     278                    pthread_cancel(handle->thread); 
     279#endif 
     280                    break; 
     281            } 
    282282        } 
    283283 
     284#ifdef PTHREAD_EXT 
     285        if (!handle->joined) 
     286            pthread_join(handle->thread, NULL); 
     287#endif 
     288 
    284289#ifdef DL_EXT 
    285290        if (handle->handle != NULL) { 
    286             deinit = (deinitModule)dlsym(handle->handle, "deinitPcExecutionThreadModule"); 
     291            deinitModule deinit = (deinitModule)dlsym(handle->handle, "deinitPcExecutionThreadModule"); 
    287292            if (deinit != NULL) 
    288293                deinit(handle->cookie); 
     
    293298        } 
    294299#endif 
    295     } 
    296  
    297     delete handle->ex; 
    298  
    299     delete handle; 
     300 
     301        delete handle->ex; 
     302        delete handle; 
     303    } 
    300304} 
    301305 
Note: See TracChangeset for help on using the changeset viewer.