简体   繁体   English

c内存泄漏问题

[英]c memory leak problem

i have c function in my project which creates a struct and returns the pointer. 我的项目中有一个c函数,该函数创建一个结构并返回指针。

typedef struct object 
{
 float var1; 
 float var2; 
} 
Object; 

Object *createObject(float newVar1, float newVar2) 
{
 Object *object; //create new structure 
 object = (Object*)malloc(sizeof(Object)); //malloc size for struct object
 if(object != NULL) //is memory is malloc'd
 {
  object->var1 = newVar1; //set the data for var1
  object->var2 = newVar2; //set the data for var2
  return object; //return the pointer to the struct
 }
 return NULL; //if malloc fails, return NULL
}

now the struct gets used and after a while i want to delete this structure, i made this function: 现在该结构被使用,过一会儿我想删除该结构,我做了这个功能:

void deleteMarnix(Object *objectPointer)
{
 free(objectPointer); //free the memory the pointer is pointing to
 objectPointer = NULL; //stop it from becomming a dangling pointer
}

this last code snippet shows how i make an object, use it and try to delete it, however, it seems that it doesn't completely free the memory. 最后一个代码片段显示了我如何制作对象,使用它并尝试删除它,但是,它似乎并没有完全释放内存。 what am i doing wrong? 我究竟做错了什么?

Object *object = createObject(21.0f, 1.87f);
//do things here with object.
deleteMarnix(object);

From the snippets you posted, there's no leak. 从您发布的摘录中,没有泄漏。

I think that with: 我认为有:

it seems that it doesn't completely free the memory 似乎不能完全释放内存

you mean that object still holds the old value. 您的意思是该object仍然具有旧值。


In deleteMarnix , when you set objectPointer to NULL , you only set the value of the pointer in that function scope. deleteMarnix ,将objectPointer设置为NULL ,只能在该函数范围内设置指针的值。

It doesn't set the value of the actual pointer object in the outer function. 它不会在外部函数中设置实际指针object的值。

To do that you can either: 为此,您可以:

  1. set it to NULL in the outer function 在外部函数中将其设置为NULL

     Object *object = createObject(21.0f, 1.87f); deleteMarnix( object ); object = NULL; 
  2. pass a pointer to a pointer to your deleteMarnix function: 传递一个指向您的deleteMarnix函数的指针的指针:

     void deleteMarnix(Object **objectPointer) { free(*objectPointer); //free the memory the pointer is pointing to *objectPointer = NULL; //stop it from becomming a dangling pointer } ... Object *object = createObject(21.0f, 1.87f); deleteMarnix( &object ); 

What free() does is not free the memory held by the pointer completely, but actually makes it available for later use if we call a malloc after calling free(). free()所做的不会完全释放指针所拥有的内存,但是,如果我们在调用free()之后调用malloc,实际上会使它可供以后使用。

An evidence to this is you will be able to access the memory location after calling free and before setting the pointer to NULL ( assuming you haven't already called malloc() ). 一个证明是,您可以在调用free之后并且将指针设置为NULL(假设您尚未调用malloc())之前访问内存位置。 Of course the values will be reset to some default values. 当然,这些值将重置为一些默认值。 ( On some compilers i found int was set to 0 ). (在某些编译器上,我发现int设置为0)。

Although there is no memory leak, this might answer your question. 尽管没有内存泄漏,但这可能会回答您的问题。

Let us know :) 让我们知道:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM