内存未被释放,导致巨大的内存泄漏

Del*_*ani 1 c++ memory memory-leaks

不幸的是,解决方案尚未奏效; 将result.values指针设置为0实际上并没有减少内存使用量.我也尝试过free(result.values)代替那个,但这并不需要,因为删除了我的字符串.

编辑2:我会尝试编写一个堆栈析构函数.

编辑3:陷入困境.感谢DeadMG,编写一个免费(值)完美伎俩的析构函数!哇...这么简单.

在我的C++ Unicode库中,ustring类为char*值和其他ustring值设置了operator = functions.在做简单的内存泄漏测试时:

#include <cstdio>
#include "ucpp"
main() {
  ustring a;
  for(;;)a="MEMORY";
}
Run Code Online (Sandbox Code Playgroud)

程序使用的内存不受控制地增长(具有大内存泄漏的程序的特征),即使我已经为这两个函数添加了free()调用.我不确定为什么这是无效的(我在其他地方错过了free()调用吗?)

这是当前的库代码:

#include <cstdlib>
#include <cstring>
class ustring {
  int * values;
  long len;
  public:
  long length() {
    return len;
  }
  ustring() {
    len = 0;
    values = (int *) malloc(0);
  }
  ustring(const ustring &input) {
    len = input.len;
    values = (int *) malloc(sizeof(int) * len);
    for (long i = 0; i < len; i++)
      values[i] = input.values[i];
  }
  ustring operator=(ustring input) {
    ustring result(input);
    free(values);
    len = input.len;
    values = input.values;
    return * this;
  }
  ustring(const char * input) {
    values = (int *) malloc(0);
    long s = 0;                                                                 // s = number of parsed chars
    int a, b, c, d, contNeed = 0, cont = 0;
    for (long i = 0; input[i]; i++)
      if (input[i] < 0x80) {                                                    // ASCII, direct copy (00-7f)
        values = (int *) realloc(values, sizeof(int) * ++s);
        values[s - 1] = input[i];
      } else if (input[i] < 0xc0) {                                             // this is a continuation (80-bf)
        if (cont == contNeed) {                                                 // no need for continuation, use U+fffd
          values = (int *) realloc(values, sizeof(int) * ++s);
          values[s - 1] = 0xfffd;
        }
        cont = cont + 1;
        values[s - 1] = values[s - 1] | ((input[i] & 0x3f) << ((contNeed - cont) * 6));
        if (cont == contNeed) cont = contNeed = 0;
      } else if (input[i] < 0xc2) {                                             // invalid byte, use U+fffd (c0-c1)
        values = (int *) realloc(values, sizeof(int) * ++s);
        values[s - 1] = 0xfffd;
      } else if (input[i] < 0xe0) {                                             // start of 2-byte sequence (c2-df)
        contNeed = 1;
        values = (int *) realloc(values, sizeof(int) * ++s);
        values[s - 1] = (input[i] & 0x1f) << 6;
      } else if (input[i] < 0xf0) {                                             // start of 3-byte sequence (e0-ef)
        contNeed = 2;
        values = (int *) realloc(values, sizeof(int) * ++s);
        values[s - 1] = (input[i] & 0x0f) << 12;
      } else if (input[i] < 0xf5) {                                             // start of 4-byte sequence (f0-f4)
        contNeed = 3;
        values = (int *) realloc(values, sizeof(int) * ++s);
        values[s - 1] = (input[i] & 0x07) << 18;
      } else {                                                                  // restricted or invalid (f5-ff)
        values = (int *) realloc(values, sizeof(int) * ++s);
        values[s - 1] = 0xfffd;
      }
    len = s;
  }
  ustring operator=(const char * input) {
    ustring result(input);
    free(values);
    len = result.len;
    values = result.values;
    return * this;
  }
  ustring operator+(ustring input) {
    ustring result;
    result.len = len + input.len;
    result.values = (int *) malloc(sizeof(int) * result.len);
    for (long i = 0; i < len; i++)
      result.values[i] = values[i];
    for (long i = 0; i < input.len; i++)
      result.values[i + len] = input.values[i];
    return result;
  }
  ustring operator[](long index) {
    ustring result;
    result.len = 1;
    result.values = (int *) malloc(sizeof(int));
    result.values[0] = values[index];
    return result;
  }
  operator char * () {
    return this -> encode();
  }
  char * encode() {
    char * r = (char *) malloc(0);
    long s = 0;
    for (long i = 0; i < len; i++) {
      if (values[i] < 0x80)
        r = (char *) realloc(r, s + 1),
        r[s + 0] = char(values[i]),
        s += 1;
      else if (values[i] < 0x800)
        r = (char *) realloc(r, s + 2),
        r[s + 0] = char(values[i] >> 6 | 0x60),
        r[s + 1] = char(values[i] & 0x3f | 0x80),
        s += 2;
      else if (values[i] < 0x10000)
        r = (char *) realloc(r, s + 3),
        r[s + 0] = char(values[i] >> 12 | 0xe0),
        r[s + 1] = char(values[i] >> 6 & 0x3f | 0x80),
        r[s + 2] = char(values[i] & 0x3f | 0x80),
        s += 3;
      else
        r = (char *) realloc(r, s + 4),
        r[s + 0] = char(values[i] >> 18 | 0xf0),
        r[s + 1] = char(values[i] >> 12 & 0x3f | 0x80),
        r[s + 2] = char(values[i] >> 6 & 0x3f | 0x80),
        r[s + 3] = char(values[i] & 0x3f | 0x80),
        s += 4;
    }
    return r;
  }
};
Run Code Online (Sandbox Code Playgroud)

sta*_*ica 13

哪里的析构函数ustring?它不应该释放分配的内存吗?


让我们看看例如赋值运算符:

ustring operator=(ustring input)
{
    ustring result(input);
    ...
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

您可以ustring按值传递参数.这可能会导致通过复制构造函数创建副本.您再次调用复制构造进行初始化result.我怀疑你再次调用复制构造时再次*this作为ustring(再次按值,而不是通过引用)返回.

让我们看看这三种情况中的一种,即result:当这个局部变量超出范围时(即在函数末尾),它将被自动销毁.但是如果你没有提供分配内存的析构函数(~ustring)free,你就会得到内存泄漏.

而且由于你显然有许多复制构造和传值,而没有用于释放分配内存的析构函数,你将获得大量的很多不同的内存.


此外:你为什么用mallocfree不是new[]delete[]你可以摆脱丑陋的sizeof(int) * ...计算和(int*)类型转换.代替:

values = (int *) malloc(sizeof(int) * len);
Run Code Online (Sandbox Code Playgroud)

你只需写:

values = new int[len];
Run Code Online (Sandbox Code Playgroud)

  • 当你可以使用`std :: vector`时,为什么要使用`new []`和`delete []`? (7认同)