delete关键字引发程序崩溃:类堆内存释放时出现Heap Corruption错误
Hey there, that heap corruption error is usually a sign your code is messing with memory it shouldn’t be—let’s break down the most likely culprits based on your scenario:
Common Causes & Fixes
1. You’re Writing Past the End of Your Dynamic Array
Looking at your code, you initialized the Butterflyfish with a capacity of 3, but called remember() 6 times! If your remember() method doesn’t handle resizing the array when it’s full, you’re writing to memory outside the bounds of the allocated array. This corrupts the heap’s internal structure, so when you try to delete the object later, the runtime freaks out because the heap is broken.
Fix: Update your remember() method to check if the array is full, and resize it (usually by doubling capacity) when needed. Here’s a quick example:
void Butterflyfish::remember(char c) { if (currentSize >= capacity) { // Double capacity to avoid frequent resizes int newCapacity = capacity * 2; char* tempArr = new char[newCapacity]; // Copy existing elements to the new array for (int i = 0; i < currentSize; ++i) { tempArr[i] = memoryArr[i]; } // Clean up old memory and switch to the new array delete[] memoryArr; memoryArr = tempArr; capacity = newCapacity; } memoryArr[currentSize++] = c; }
2. Mismatched new[] and delete (or Vice Versa)
If your Butterflyfish constructor uses new[] to allocate the array (which it should for a dynamic array), your destructor must use delete[] to free it. Using plain delete on an array allocated with new[] causes undefined behavior—including heap corruption.
Check your destructor: It should look like this:
Butterflyfish::~Butterflyfish() { delete[] memoryArr; // Not delete memoryArr! memoryArr = nullptr; // Optional, but prevents dangling pointers }
3. Missing Copy Constructor & Assignment Operator (Shallow Copy Bug)
If anywhere in your code you’re copying Butterflyfish objects (like passing them by value, returning them from a function, or assigning one to another) without implementing custom copy methods, you’ll get a shallow copy. This means two objects point to the same dynamic array. When one is destroyed, it frees the array, leaving the other object with a dangling pointer. When that second object is destroyed (or you try to use the array), it corrupts the heap.
Fix: Implement deep copy versions of these methods:
// Copy Constructor Butterflyfish::Butterflyfish(const Butterflyfish& other) { capacity = other.capacity; currentSize = other.currentSize; memoryArr = new char[capacity]; for (int i = 0; i < currentSize; ++i) { memoryArr[i] = other.memoryArr[i]; } } // Assignment Operator Butterflyfish& Butterflyfish::operator=(const Butterflyfish& other) { if (this != &other) { // Avoid self-assignment // Free existing memory first delete[] memoryArr; // Copy values from the other object capacity = other.capacity; currentSize = other.currentSize; memoryArr = new char[capacity]; for (int i = 0; i < currentSize; ++i) { memoryArr[i] = other.memoryArr[i]; } } return *this; }
How to Debug Further
If you’re still stuck, use a memory debugging tool:
- On Windows, enable Visual Studio’s AddressSanitizer in project settings to catch exactly where the out-of-bounds write happens.
- On Linux/macOS, use Valgrind—it will point you to the exact line corrupting the heap.
内容的提问来源于stack exchange,提问作者MaxAttax

