【发布时间】:2022-01-10 04:54:53
【问题描述】:
我正在尝试用 C 语言开发一个库,并从内存管理开始。
尝试多次测试我的分配函数和释放函数以测试多个分配和释放。
但是,在免费功能的第二次运行中,我遇到了双重免费和崩溃。
我的简单标题:
#include <stdio.h>
#include <stdlib.h>
#include "xyz_props.h"
#include <assert.h>
#include <string.h>
Header:
#ifndef XYZ_PROPS_H
#define XYZ_PROPS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#define DEFAULT_MAX_KEY_SIZE_IN_BYTES 256
#define DEFAULT_MAX_VALUE_SIZE_IN_BYTES 4096
/*represents a single linked list node with (key,value) pair*/
typedef struct xyz_config_node
{
char* key;
char* value;
struct xyz_config_node* p_next;
} xyz_config_node;
/*represents all properties*/
typedef struct xyz_config_list
{
xyz_config_node* p_head;
int KEY_SIZE_IN_BYTES;
int VALUE_SIZE_IN_BYTES;
} xyz_config_list;
/*declare variables*/
extern xyz_config_list* p_self;
/*===========================================================================
Function: xyz_config_alloc
Description: allocates heap memory for the wrapper xyz_config_list
* that contains the head node.
Inputs: max_key_size in bytes.
* Input of 0 or greater than 4096 will default to 256 bytes
* length for the key size.
*
* max_value_size in bytes.
* Input of 0 or greater than 4096 will default to 4096 bytes
* length for the value size.
Outputs: pointer to xyz_config_list
==========================================================*/
xyz_config_list* xyz_config_alloc(int max_key_size, int max_value_size);
/*===========================================================================
Function: xyz_config_free
Description: Frees heap memory allocated to xyz_config_list & the
* linked list within xyz_config_list.
Inputs: xyz_config_list** pp_self - pass by reference
Outputs: void
References:
Example call: xyz_config_free(&props);
==========================================================*/
void xyz_config_free(xyz_config_list** pp_self);
实现 C 文件:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "xyz_props.h"
xyz_config_list* p_self = NULL;
/*=============================================================================
Private Declarations
==============================================================================*/
xyz_config_list* xyz_config_alloc_helper(int max_key_size, int max_value_size, xyz_config_list** props);
/*=============================================================================
* Implementations
==============================================================================*/
xyz_config_list* xyz_config_alloc(int max_key_size, int max_value_size) {
return xyz_config_alloc_helper(max_key_size, max_value_size,&p_self);
}
xyz_config_list* xyz_config_alloc_helper(int max_key_size, int max_value_size, xyz_config_list** props)
{
if (NULL == *props) {
*props = (xyz_config_list*) calloc(1, sizeof(xyz_config_list));
//set max key size
if (max_key_size > 0 && max_key_size<=4096) {
(*props)->KEY_SIZE_IN_BYTES = max_key_size;
} else {
//defaults to 256
(*props)->KEY_SIZE_IN_BYTES = DEFAULT_MAX_KEY_SIZE_IN_BYTES;
fprintf(stderr,"WARNING xyz_config,xyz_config_alloc_helper(), "
"max_key_size MUST be 0<max_key_size<=4096.max_key_size is set to "
"default 256.\n");
}
//set max value size
if (max_value_size > 0 && max_value_size<=4096) {
(*props)->VALUE_SIZE_IN_BYTES = max_value_size;
} else {
//defaults to 4096
(*props)->VALUE_SIZE_IN_BYTES = DEFAULT_MAX_VALUE_SIZE_IN_BYTES;
fprintf(stderr,"WARNING xyz_config,xyz_config_alloc_helper(), "
"max_value_size MUST be 0<max_value_size<=4096.max_value_size is set to "
"default 4096.\n");
}
}
return *props;
}
void xyz_config_free(xyz_config_list** pp_self)
{
if (NULL!=pp_self && NULL!=(*pp_self))
{
xyz_config_node* p_current = (*pp_self)->p_head;
xyz_config_node* p_next = NULL;
//iterate and free the nodes
while (NULL!=p_current)
{
p_next = p_current->p_next;
//free child attributes
free(p_current->key);
free(p_current->value);
//free the node
free(p_current);
p_current = p_next;
}
//free the super structure
if (NULL!=*pp_self)
{
free (*pp_self); //ERROR HAPPENS ON 2ND TIME HERE.
*pp_self = NULL;
}
}
}
主文件:
/*
*
*/
void test();
void test2();
int main(int argc, char** argv) {
test();
return (EXIT_SUCCESS);
}
/*single alloc & free*/
void test()
{
xyz_config_list* props = xyz_config_alloc(128,1600); //defaults to max_key_size=256,max_value_size=4096
assert(props);
//finally free all memory
xyz_config_free(&props);
assert(NULL==props);
printf("free\n");
}
/*multiple allocs & frees*/
void test2()
{
//1-alloc
xyz_config_list* props = xyz_config_alloc(128,1600); //defaults to max_key_size=256,max_value_size=4096
assert(props);
//1-finally free all memory
xyz_config_free(&props);
assert(NULL==props);
//2- alloc
props = xyz_config_alloc(128,1600); //defaults to max_key_size=256,max_value_size=4096
assert(props);
//2-finally free all memory
xyz_config_free(&props); //CRASH in 2nd free function. Output: RUN FINISHED; Segmentation fault; core dumped;
assert(NULL==props);
printf("free\n");
}
调试器内存和变量在第一次运行时观察:
调试器内存和变量在第 2 次运行出现问题时观察:
任何帮助将不胜感激。
【问题讨论】:
-
为什么你有全局的
p_self变量?我认为如果你摆脱它,调试会容易得多。 -
@EdmCoff,这只是变量所在的位置,它只是指向包含其他数据结构和其他相关数据的结构的指针。如果没有 OOP,就很难做到,不管它与双重释放没有直接关系。你会建议什么模式?
-
@EdmCoff,好的,我删除了
extern关键字。它是早期版本的遗物。
标签: c scope segmentation-fault function-definition null-pointer