简体   繁体   English

C ++应用程序在删除时崩溃

[英]C++ application crashes at delete

I've a fairly complex application written in c++. 我有一个用c ++编写的相当复杂的应用程序。 I've a class called OrderBook . 我有一个叫做OrderBook的类。 I need to create an array of OrderBook objects dynamically, so what i've done is, 我需要动态创建一个OrderBook对象数组,所以我要做的是,

OrderBook* pOrderBooks; // In header file

At runtime, I create the array as 在运行时,我将数组创建为

p_OrderBooks = new OrderBook[n]; // n is an integer initialized at run time

The program works fine. 该程序工作正常。 But when I try to delete the array (as I need to create a new array pointed by pOrderBooks ) the program crashes. 但是,当我尝试删除数组(因为我需要创建一个由pOrderBooks指向的新数组)时,程序崩溃了。 This is how i delete it. 这就是我删除它的方式。

delete[] p_OrderBooks;

I've made sure that the crash happen exactly due to that line. 我已经确定崩溃是由于该行而发生的。 So what i'm currently doing is reinitializing the pointer without deleting the previously allocated memory. 因此,我目前正在做的是重新初始化指针,而不删除先前分配的内存。

//delete[] p_OrderBooks; // <- crash happens here
p_OrderBooks = new OrderBook[k]; // for some 'k'

But it's bad since there'll be a memory leak. 但这很糟糕,因为会发生内存泄漏。 I'd like to know how to properly free the memory before re-pointing to the new array. 我想知道在重新指向新数组之前如何正确释放内存。

You are allocating p_OrderBooks but deleting pOrderBooks 您正在分配p_OrderBooks但删除pOrderBooks

If that's just a simple typo in your post, then it is likely that you are overrunning the bounds of this array, writing to elements past the beginning or end, therefore corrupting the heap so it crashes when you try to delete it. 如果这只是帖子中的简单错字,则可能是您超出了此数组的范围,写入了开头或结尾之后的元素,因此损坏了堆,因此在尝试删除它时会崩溃。

Is it possible that one or more of your destructors for OrderBook is throwing an exception out of the destructor? 您的OrderBook的一个或多个析构函数是否有可能从析构函数抛出异常? If so ( typically considered bad ) and if it is not handled will crash your application. 如果是这样( 通常认为是不好的 ),并且如果不进行处理,则会使您的应用程序崩溃。

If you are doing something like this: 如果您正在执行以下操作:

OrderBook* p_OrderBooks; OrderBook * p_OrderBooks;

int n; int n;

p_OrderBooks = new OrderBook[n]; p_OrderBooks =新的OrderBook [n]; // here n contains garbage //这里n包含垃圾

cin>>n CIN >> N

delete[] p_OrderBooks; delete [] p_OrderBooks;

Here n can be any garbage value, we don't know its size ,and perhaps we start accessing memory that we don't own. 这里n可以是任何垃圾值,我们不知道它的大小,也许我们开始访问我们不拥有的内存。 It could be problematic. 这可能是有问题的。 You should take input first 你应该先输入

cin>>n CIN >> N

p_OrderBooks = new OrderBook[n]; p_OrderBooks =新的OrderBook [n];

I found the issue. 我发现了问题。 I'm passing a pointer to an object created in the base class to OrderBook objects. 我将指向基类中创建的对象的指针传递给OrderBook对象。

Server* p_Server = new Server(); // Some class
...
pOrderbook[i]->SetServer(p_Server) // <- for i=[0:99]

Passed p_Server is stored in OrderBook objects as p_ServerBase (say). 传递的p_Server作为p_ServerBase存储在OrderBook对象中(例如)。

Server* p_ServerBase; // <- in OrderBook.h
...
OrderBook::SetServer(Server* pServer)
{
    p_ServerBase = pServer;
}

Then in the OrderBook 's distructor i'm trying to delete that, p_ServerBase , which is still being used in the base class. 然后在OrderBookp_ServerBase ,我试图删除p_ServerBase ,该类仍在基类中使用。

...
~OrderBook()
{
    delete p_ServerBase;
}

Haven't had that experiance before. 以前没有这种经验。 I' won't do that again :) 我不会再这样做了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM