全局结构内存错误
Global struct memory error
我的程序使用 libconfig
从配置文件中读取并将值保存到全局结构。它工作正常,但 valgrind 说有错误。并且错误仅针对 char 指针变量显示。该错误是什么以及如何解决?谢谢
#include <stdio.h>
#include <libconfig.h>
#include "stdlib.h"
#define conf_file "myconf"
struct setting_data
{
int number;
const char* timeformat;
};
struct setting_data conf_data;
void read_config();
int main(){
read_config();
printf("%d @ %p\n", conf_data.number,&conf_data.number);
printf("%s @ %p\n", conf_data.timeformat,&conf_data.timeformat);
}
void read_config(){
config_t cfg;
// config_setting_t *rules, *settings,*m_number,*device,*sendduration,*pin_code;
config_init(&cfg);
if(! config_read_file(&cfg, conf_file))
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
exit(1);
}
// conf_data.number = config_setting_get_int (config_lookup(&cfg, "number" ));
config_lookup_int(&cfg,"number",&conf_data.number);
config_lookup_string(&cfg, "timeformat", &conf_data.timeformat);
config_destroy(&cfg);
}
==8238== Invalid read of size 1
==8238== at 0x50AFBC9: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1317)
==8238== by 0x5083972: vfprintf (vfprintf.c:1629)
==8238== by 0x508C269: printf (printf.c:35)
==8238== by 0x400993: main (test.c:16)
....
==8238== Address 0x53cdb0c is 12 bytes inside a block of size 15 free'd
==8238== ERROR SUMMARY: 43 errors from 5 contexts (suppressed: 4 from 4)
libconfig 向您传递一个指向其内部字符串存储的指针,用于 timeformat
值。当您执行 config_destroy()
导致全局结构的时间格式指针无效时,它会被释放。为避免这种情况 strdup()
libconfig 返回给您的时间格式字符串:
const char *time_str;
config_lookup_string(&cfg, "timeformat", &time_str);
conf_data.timeformat = strdup(time_str);
config_destroy(&cfg);
/* conf_data.timeformat is still valid */
Storage for the string returned by config_lookup_string() is managed by the library and released automatically when the setting is destroyed or when the setting's value is changed; the string must not be freed by the caller.
所以config_lookup_string()
返回的指针如果要在config_destroy()
之后使用需要复制到另一个数组中
我的程序使用 libconfig
从配置文件中读取并将值保存到全局结构。它工作正常,但 valgrind 说有错误。并且错误仅针对 char 指针变量显示。该错误是什么以及如何解决?谢谢
#include <stdio.h>
#include <libconfig.h>
#include "stdlib.h"
#define conf_file "myconf"
struct setting_data
{
int number;
const char* timeformat;
};
struct setting_data conf_data;
void read_config();
int main(){
read_config();
printf("%d @ %p\n", conf_data.number,&conf_data.number);
printf("%s @ %p\n", conf_data.timeformat,&conf_data.timeformat);
}
void read_config(){
config_t cfg;
// config_setting_t *rules, *settings,*m_number,*device,*sendduration,*pin_code;
config_init(&cfg);
if(! config_read_file(&cfg, conf_file))
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
exit(1);
}
// conf_data.number = config_setting_get_int (config_lookup(&cfg, "number" ));
config_lookup_int(&cfg,"number",&conf_data.number);
config_lookup_string(&cfg, "timeformat", &conf_data.timeformat);
config_destroy(&cfg);
}
==8238== Invalid read of size 1
==8238== at 0x50AFBC9: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1317)
==8238== by 0x5083972: vfprintf (vfprintf.c:1629)
==8238== by 0x508C269: printf (printf.c:35)
==8238== by 0x400993: main (test.c:16)
....
==8238== Address 0x53cdb0c is 12 bytes inside a block of size 15 free'd
==8238== ERROR SUMMARY: 43 errors from 5 contexts (suppressed: 4 from 4)
libconfig 向您传递一个指向其内部字符串存储的指针,用于 timeformat
值。当您执行 config_destroy()
导致全局结构的时间格式指针无效时,它会被释放。为避免这种情况 strdup()
libconfig 返回给您的时间格式字符串:
const char *time_str;
config_lookup_string(&cfg, "timeformat", &time_str);
conf_data.timeformat = strdup(time_str);
config_destroy(&cfg);
/* conf_data.timeformat is still valid */
Storage for the string returned by config_lookup_string() is managed by the library and released automatically when the setting is destroyed or when the setting's value is changed; the string must not be freed by the caller.
所以config_lookup_string()
返回的指针如果要在config_destroy()