Я продолжаю получать сообщения об ошибках от компилятора, говоря, что есть утечка памяти, два 8-байтовых блока и один 5-байтовый блок. Я уже удалил массив *name
в деструкторе. Как дескриптор дескриптора? У меня сложилось впечатление, что после того, как основной закончатся, объекты fruit
будут удалены, поскольку они вышли из сферы действия.Как остановить утечку памяти?
#include "Fruit.h"
#include "LeakWatcher.h"
using namespace std;
Fruit::Fruit(const Fruit &temp)
{
name = temp.name;
for(int i = 0; i < CODE_LEN - 1; i++)
{
code[i] = temp.code[i];
}
}
void Fruit::operator=(const Fruit &tempFruit)
{
name = tempFruit.name;
for(int i = 0; i < CODE_LEN; i++)
{
code[i] = tempFruit.code[i];
}
}
Fruit::~Fruit()
{
delete[] name;
}
bool Fruit::operator==(const Fruit &tempFruit)
{
int i = 0;
while(name[i] != NULL && tempFruit.name[i] != NULL)
{
if(name[i] != tempFruit.name[i])
return false;
i++;
}
if(name[i] != NULL || tempFruit.name[i] != NULL)
return false;
return true;
}
bool Fruit::operator<(const Fruit &tempFruit)
{
int i = 0;
while(name[i] != NULL && tempFruit.name[i] != NULL)
{
if((int)name[i] < (int)tempFruit.name[i])
return true;
else if((int)name[i] > (int)tempFruit.name[i])
return false;
i++;
}
if(name[i] == NULL && tempFruit.name[i] != NULL)
return true;
else
return false;
}
std::ostream & operator<<(std::ostream &os, const Fruit *printFruit)
{
os << setiosflags(ios::left) << setw(MAX_NAME_LEN) << printFruit->name << " ";
for(int i = 0; i < CODE_LEN; i++)
{
os << printFruit->code[i];
}
os << endl;
return os;
}
std::istream & operator>>(std::istream &is, Fruit *readFruit)
{
string tempString;
is >> tempString;
int size = tempString.length();
readFruit->name = new char[tempString.length()];
for(int i = 0; i <= (int)tempString.length(); i++)
{
readFruit->name[i] = tempString[i];
}
readFruit->name[(int)tempString.length()] = '\0';
for(int i =0; i < CODE_LEN; i++)
{
is >> readFruit->code[i];
}
return is;
}
void stuff()
{
}
void main()
{
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
Fruit *fruit = new Fruit();
Fruit *fruit1 = new Fruit();
cin >> fruit;
*fruit1 = *fruit;
cout << fruit << fruit1;
_CrtDumpMemoryLeaks();
}
H
#ifndef _FRUIT_H
#define _FRUIT_H
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
enum { CODE_LEN = 4 };
enum { MAX_NAME_LEN = 30 };
class Fruit
{
private:
char *name;
char code[CODE_LEN];
public:
Fruit(const Fruit &temp);
Fruit(){name = NULL;};
bool operator<(const Fruit &other);
friend std::ostream & operator<<(std::ostream &os, const Fruit *printFruit);
bool operator==(const Fruit &other);
bool operator!=(const Fruit &other){return!(*this==other);};
friend std::istream & operator>>(std::istream& is, Fruit *readFruit);
void Fruit::operator=(const Fruit &tempFruit);
~Fruit();
};
#endif
Возможно, вам нужно удалить 'fruit' и' fruit1'? Также обратите внимание, что вы присваиваете 'fruit'' fruit1', что означает, что вы никогда не сможете освободить второй объект. –
Простой: прекратите использование 'new' всюду. Если вы настаиваете на его использовании, следуйте правилу [* of three *] (http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29) и не забудьте «удалить» все, что вы «новое». На данный момент это не похоже на то, что «Фрукты» безопасно переписываются или присваиваются. – juanchopanza
Вы проверяете утечки памяти * до того, как * переменные выходят из области видимости. Конечно, будут обнаружены утечки. Но опять же вы не используете 'delete'. –