在析构函数中删除指针时崩溃

发布于 2025-01-08 07:15:40 字数 1051 浏览 1 评论 0原文

我遇到了对 C++ 中的指针缺乏深入理解的问题。我编写了一个名为 Skymap 的类,它具有以下定义:

class Skymap {
 public:
  Skymap();
  ~Skymap();
  void DrawAitoffSkymap();
 private:
  TCanvas mCanvas;

  TBox* mSkymapBorderBox;
};

函数定义为:(

#include "Skymap.h"

Skymap::Skymap()
{
  mCanvas.SetCanvasSize(1200,800);
  mMarkerType=1;
}

Skymap::~Skymap()
{
  delete mSkymapBorderBox;
}

void Skymap::DrawAitoffSkymap()
{
  TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);
  //Use the mSkymapBorderBox pointer for a while
}

我正在使用 ROOT 绘图包,但我认为这只是一个一般的 C++ 问题)。

现在,以下程序将在到达 skymap2 的析构函数时崩溃:

int main(){
  Skymap skymap1;
  Skymap skymap2;
  skymap1.DrawAitoffSkymap();
  skymap2.DrawAitoffSkymap();
  return(0);
}

但是,以下程序不会崩溃:

int main(){
  Skymap skymap1;
  skymap1.DrawAitoffSkymap();
  return(0);
}

另外,如果我在构造函数中将指针 mSkymapBorderBox 初始化为 NULL,则在执行第一个程序后我不再遇到崩溃(有 2 个 Skymap 对象)。

谁能解释一下造成这种情况的根本原因是什么?这似乎是第二个 Skymap 对象中的指针有问题,但我不明白它是什么。

I've bumped up against my lack of deep understanding of pointers in C++. I've written a class called Skymap, which has the following definition:

class Skymap {
 public:
  Skymap();
  ~Skymap();
  void DrawAitoffSkymap();
 private:
  TCanvas mCanvas;

  TBox* mSkymapBorderBox;
};

with the functions defined as:

#include "Skymap.h"

Skymap::Skymap()
{
  mCanvas.SetCanvasSize(1200,800);
  mMarkerType=1;
}

Skymap::~Skymap()
{
  delete mSkymapBorderBox;
}

void Skymap::DrawAitoffSkymap()
{
  TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);
  //Use the mSkymapBorderBox pointer for a while
}

(I am using the ROOT plotting package, but I think this is just a general C++ question).

Now, the following program will crash upon reaching the destructor of skymap2:

int main(){
  Skymap skymap1;
  Skymap skymap2;
  skymap1.DrawAitoffSkymap();
  skymap2.DrawAitoffSkymap();
  return(0);
}

However, the following will not crash:

int main(){
  Skymap skymap1;
  skymap1.DrawAitoffSkymap();
  return(0);
}

Also, if I initialise the pointer mSkymapBorderBox to NULL in the constructor, I no longer experience a crash following the execution of the first program (with 2 Skymap objects).

Can anyone please explain what the underlying cause of this is? It appears to be a problem with the pointer in the second Skymap object, but I do not see what it is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愚人国度 2025-01-15 07:15:40
TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);

在这里,您将内存分配给局部变量,而不是成员变量。由于您没有为成员变量分配内存,因此对其调用 delete 会调用未定义的行为,从而导致您的情况崩溃。

您应该做的是:

mSkymapBorderBox=new TBox(-200,-100,200,100);

现在为成员变量分配内存。这是局部变量的命名应与成员变量不同的原因之一。命名约定有助于避免此类错误。

作为旁注,或者更确切地说是非常重要的说明,由于您的类管理资源,请考虑正确实现复制语义和析构函数:此规则通常称为 三规则。或者使用一些智能指针,例如 std::shared_ptr、std::unique_ptr 或任何适合您场景的指针。

TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);

Here you're allocating memory to a local variable, not to the member variable. And since you're not allocating memory for the member variable, calling delete on it would invoke undefined behavior, which results in crash in your case.

