我的应用程序在stlinux(sh4)中使用,不幸的是valgrind不支持sh4 cpu。 因为我看到了内存泄漏与我的应用程序,我用mtrace,并确认一些内存不是免费的。问题是,在返回中使用malloc的变量,因此我没有任何想法,我怎么可以释放它(因为如果它是免费的,那么在函数中返回是没有意义的)?malloc,函数如何自由返回值
我写cs_malloc(把从上面的链接oscam,simple.c娄代码),命令mtrace日志说,在行:
*tmp = malloc (size);
内存是不是免费的
/* This function encapsulates malloc. It automatically adds an error message to the log if it failed and calls cs_exit(quiterror) if quiterror > -1.
result will be automatically filled with the new memory position or NULL on failure. */
void *cs_malloc(void *result, size_t size, int32_t quiterror){
void **tmp = result;
*tmp = malloc (size);
if(*tmp == NULL){
cs_log("Couldn't allocate memory (errno=%d %s)!", errno, strerror(errno));
if(quiterror > -1) cs_exit(quiterror);
} else {
memset(*tmp, 0, size);
}
return *tmp;
}
而且那么对于malloc的,我把它,像这样:
// create the AES key entry for the linked list
if(!cs_malloc(&new_entry, sizeof(AES_ENTRY), -1)) return;
请看看这3个函数(malloc的是不是免费的,一ND作为其他用户说,Valgrind的权利要求,这些代码导致内存泄漏module-datastruct-llist.c
存储器泄漏由3个不同的部件会导致:
- 在下面的代码
“新”永远不会自由,但由于它使用在该函数的返回,我没有想法,我怎么会释放它:
LL_NODE* ll_append_nolock(LLIST *l, void *obj) { if (l && obj) { LL_NODE *new; if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL; new->obj = obj; if (l->last) l->last->nxt = new; else l->initial = new; l->last = new; l->count++; return new; } }
也是“L”在下面的功能使用,又因为它的回报功能使用,我不知道如何释放它。 :
LLIST *ll_create() { LLIST *l = cs_malloc(&l, sizeof(LLIST), 0); pthread_mutex_init(&l->lock, NULL); return l; }
同样的故事与新:
LL_NODE *ll_prepend(LLIST *l, void *obj) { if (l && obj) { LL_NODE *new; if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL; new->obj = obj; ll_lock(l); new->nxt = l->initial; l->initial = new; if (!l->last) l->last = l->initial; l->count++; ll_unlock(l); return new; } return NULL; }
更多的功能,你可以看到module-datastruct-llist.c
会非常感激,如果任何专家告诉我,我怎么能解决这个问题内存泄漏(如果您觉得,应该重写cs_malloc,或者需要添加新的函数,请写下您的意思源代码。)
对于全部源代码需要更深入地了解更深,请检查该浏览器的TRAC]: http://www.streamboard.tv/oscam/browser/trunk/?rev= 5375 –
您是否考虑在标准x86或x86_64 Linux发行版上编译和测试您的应用程序,并在其上使用'valgrind'? –
除了我的C编译器抱怨从void *到void **的无效隐式赋值(但是我知道它在做什么,因此一个强制转换清除了这个),cs_malloc()函数显得声音(或者我还没有今天早上有足够的咖啡),所以我认为你的问题不在那里。开始检查使用情况。 – WhozCraig