我是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);
}
我不知道内存是否已被释放?
非常感谢任何建议。
答案 0 :(得分:1)
free()
属于void
类型,这意味着您无法检查您的免费是否有效。释放未分配的地址将导致未定义的行为。例如,在Linux上,程序会崩溃。
因此检查你是否真的免费的唯一方法是使用内存调试器。 Valgrind是一个很好的