What you should be doing is this:

mSkymapBorderBox=new TBox(-200,-100,200,100);

which now allocates memory for the member variable. It is one of the reason why local variables should be named differently than member variables. Naming conventions helps avoiding such bugs.

As a sidenote, or rather a very important note, since your class manages resources, consider implementing copy-semantics along with destructor properly: this rule is popularly referred to as the-rule-of-three. Or use some smart pointers, such asstd::shared_ptr, std::unique_ptr or whatever fits in your scenario.

未央 2025-01-15 07:15:40

纳瓦兹的回答是正确的。但除此之外,您的代码还有几个可能的问题:

  1. 如果有人创建了 SkyMap 并且从未使用它调用 DrawAitoffSkymap,那么您将得到未定义的行为(因为 mSkymapBorderBox 从未初始化,它将有一个随机值,然后您将其删除)。
  2. 如果有人使用给定的 SkyMap 多次调用 DrawAitoffSkymap ,那么您将遇到内存泄漏。

修复方法:

(1) 在构造函数中将 mSkymapBorderBox 初始化为零。

(2) 决定当DrawAitoffSkymap被多次调用时应该做什么。如果它应该重用旧的 mSkymapBorderBox,那么你会想说这样的话:

void Skymap::DrawAitoffSkymap() {
   if (!mSkymapBorderBox) mSkymapBorderBox = new TBox(...);
   ...
}

另一方面,如果每次都应该创建一个新的 TBox,那么你想要:

void Skymap::DrawAitoffSkymap() {
   delete mSkymapBorderBox; // note: does nothing if mSkymapBorderBox == 0
   mSkymapBorderBox = new TBox(...);
   ...
}

Nawaz's answer is correct. But beyond that, your code has several possible problems:

  1. If anyone creates a SkyMap and never calls DrawAitoffSkymap with it, then you'll get undefined behavior (since mSkymapBorderBox is never initialized, it will have a random value, which you then delete).
  2. If anyone calls DrawAitoffSkymap more than once with a given SkyMap, then you'll get a memory leak.

To fix:

(1) Initialize mSkymapBorderBox to zero in your constructor.

(2) Decide what DrawAitoffSkymap should do if it's called multiple times. If it should reuse the old mSkymapBorderBox, then you would want to say something like:

void Skymap::DrawAitoffSkymap() {
   if (!mSkymapBorderBox) mSkymapBorderBox = new TBox(...);
   ...
}

On the other hand, if a new TBox should be created each time, then you want:

void Skymap::DrawAitoffSkymap() {
   delete mSkymapBorderBox; // note: does nothing if mSkymapBorderBox == 0
   mSkymapBorderBox = new TBox(...);
   ...
}
夜夜流光相皎洁 2025-01-15 07:15:40

TBox* mSkymapBorderBox=new TBox(-200,-100,200,100); 您正在创建一个新的 TBox* 指针,它不是数据成员。

考虑在同一逻辑单元/范围中实现new后正确实现delete...

TBox* mSkymapBorderBox=new TBox(-200,-100,200,100); you are creating a new TBox* pointer, which is not the data member.

Consider to properly implement the delete after you implement a new, in the same logical unit/scope...

画▽骨i 2025-01-15 07:15:40
TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);

当你声明它时,这将创建一个 TBox 类的对象。当你退出时
DrawAitoffSkymap 此时就失去了该分配内存的引用。

当析构函数被调用时,它会释放一些垃圾内存。

为了避免这种情况,请使用这个
mSkymapBorderBox=new TBox(-200,-100,200,100);
而不是 TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);

TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);

when you declared this this create an object of the class TBox. When you exit
DrawAitoffSkymap at that time this lose the reference of this allocated memory.

When the destructor is called at time it frees some garbage memory.

To avoid this use this
mSkymapBorderBox=new TBox(-200,-100,200,100);
instead of TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文