Changeset 62 in libcf for trunk/src/cf_debug.c


Ignore:
Timestamp:
04/08/13 11:03:03 (11 years ago)
Author:
cheese
Message:

#1 fix memory leak under debug util

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cf_debug.c

    r55 r62  
    2626        return CF_ERROR_DEBUG_INVALID_CTX
    2727
    28 #define GET_CTX_OSTREAM(__ctx)              \
    29     ((((CF_DEBUG_CTX *)__ctx)->fp == NULL)  \
    30      ? stderr                               \
    31      : ((CF_DEBUG_CTX *)__ctx)->fp)
    32 
    3328/**
    3429 * 디버그 컨텍스트
     
    6661    int         line;
    6762
    68     FILE        * fp;
    6963    CF_Mutex    mutex;
    7064
     
    135129        }
    136130        fprintf (fp, "\n");
    137     }
    138 
    139     return CF_OK;
    140 }
    141 
    142 /**
    143  * 디버그 컨텍스트를 해제
    144  *
    145  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    146  *
    147  * @param ctx 디버그 컨텍스트
    148  *
    149  * @see CF_DEBUG_DESTROY_CTX
    150  */
    151 static int
    152 CF_Debug_DestroyCtx (CF_Debug_Ctx ctx)
    153 {
    154     CF_DEBUG_CTX        * context = (CF_DEBUG_CTX *) ctx;
    155     CF_DEBUG_CALLSTACK  * pop = NULL;
    156     CF_DEBUG_CALLSTACK  * next = NULL;
    157 
    158     ASSERT_CTX (ctx);
    159 
    160     if (context->fp != NULL)
    161         fclose (context->fp);
    162 
    163     for (pop = next = context->callstack.caller ; pop ; pop = next)
    164     {
    165         next = next->caller;
    166         free (pop);
    167     }
    168 
    169     if (context->mutex)
    170         CF_Mutex_Destory (&context->mutex);
    171 
    172     free (context);
    173 
    174     return CF_OK;
    175 }
    176 
    177 /**
    178  * 디버그 컨텍스트를 생성
    179  *
    180  * @return 성공 시, CF_Debug_Ctx 형태의 컨텍스트; 실패 시, NULL
    181  * @see CF_DEBUG_CREATE_CTX
    182  */
    183 static int
    184 CF_Debug_CreateCtx (CF_Debug_Ctx * ctx)
    185 {
    186     int             result = 0;
    187     CF_DEBUG_CTX    * context = NULL;
    188 
    189     TRY
    190     {
    191         context = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
    192         if (context == NULL)
    193         {
    194             result = CF_ERROR_DEBUG_ALLOCATE_CTX;
    195             TRY_BREAK;
    196         }
    197 
    198         result = CF_Mutex_Create (&context->mutex);
    199         if (result < 0)
    200         {
    201             TRY_BREAK;
    202         }
    203 
    204         *ctx = (CF_Debug_Ctx) context;
    205     }
    206     CATCH_IF (result < 0)
    207     {
    208         CF_Debug_DestroyCtx (context);
    209131    }
    210132
     
    395317
    396318/**
    397  * 현재 콜스택을 출력
     319 * 디버그 컨텍스트를 해제
     320 *
     321 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     322 *
     323 * @param ctx 디버그 컨텍스트
     324 *
     325 * @see CF_DEBUG_DESTROY_CTX
     326 */
     327static int
     328CF_Debug_DestroyCtx (CF_Debug_Ctx ctx)
     329{
     330    CF_DEBUG_CTX        * context = (CF_DEBUG_CTX *) ctx;
     331
     332    ASSERT_CTX (ctx);
     333
     334    while (context->callstack.caller)
     335        CF_Debug_CallStackPop (ctx, NULL);
     336
     337    if (context->mutex)
     338        CF_Mutex_Destory (&context->mutex);
     339
     340    free (context);
     341
     342    return CF_OK;
     343}
     344
     345/**
     346 * 디버그 컨텍스트를 생성
     347 *
     348 * @return 성공 시, CF_Debug_Ctx 형태의 컨텍스트; 실패 시, NULL
     349 * @see CF_DEBUG_CREATE_CTX
     350 */
     351static int
     352CF_Debug_CreateCtx (CF_Debug_Ctx * ctx)
     353{
     354    int             result = 0;
     355    CF_DEBUG_CTX    * context = NULL;
     356
     357    TRY
     358    {
     359        context = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
     360        if (context == NULL)
     361        {
     362            result = CF_ERROR_DEBUG_ALLOCATE_CTX;
     363            TRY_BREAK;
     364        }
     365
     366        result = CF_Mutex_Create (&context->mutex);
     367        if (result < 0)
     368        {
     369            TRY_BREAK;
     370        }
     371
     372        *ctx = (CF_Debug_Ctx) context;
     373    }
     374    CATCH_IF (result < 0)
     375    {
     376        CF_Debug_DestroyCtx (context);
     377    }
     378
     379    return CF_OK;
     380}
     381
     382/**
     383 * 콜스택 매니저 초기화 (글로벌 컨텍스트)
     384 *
     385 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     386 *
     387 * @see CF_Debug_Finalize
     388 */
     389int
     390CF_Debug_Initialize (void)
     391{
     392    int result = 0;
     393
     394    if (gDebugSingleCtx == NULL)
     395    {
     396        result = CF_Debug_CreateCtx (&gDebugSingleCtx);
     397        if (result != CF_OK)
     398            return result;
     399    }
     400
     401    return CF_OK;
     402}
     403
     404/**
     405 * 콜스택 매니저 해제 (글로벌 컨텍스트)
     406 *
     407 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     408 */
     409int
     410CF_Debug_Finalize (void)
     411{
     412    return CF_Debug_DestroyCtx (gDebugSingleCtx);
     413}
     414
     415/**
     416 * 현재 콜스택을 출력 (글로벌 컨텍스트)
    398417 *
    399418 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     
    427446
    428447/**
    429  * 함수 진입을 명시
     448 * 함수 진입을 명시 (글로벌 컨텍스트)
    430449 *
    431450 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     
    442461                        const int   line)
    443462{
    444     int             result = 0;
    445     CF_DEBUG_CTX    * ctx = NULL;
    446 
    447     if (gDebugSingleCtx == NULL)
    448     {
    449         result = CF_Debug_CreateCtx (&gDebugSingleCtx);
    450         if (result != CF_OK)
    451             return result;
    452     }
    453     ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
     463    CF_DEBUG_CTX * ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
     464
     465    ASSERT_CTX (ctx);
    454466
    455467    CF_Mutex_Lock (&ctx->mutex);
     
    461473
    462474/**
    463  * 함수 종료를 명시
     475 * 함수 종료를 명시 (글로벌 컨텍스트)
    464476 *
    465477 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     
    472484    CF_DEBUG_CTX * ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
    473485
    474     if (ctx == NULL)
    475         return CF_ERROR_DEBUG_INVALID_CTX;
     486    ASSERT_CTX (ctx);
    476487
    477488    CF_Mutex_Lock (&ctx->mutex);
Note: See TracChangeset for help on using the changeset viewer.