Changeset 5438:4eb05969f66c in roaraudio


Ignore:
Timestamp:
03/20/12 17:00:08 (12 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

fix code so refcount is correct and avoid double free

Location:
libroar
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libroar/plugincontainer.c

    r5436 r5438  
    5757 
    5858static void * _copy_kv(struct roar_keyval ** copy, struct roar_keyval * src, size_t len) { 
    59  size_t buflen, i; 
     59 size_t buflen = 0; 
     60 size_t i; 
    6061 void * ret; 
    6162 char * p; 
    6263 struct roar_keyval * c; 
    6364 
    64  buflen = len * (sizeof(struct roar_keyval) + 2 /* terminating \0s */); 
    65  
    6665 for (i = 0; i < len; i++) { 
    6766  if ( src[i].key != NULL ) 
    68    buflen += roar_mm_strlen(src[i].key); 
     67   buflen += roar_mm_strlen(src[i].key) + 1; 
    6968  if ( src[i].value != NULL ) 
    70    buflen += roar_mm_strlen(src[i].value); 
    71  } 
     69   buflen += roar_mm_strlen(src[i].value) + 1; 
     70 } 
     71 
     72 c   = roar_mm_malloc(len * sizeof(struct roar_keyval)); 
     73 if ( c == NULL ) 
     74  return NULL; 
     75 
     76 memset(c, 0, len * sizeof(struct roar_keyval)); 
    7277 
    7378 ret = roar_mm_malloc(buflen); 
     
    7782 memset(ret, 0, buflen); 
    7883 
    79  c = ret; 
    80  p = ret + len*sizeof(struct roar_keyval); 
     84 p = ret; 
    8185 
    8286 for (i = 0; i < len; i++) { 
     
    178182int roar_plugincontainer_ref(struct roar_plugincontainer * cont) { 
    179183 if ( cont == NULL ) { 
     184  ROAR_DBG("roar_plugincontainer_ref(cont=%p) = -1 // error=FAULT", cont); 
    180185  roar_err_set(ROAR_ERROR_FAULT); 
    181186  return -1; 
     
    184189 cont->refc++; 
    185190 
     191 ROAR_DBG("roar_plugincontainer_ref(cont=%p) = 0", cont); 
    186192 return 0; 
    187193} 
     
    191197 
    192198 if ( cont == NULL ) { 
     199  ROAR_DBG("roar_plugincontainer_unref(cont=%p) = -1 // error=FAULT", cont); 
    193200  roar_err_set(ROAR_ERROR_FAULT); 
    194201  return -1; 
     
    197204 cont->refc--; 
    198205 
    199  if ( cont->refc ) 
     206 if ( cont->refc ) { 
     207  ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont); 
    200208  return 0; 
     209 } 
    201210 
    202211 while (cont->numhandles) { 
     
    220229 roar_mm_free(cont); 
    221230 
     231 ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont); 
    222232 return 0; 
    223233} 
  • libroar/roardl.c

    r5431 r5438  
    110110int roar_dl_para_ref                    (struct roar_dl_librarypara * para) { 
    111111 if ( para == NULL ) { 
     112  ROAR_DBG("roar_dl_para_ref(para=%p) = -1 // error=FAULT", para); 
    112113  roar_err_set(ROAR_ERROR_FAULT); 
    113114  return -1; 
     
    116117 para->refc++; 
    117118 
     119 ROAR_DBG("roar_dl_para_ref(para=%p) = 0", para); 
    118120 return 0; 
    119121} 
     
    121123int roar_dl_para_unref                  (struct roar_dl_librarypara * para) { 
    122124 if ( para == NULL ) { 
     125  ROAR_DBG("roar_dl_para_unref(para=%p) = -1 // error=FAULT", para); 
    123126  roar_err_set(ROAR_ERROR_FAULT); 
    124127  return -1; 
     
    127130 para->refc--; 
    128131 
    129  if ( para->refc ) 
     132 if ( para->refc ) { 
     133  ROAR_DBG("roar_dl_para_unref(para=%p) = 0", para); 
    130134  return 0; 
     135 } 
    131136 
    132137 if ( para->notifycore != NULL ) 
     
    140145 roar_mm_free(para); 
    141146 
     147 ROAR_DBG("roar_dl_para_unref(para=%p) = 0", para); 
    142148 return 0; 
    143149} 
     
    408414int                      roar_dl_ref    (struct roar_dl_lhandle * lhandle) { 
    409415 if ( (void*)lhandle < (void*)128 ) { 
     416  ROAR_DBG("roar_dl_ref(lhandle=%p) = -1 // error=BADFH", lhandle); 
    410417  roar_err_set(ROAR_ERROR_BADFH); 
    411418  return -1; 
     
    414421 lhandle->refc++; 
    415422 
     423 ROAR_DBG("roar_dl_ref(lhandle=%p) = 0", lhandle); 
    416424 return 0; 
    417425} 
     
    421429 
    422430 if ( (void*)lhandle < (void*)128 ) { 
     431  ROAR_DBG("roar_dl_unref(lhandle=%p) = -1 // error=BADFH", lhandle); 
    423432  roar_err_set(ROAR_ERROR_BADFH); 
    424433  return -1; 
    425434 } 
    426435 
    427  lhandle->refc++; 
    428  
    429  if ( lhandle->refc ) 
     436 lhandle->refc--; 
     437 
     438 if ( lhandle->refc ) { 
     439  ROAR_DBG("roar_dl_unref(lhandle=%p) = 0", lhandle); 
    430440  return 0; 
     441 } 
    431442 
    432443 if ( lhandle->lib != NULL && lhandle->lib->unload != NULL ) { 
     
    457468 roar_mm_free(lhandle); 
    458469 
     470 ROAR_DBG("roar_dl_unref(lhandle=%p) = %i", lhandle, ret); 
    459471 return ret; 
    460472} 
Note: See TracChangeset for help on using the changeset viewer.