代码之家  ›  专栏  ›  技术社区  ›  Matthew Mitchell

“未分配释放的指针”内存块由于某些原因无法释放

  •  1
  • Matthew Mitchell  · 技术社区  · 16 年前

    我正在努力寻找我为什么不能释放内存块。指针一定有问题。结构的内存块在函数中生成,使用的指针存储在数组中。稍后,指针从数组中取出,用于释放内存。

    我已经弄清楚它是哪个免费的了。我把“//这一个”放在它旁边。

    #include <stdlib.h>
    
    typedef enum {
        INT = 0,
        FLOAT = 1,
        STRING = 2,
        NONE = 3,
        BOOL = 4
    } TYPE;
    
    typedef struct {
        TYPE type;
        void * data;
    } Variable;
    
    typedef Variable ** var; //Convenience typedef for pointer to variable pointer
    
    typedef struct {
        size_t size;
        Variable *** pool; //Pointer to the array of pointers to the Variable structure pointers. Pointer required for memory allocation and array consists of pointers to pointers so the pointers to the Variable structures can be changed throughtout different functions.
    } VarPool; //Variable pools will just be used to deallocate all variables at the end of a scope
    
    VarPool * global_pool; //Pool for global scope
    VarPool ** pool_stack; //Keeps track of pools in stack
    unsigned int pool_stack_size;
    
    void init_pool_stack(){
        pool_stack = malloc(sizeof(VarPool *)); //Start with global_pool
        pool_stack_size = 1;
        global_pool = malloc(sizeof(VarPool));
        pool_stack[0] = global_pool;
        global_pool->pool = NULL;
        global_pool->size = 0;
    }
    
    Variable ** new_var(){ //Makes new variable
        Variable ** return_variable;
        Variable * new_variable = malloc(sizeof(Variable));
        VarPool * var_pool = pool_stack[pool_stack_size-1]; //Current variable pool on top of stack
        var_pool->size++;
        var_pool->pool = realloc(var_pool->pool,var_pool->size*sizeof(Variable **));
        return_variable = &new_variable;
        var_pool->pool[var_pool->size - 1] = return_variable;
        return return_variable; //Return pointer to new pointer so pointer can be changed to NULL when deleted
    }
    void empty_pool(){ //Frees all data from variable pool
        VarPool * var_pool = pool_stack[pool_stack_size-1]; //Current pool on top of stack
        for (int x = 0; x < var_pool->size; x++) {
            free(*var_pool->pool[x]); //Free variable data
        }
        free(var_pool->pool); //Free pool variable array
        free(var_pool); //This one
        pool_stack_size--;
        pool_stack = realloc(pool_stack, pool_stack_size*sizeof(VarPool *));
    }
    
    int main (int argc, const char * argv[]) {
        init_pool_stack();
        new_var();
        empty_pool(); //Finally empty globals pool which will deallocate pool_stack
        return 0;
    }
    

    谢谢你的帮助。

    1 回复  |  直到 16 年前
        1
  •  7
  •   pmg    16 年前

    在里面 new_var() 你已经(简化)

    Variable ** new_var(){ //Makes new variable
      Variable ** return_variable;
      Variable * new_variable = malloc(sizeof(Variable));
    
      return_variable = &new_variable;
      return return_variable;
    }
    

    函数结束后,从该函数返回的值无效。 局部变量的地址只有在该变量存在时才有意义。