不幸的是,解决方案尚未起作用;将result.values指针设置为0实际上并不能减少内存的使用。我也尝试了免费(result.values)代替它,但这是不想要的,因为这会删除我的字符串。
编辑2:我将尝试编写一个堆栈析构函数。
编辑3:明白。谢谢DeadMG,编写一个免费(值)的析构函数非常完美!哇..。太简单了。
在用于C++的Unicode库中,ustring类为char*值和其他ustring值设置了operator=函数。在进行简单内存泄漏测试时:
#include <cstdio>
#include "ucpp"
main() {
ustring a;
for(;;)a="MEMORY";
}尽管我对这两个函数都添加了空闲()调用,但程序使用的内存却无法控制地增长(具有内存泄漏严重的程序的特点)。我不知道为什么这是无效的(我是不是错过了其他地方的免费()电话?)
这是当前的库代码:
#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;
}
};发布于 2010-05-02 12:42:55
基本上,您创建了太多的ustring,您需要更多的引用,并且没有实现析构函数,所以当它们都从堆栈中掉下来时,它们就不会被释放。
另外,在赋值操作符中,需要将result.values设置为NULL,否则内存将被删除。您可以使用一个移动操作符,使这是一个很好的快速操作,虽然我仍然不明白你为什么会。
发布于 2010-05-02 11:04:14
,ustring的破坏者在哪里?它不应该释放分配的内存吗?
例如,让我们看看赋值操作符:
ustring operator=(ustring input)
{
ustring result(input);
...
return *this;
}通过值传递一个ustring参数。这可能会导致通过复制构造函数创建副本。再次调用复制构造来初始化result。我怀疑,当您将*this作为ustring返回时(也是通过值而不是引用)调用复制构造。
让我们看看这三种情况中的一种,即result:当这个局部变量超出作用域时(即。在函数的末尾,它将被自动销毁。但是,如果您不提供一个析构函数(~ustring),说明free是所分配的内存,那么您将得到一个内存泄漏。
而且,由于您显然拥有大量的复制构造和按值传递,而没有一个析构函数来释放所分配的内存,所以您将得到很多很多未释放的内存。
此外:为什么你使用malloc 和 free 而不是 new[] 和 delete[]**?*,你可以去掉丑陋的‘type of (Int).calculations and(int*)’类型的类型。而不是:
values = (int *) malloc(sizeof(int) * len);你只需写:
values = new int[len];发布于 2010-05-02 11:06:21
ustring operator=(ustring input) {
ustring result(input);
free(values);
len = input.len;
values = input.values;
return * this;
}为什么在这个文件中声明了result?
https://stackoverflow.com/questions/2753228
复制相似问题