2013-07-29 13 views
1

我是C的新手。我試圖讓malloc +免費。使用g_malloc0後的可用內存

我有以下結構:

typedef struct { 
    GstElement* pipeline; 
    GFile* file; 
    char* filename; 
} Record; 

我分配的結構,一些內存,並指定一些數據吧:

Record* record_start (const char* filename) 
{ 
    GstElement *pipeline; 
    GFile* file; 
    char* path; 

    pipeline = gst_pipeline_new ("pipeline", NULL); 

    /* same code */ 

    path = g_strdup_printf ("%s.%s", filename, gm_audio_profile_get_extension (profile)); 
    file = g_file_new_for_path (path); 

    Record *record = g_malloc0 (sizeof (Record)); 
    record->file = file;  
    record->filename = path; 
    record->pipeline = pipeline; 

    return record; 
} 

然後我嘗試釋放所有的內存分配:

void record_stop (Record *record) 
{ 
    g_assert (record); 

    /* same code */  

    gst_object_unref (record->pipeline)); 
    g_clear_object (&record->file); 
    g_free (record->filename); 
    g_free (record); 
} 

我不知道內存是否被釋放?

非常感謝您的任何建議。

+1

使用'valgrind' http://valgrind.org/這是非常好的這種類型的信息。你的解脫看起來是正確的。一個建議是,_ALWAYS_檢查返回給你的malloc不是空的。許多程序員忘記了這一點,它可能會導致一些醜陋的錯誤。 –

回答

1

free()void的類型,這意味着您無法檢查您的解除工作與否。釋放未分配的地址將導致未定義的行爲。例如,在Linux上,該程序會崩潰。

因此,檢查是否真正釋放所有內容的唯一方法是使用內存調試器。 Valgrind是一個好的