Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 内存未被释放,导致巨大内存泄漏_C++_Memory_Memory Leaks - Fatal编程技术网

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

C++ 内存未被释放,导致巨大内存泄漏,c++,memory,memory-leaks,C++,Memory,Memory Leaks,不幸的是,这些解决方案尚未奏效;将result.values指针设置为0实际上并不会减少内存使用。我也尝试用free(result.values)来代替它,但这并不理想,因为它会删除我的字符串 编辑2:我将尝试编写堆栈析构函数 ustring& operator=(const char * input) { ustring result(input); free(values); len = result.len; values = result.valu

不幸的是,这些解决方案尚未奏效;将result.values指针设置为0实际上并不会减少内存使用。我也尝试用free(result.values)来代替它,但这并不理想,因为它会删除我的字符串

编辑2:我将尝试编写堆栈析构函数

ustring& operator=(const char * input) {
    ustring result(input);
    free(values);
    len = result.len;
    values = result.values;
    result.values = 0;
    return * this;
}
编辑3:Gotcha。感谢DeadMG,编写了一个析构函数,free(values)完美地完成了这个任务!哇!很简单。

在我的Unicode库C++中,Urutin类具有Cal**函数和chur*值和其他WORDEN值。执行简单内存泄漏测试时:

#include <cstdio>
#include "ucpp"
main() {
  ustring a;
  for(;;)a="MEMORY";
}
#包括
#包括“ucpp”
main(){
对a进行分类;
对于(;;)a=“内存”;
}
程序使用的内存无法控制地增长(具有大内存泄漏的程序的特征),即使我已经为这两个函数都添加了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;
  }
};
#包括
#包括
阶级斗争{
int*值;
龙伦;
公众:
长(){
回程透镜;
}
ustring(){
len=0;
值=(int*)malloc(0);
}
ustring(施工和输入){
len=input.len;
值=(int*)malloc(sizeof(int)*len);
对于(长i=0;i12 | 0xe0),
r[s+1]=char(值[i]>>6&0x3f | 0x80),
r[s+2]=char(值[i]&0x3f | 0x80),
s+=3;
其他的
r=(char*)realloc(r,s+4),
r[s+0]=char(值[i]>>18 | 0xf0),
r[s+1]=char(值[i]>>12&0x3f | 0x80),
r[s+2]=char(值[i]>>6&0x3f | 0x80),
r[s+3]=字符(值[i]&0x3f | 0x80),
s+=4;
}
返回r;
}
};

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


让我们看一下赋值运算符:

ustring operator=(ustring input)
{
    ustring result(input);
    ...
    return *this;
}
通过值传递
ustring
参数。这可能会导致通过复制构造函数创建副本。另一次调用复制构造来初始化
结果
。我怀疑您再次调用copy construction,将
*this
返回为
ustring
(同样是通过值,而不是通过引用)

让我们看看这三种情况中的一种,即
result
:当此局部变量超出范围(即在函数末尾)时,它将自动销毁。但是如果您不提供一个析构函数(
~ustring
)来
释放分配的内存,您将得到内存泄漏

而且,由于您显然有大量的拷贝构造和值传递,而没有解构函数来释放分配的内存,因此您将获得大量的未绑定内存


此外:为什么要使用
malloc
free
而不是
new[]
delete[]
您可以摆脱丑陋的
sizeof(int)*……
计算和
(int*)
类型转换。而不是:

values = (int *) malloc(sizeof(int) * len);
你只需写下:

values = new int[len];

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


让我们看一下赋值运算符:

ustring operator=(ustring input)
{
    ustring result(input);
    ...
    return *this;
}
通过值传递
ustring
参数。这可能会导致通过复制构造函数创建副本。另一次调用复制构造来初始化
结果
。我怀疑您再次调用copy construction,将
*this
返回为
ustring
(同样是通过值,而不是通过引用)

让我们看看这三种情况中的一种,即
result
:当此局部变量超出范围(即在函数末尾)时,它将自动销毁。但是如果您不提供一个析构函数(
~ustring
)来
释放分配的内存,您将得到内存泄漏

而且,由于您显然有大量的拷贝构造和值传递,而没有解构函数来释放分配的内存,因此您将获得大量的未绑定内存


此外:为什么要使用
malloc
free
而不是
new[]
delete[]
您可以摆脱丑陋的
sizeof(int)*……
计算和
(int*)
类型转换。而不是:

values = (int *) malloc(sizeof(int) * len);
你只需写下:

values = new int[len];
为什么要在此声明
result

为什么要在此声明
result

  • 尝试使用以避免内存泄漏问题。如果将数组包装成类似于的格式,则不必编写显式析构函数来释放内存
  • ustring运算符=(ustring输入)
    应为
    us
    
    ustring& operator=(const char * input) {
        ustring result(input);
        free(values);
        len = result.len;
        values = result.values;
        result.values = 0;
        return * this;
    }