[英]C: Freeing a malloc'd array at the wrong time?
我有一个struct cell
->
struct cell {
double x, y, h, g, rhs;
struct key *keys;
};
我正在使用以下方法释放单元格->
void cellFree(struct cell *c) {
// Free the keys
free(c->keys);
// Free the cell itself.
free(c);
}
void cellFreeSors(struct cell *cn) {
int i;
for(i = 0; i < 5; i++) {
// Free keys
free(cn[i].keys);
}
// Free array
free(cn);
}
现在,我所创建的一个malloc数组面临一个怪异的问题。 基本上,我试图找到一个单元格的邻居,并使用以下两种方法根据其值进行一些处理->
struct cell * cellGetSuccessors(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) {
int i;
// CREATE 5 CELLS
struct cell *cn = malloc(5 * sizeof (struct cell));
if (cn == NULL) {
printf("--> Unable to malloc *cn!\n");
errno = ENOMEM;
return NULL;
}
for(i = 0; i < 5; i++) {
cn[i].keys = malloc(sizeof(struct key));
if (cn[i].keys == NULL) {
printf("--> Unable to malloc *cn[%d].keys!\n", i);
errno = ENOMEM;
return NULL;
}
cellCopyValues(&cn[i], c);
}
// MAKE THEM NEIGHBORS
// PROCESS
return cn;
}
double cellRHS(struct cell *c, struct cell *sstart, struct cell *sgoal, double km, struct cell * prevCell) {
// GET NEIGHBORS of c
struct cell *cn = cellGetSuccessors(c, sstart, sgoal, km);
double minsum;
// SOME PROCESS TO UPDATE minsum
minsum = 5.232111; // SAY
// Free memory
cellFreeSors(cn);
return minsum;
}
麻烦的是,当我在cellRHS()
调用cellFreeSors()
, cellRHS()
遇到问题..这就是这些函数的调用方式。
struct cell *u = cellCreateNew();
u->rhs = cellRHS(u, sstart, sgoal, km, prevCell);
queueAdd(&U, u);
当我尝试打印队列时,这给了我一个分段错误->
QUEUE CONTENTS
==================================================================
F -> 0x2354550
L - >0x2354550
(1) [0x2354550] X 50.000000, Y 45.000000 PREV: (nil) NEXT: 0x4014000000000000
Segmentation fault
如您所见,由于某种原因,该条目的NEXT似乎已初始化。 在不使用cellRHS()
情况下执行相同的代码也可以正常工作cellRHS()
>
struct cell *u = cellCreateNew();
queueAdd(&U, u);
QUEUE CONTENTS
==================================================================
F -> 0x2354550
L - >0x2354550
(1) [0x2354550] X 50.000000, Y 45.000000 PREV: (nil) NEXT: (nil)
为什么cellFreeSors()
导致此问题? 我对超出cellRHS
范围的u产生的邻居没有用。 我究竟做错了什么?
谢谢..
**编辑queue_node的结构是
/* QUEUE NODE
* ----------------------------
* Contains a struct cell c and
* reference to next queue_node
*/
struct queue_node {
struct cell *c;
struct queue_node *next;
struct queue_node *prev;
};
/* PRIORITY QUEUE
* ----------------------------
* The queue itself, with first
* and last pointers to queue_nodes
*/
struct priority_queue {
struct queue_node *first;
struct queue_node *last;
};
queuePrint()
方法显示队列内容->
void queuePrint(struct priority_queue *q)
{
printf("\n\n\tQUEUE CONTENTS\n\t==================================================================\n");
int i = 1;
struct queue_node *temp = q->first;
printf("\tF -> %p\n\tL -> %p\n", q->first, q->last);
while(temp != NULL) {
printf("\t(%d) [%p]\tX %f, Y %f\tPREV: %p\tNEXT: %p", i, temp, temp->c->x, temp->c->y, temp->prev, temp->next);
printf("\n");
temp = temp->next;
i++;
}
printf("\n\n");
}
这里的一些建议:
使用集中式allocCell()和freeCell()方法。 这使您可以精确地跟踪每个分配和释放的单元格。
在每个单元格上放置一个ID。 同样,这允许进行详细的跟踪。 如果空间有限,可以将其编译为生产方式,将其编译为调试方式。
为单元编写跟踪例程,以查看例程仅释放分配的单元,任何单元仅释放一次,依此类推。您甚至可以编写哈希表来存储分配和释放的单元的ID(当然只是在调试过程中)并编写验证检查。
如果不这样做,您可能会再次遇到内存错误。
即使执行此操作,也将再次遇到内存错误,但是这些工具将帮助您下次更快地对其进行跟踪。
切换到C#或Java,不释放任何东西:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.