Changeset 62 in libcf


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

#1 fix memory leak under debug util

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/cf_debug.h

    r54 r62  
    5555
    5656/**
     57 * 디버깅 모듈 초기화
     58 *
     59 * @see CF_Debug_Initialize
     60 */
     61# define CF_DEBUG_INITIALIZE                                        \
     62    CF_Debug_Initialize ()
     63
     64/**
     65 * 디버깅 모듈 해제
     66 *
     67 * @see CF_Debug_Finalize
     68 */
     69# define CF_DEBUG_FINALIZE                                          \
     70    CF_Debug_Finalize ()
     71
     72/**
    5773 * 함수에 진입
    5874 *
    5975 * @see CF_Debug_EnterFunction
    6076 */
    61 # define CF_DEBUG_BEGIN_FUNCTION                                \
     77# define CF_DEBUG_BEGIN_FUNCTION                                    \
    6278    CF_Debug_EnterFunction (__FILE__,__func__,__LINE__)
    6379
     
    6783 * @see CF_Debug_LeaveFunction
    6884 */
    69 # define CF_DEBUG_END_FUNCTION                  \
     85# define CF_DEBUG_END_FUNCTION                                      \
    7086    CF_Debug_LeaveFunction ()
    7187
     
    7389# define CF_DEBUG_PRINT(__fp,__fmt,...)
    7490# define CF_DEBUG_PRINT_BIN(__fp,__bin,__len,__fmt,...)
     91# define CF_DEBUG_INITIALIZE
     92# define CF_DEBUG_FINALIZE
    7593# define CF_DEBUG_BEGIN_FUNCTION
    7694# define CF_DEBUG_END_FUNCTION
     
    98116
    99117CF_EXPORT int
     118CF_Debug_Initialize     (void);
     119
     120CF_EXPORT int
     121CF_Debug_Finalize       (void);
     122
     123CF_EXPORT int
    100124CF_Debug_PrintCallStack (FILE * fp);
    101125
  • 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);
  • trunk/test/debug.c

    r51 r62  
    2323        "『 절원의 템페스트 OP2 Theme 』\n";
    2424
     25    CF_DEBUG_INITIALIZE;
    2526    CF_DEBUG_BEGIN_FUNCTION;
    2627
     
    3233
    3334    CF_DEBUG_END_FUNCTION;
     35    CF_DEBUG_FINALIZE;
    3436
    3537    return 0;
  • trunk/test/test.c

    r61 r62  
    114114        if (CF_Thread_Join (&tid[i]) < 0)
    115115            CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
     116        if (CF_Thread_Release (&tid[i]) < 0)
     117            CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
    116118    }
    117119
     
    255257void test_socket (void)
    256258{
    257     CF_Thread tid[THREAD_POOL];
     259    CF_Thread stid[THREAD_POOL];
     260    CF_Thread ctid[THREAD_POOL];
    258261
    259262    int sock = 0;
     
    289292    for (iter = 0 ; iter < THREAD_POOL ; iter++)
    290293    {
    291         if (CF_Thread_Create (&tid[iter], socket_echo_server, &sock) < 0)
     294        if (CF_Thread_Create (&stid[iter], socket_echo_server, &sock) < 0)
    292295        {
    293296            CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
     
    299302    for (iter = 0 ; iter < THREAD_POOL ; iter++)
    300303    {
    301         CF_Thread dummy;
    302         if (CF_Thread_Create (&dummy, socket_echo_client, &sock) < 0)
     304        if (CF_Thread_Create (&ctid[iter], socket_echo_client, &sock) < 0)
    303305        {
    304306            CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
     
    310312    for (iter = 0 ; iter < THREAD_POOL ; iter++)
    311313    {
    312         CF_Thread_Join (&tid[iter]);
    313         CF_Thread_Release (&tid[iter]);
     314        CF_Thread_Join (&ctid[iter]);
     315        CF_Thread_Release (&ctid[iter]);
     316
     317        CF_Thread_Join (&stid[iter]);
     318        CF_Thread_Release (&stid[iter]);
     319
    314320        CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
    315321    }
     
    327333int main (int argc, char ** argv)
    328334{
     335    CF_DEBUG_INITIALIZE;
    329336    CF_DEBUG_BEGIN_FUNCTION;
    330337
     
    348355
    349356    CF_DEBUG_END_FUNCTION;
     357    CF_DEBUG_FINALIZE;
    350358
    351359    return 0;
Note: See TracChangeset for help on using the changeset viewer.