- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不断收到编译器的错误消息,说存在内存泄漏,两个 8 字节 block 和一个 5 字节 block 。我已经在析构函数中删除了 *name
数组。析构函数如何处理作用域?我的印象是,一旦 main 结束,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”指针 - _CrtDumpMemoryLeaks() 检查尚未释放的内存。调用之前需要删除这两个指向的数据。
也就是说,您还有其他问题。
C++ 没有自动垃圾回收。您必须跟踪和管理您的内存分配或使用为您完成的类/代码。
考虑以下代码:
void iNeedALeak() {
void* p = new char[64];
strcpy(p, "Hello world");
std::cout << p << std::endl;
}
此代码进行了分配,并将其存储在“p”中。即使我们使用了几个函数的值,我们也从未存储过它。因此,它永远不会返回到系统。
代码中等待发生泄漏的一个示例是在运算符中>>
std::istream & operator>>(std::istream &is, Fruit *readFruit)
{
string tempString;
is >> tempString;
int size = tempString.length();
readFruit->name = new char[tempString.length()];
是的,在您的删除运算符中,您从名称中删除了[]。但这仅处理您的代码达到 ~Fruit 的情况,请考虑:
Fruit f;
cin >> f; // readFruit->name = new char[]..
cin >> f; // readFruit->name = new char[]...
此时,您不再存储原始值,也没有删除它。
您真正遇到的问题是您使用的是非常类似于 C 的方法。如果你打算用 C++ 编写它,你应该考虑使用 RAII ,例如,您可以使用 std::unique_ptr类。
TL;DR 不要公开原始指针,将它们封装在某些东西后面,以确保它们在超出范围或重新分配给它们时得到释放。
关于c++ - 如何阻止内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19239609/
我刚开始学习JSP技术,遇到了瓶颈。 如何从 JSP 声明 block ? 这不起作用: ... 服务器说没有“out”。 U: 我确实知道如何使用返回字符串的方法重写代码,但是有没有办法在 ?
在一个字段中,我想设置一个具有自定义过滤器的自定义分析器-着眼于词干-因此,“闪存卡”和“闪存卡”的词根相同,因此返回的结果相同 当我运行以下查询时,我的命中率很高,但是“闪存卡”和“闪存卡”各自返回
快速提问。 我有一个通过 PInvoke 使用 native DLL 的应用程序,这个 DLL 可能会调用 PostQuitMessage()。 如何避免? (因为我的应用程序不应该关闭) 我试过 A
一些给定的 HTML 文章,例如: Content 与一些基本的 Jquery 结合使用,例如: $(".some_
我正在构建一个灯箱相册。当第一个图像加载时,CSS 转换起作用。当加载后的每个图像都没有。任何想法为什么?加载第一张之后的照片,但没有过渡。 Image.prototype.load = functi
这个问题在这里已经有了答案: Disable recent tasks button on Android 5.0 (2 个答案) 关闭 2 年前。 我知道这个问题之前在这里被问过 Android
我是 Objective-C 的新手,我只是想弄清楚我是否可以使用 block 或选择器作为 UIAlertView 的 UIAlertViewDelegate 参数 - 哪个更合适? 我已经尝试了以
我是 Linux (UNIX) 套接字下套接字编程的新手。我在 Internet 上找到了以下代码,用于为每个连接生成一个线程的 tcp 服务器。但是它不起作用。accept() 函数立即返回,不等待
recv()库函数手册页提到: It returns the number of bytes received. It normally returns any data available, up
我有一个用于其他项目的共享 ts 库。在这个库中有被同一个库的其他资源使用的资源。该库的结构分为 components/*、interfaces/*、services/* 等目录。在每个目录的根目录中
我想在同一行中一个接一个地显示我的 ListView ,但 ListView 显示每个新行中的每个项目。我怎样才能防止换行显示。以便它显示为段落 ListView.builder( shr
我有一个包含数千行的表格。 import React from "react" import { useSelector } from "react-redux"; import { useEffec
假设我通常希望收到关于代码中不完整模式的警告,但有时我知道某个函数的模式不完整,我知道这很好。 是still true GHC 的警告粒度是每个模块的,并且没有办法更改有关特定功能或定义的警告? 最佳
我的网络应用程序发送浏览器通知,我知道如何检查通知的浏览器权限,以及如果未授予权限,如何请求权限。 但是,即使用户授予我的站点发送通知的权限,她可能仍然无法收到通知,因为它们 might be dis
我有 Xcode 3.2.1,并且喜欢使用它,但是当我编辑文本中带有超链接的文件时(例如,带有引用的注释:# see http://example.com)Xcode 将文本变成可点击的超链接。尝试编
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我有一个在 MY_Controller 中运行的 acl。如果权限被拒绝,那么此刻,我只是执行 redirect('denied') - 这是一个非常基本的 Controller ,它加载一个非常基本
我一直很好奇尝试从 Chrome 切换到 Firefox Quantum,但是对于 Web 开发遇到了一个我无法轻松解决的主要障碍——它正在缓存我的本地主机文件,因此当我尝试在本地主机加载各种 emb
这真的让我很兴奋!在任何时候,我都会参与多个项目。当我退出Xcode时,下次打开Xcode时,我前一天的所有项目都会自动一一打开。 经常我最终编辑错误的文件,AHHHHHHHHHHH!我可以阻止这种行
我的Wiki上有500个左右的Spambot和大约5个实际注册用户。我已经使用nuke删除了他们的页面,但是他们一直在重新发布。我已经使用reCaptcha控制了spambot的注册。现在,我只需要一
我是一名优秀的程序员,十分优秀!