1

私はCに慣れていません。malloc + freeに慣れようとしています。

私は次の構造を持っています:

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);
}

メモリは解放されましたか?

4

1 に答える 1

1

free()つまりvoid、解放が機能したかどうかを確認できません。割り当てられていないアドレスを解放すると、未定義の動作が発生します。たとえば、Linux では、プログラムがクラッシュします。

したがって、本当にすべてを解放したかどうかを確認する唯一の方法は、メモリ デバッガを使用することです。Valgrindは良いものです。

于 2013-08-05T13:57:51.910 に答える