为什么这个断言失败了?

时间:2012-07-26 23:01:58

标签: c

我想创建lirc命令来停止录制。我有3个文件:

文件:record.c

...
#include "rec_tech.h"
...
void stop_rec_button_clicked_cb(GtkButton *button, gpointer data)
{
    Recording *recording = data;
    close_status_window();
    recording_stop(recording);
}
... 

文件:rec_tech.c

...
void recording_stop(Recording *recording)
{
    g_assert(recording);

    GstState state;
    gst_element_get_state(recording->pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
    if (state != GST_STATE_PLAYING) {
        GST_DEBUG ("pipeline in wrong state: %s", gst_element_state_get_name (state));
    } else {
        gst_element_set_state(recording->pipeline, GST_STATE_NULL);
    }
    gst_object_unref(GST_OBJECT(recording->pipeline));
    g_free(recording->filename);
    g_free(recording->station);
    g_free(recording);
}   
... 

file rec_tech.h

...
#ifndef _REC_TECH_H
#define _REC_TECH_H
#include <gst/gst.h>
....
typedef struct {
    GstElement* pipeline;
    char* filename;
    char* station;
} Recording;

Recording*
recording_start(const char* filename);

void
recording_stop(Recording* recording);
...

文件:lirc.c

...
#include <lirc/lirc_client.h>
#include "lirc.h"
#include "rec_tech.h"
#include "record.h"

static void execute_lirc_command (char *cmd)
{
        printf("lirc command: %s\n", cmd);

        if (strcasecmp (cmd, "stop recording") == 0) {
            stop_rec_button_clicked_cb(NULL, data);
        }
...

当我尝试在文件lirc.c中编译get error时

error: 'data' undeclared (first use in this function)

更新

如果在lirc.c中添加行记录*数据;

...
#include <lirc/lirc_client.h>
#include "lirc.h"
#include "rec_tech.h"
#include "record.h"

Recording *data;    
static void execute_lirc_command (char *cmd)
{
        printf("lirc command: %s\n", cmd);

        if (strcasecmp (cmd, "stop recording") == 0) {
            stop_rec_button_clicked_cb(NULL, data);
        }
...

得到此运行时错误:

ERROR:rec_tech.c:recording_stop: assertion failed: (recording)

为什么?

1 个答案:

答案 0 :(得分:3)

g_assert()中的recording_stop()检查recording不是空指针,但exec_lirc_command()中的修改后的调用会传递空指针。因此断言失败了。

道德 - 不要将空指针传递给不期望它们的函数。