C 为什么这个断言失败了?
我想创建lirc命令来停止录制。我对此有3个文件: 文件:record.c 文件:rec_tech.c 文件rec_tech.h 更新 如果在lirc.c中,添加行记录*数据C 为什么这个断言失败了?,c,C,我想创建lirc命令来停止录制。我对此有3个文件: 文件:record.c 文件:rec_tech.c 文件rec_tech.h 更新 如果在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 *
...
#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);
}
...
为什么?在录制停止()
中的g\u assert()
检查录制
是否不是空指针,但在exec\u lirc\u命令()
中修改的调用会传递空指针。因此,断言失败了
道德-不要将空指针传递给不需要它们的函数。上述代码中的
数据未在execute\u lirc\u命令中定义
如何在execute\u lirc\u命令中定义数据?记录*记录=数据<代码>g_断言(记录)真正的问题是,为什么不呢?你似乎在写你不懂的随机代码,并希望它能起作用。您试图传递一个名为data的变量,但尚未声明该变量,因此编译器当然会给您一个错误。正确的反应不是仅仅将数据更改为NULL并希望它神奇地工作,而是找出该参数的用途并为其提供适当的值。
...
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);
}
...
...
#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);
...
...
#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);
}
...
error: 'data' undeclared (first use in this function)
...
#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)