在对Linux内核进行静态分析以查找内存泄漏时,我遇到了一个有趣的场景,即我无法找到变量的释放。分配是在以下函数中(使用kmalloc调用)进行的:
static int mounts_open_common(struct inode *inode, struct file *file,
int (*show)(struct seq_file *, struct vfsmount *)){
struct proc_mounts *p;
//some code//
*p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);**
file->private_data = &p->m;//the allocated variable is escaped to file structure
//some code
}
我希望这块内存能够被固定在以下位置:
static int mounts_release(struct inode *inode, struct file *file)
{
struct proc_mounts *p = proc_mounts(file->private_data);
path_put(&p->root);
put_mnt_ns(p->ns);
return seq_release(inode, file);
}
但是看起来这个函数正在访问已分配的变量以释放其内部成员,但不是变量“p”本身。 那么这个变量的内存被释放了吗?如果应该在mounts_release函数中释放它,那么这可能会导致内存泄漏。