[英]Generic Memory allocation crash for overloding new and delete in C++
I am having following code. 我有以下代码。 Following code is crashing when I am deleting the allocated memory ie, Rational::deleteMemPool();
当我删除分配的内存时,以下代码崩溃:Rational :: deleteMemPool();
Getting output as 获取输出为
mempool value is 00000000 mempool value is 003462E8 mempool value is 003462E8
内存池值为00000000内存池值为003462E8内存池值为003462E8
// Here for delete crashing though we have valid pointer. //尽管我们有有效的指针,但此处用于删除崩溃。
Can any one please help me what is bug here? 谁能帮我这是什么bug?
#include <string>
#include <iostream>
template < class T >
class MemoryPool {
public:
MemoryPool (size_t size = EXPANSION_SIZE);
~MemoryPool ();
inline void* alloc (size_t size); // Allocate a T element from the free list.
inline void free (void *someElement); // Return a T element to the free list.
private:
MemoryPool<T> *next; // next element on the free list.
enum { EXPANSION_SIZE = 32}; // If the freeList is empty, expand it by this amount.
void expandTheFreeList(int howMany = EXPANSION_SIZE); // Add free elements to the free list
};
template < class T > MemoryPool < T > :: MemoryPool (size_t size) {
expandTheFreeList(size);
}
template < class T > MemoryPool < T > :: ~MemoryPool () {
MemoryPool<T> *nextPtr = next;
for (nextPtr = next; nextPtr != NULL; nextPtr = next) {
next = next->next;
delete [] nextPtr;
}
}
template < class T > inline void* MemoryPool < T > :: alloc (size_t) {
if (!next) {
expandTheFreeList();
}
MemoryPool<T> *head = next;
next = head->next;
return head;
}
template < class T > inline void MemoryPool < T > :: free (void *doomed) {
MemoryPool<T> *head = static_cast <MemoryPool<T> *> (doomed);
head->next = next;
next = head;
}
template < class T > void MemoryPool < T > :: expandTheFreeList(int howMany) {
// We must allocate an object large enough to contain the next pointer.
size_t size = (sizeof(T) > sizeof(MemoryPool<T> *)) ? sizeof(T) : sizeof(MemoryPool<T> *);
void *pNewAlloc = new char[size];
MemoryPool<T> *runner = static_cast <MemoryPool<T> *> (pNewAlloc);
next = runner;
for (int i = 0; i < howMany ; i++) {
void *pNewAlloc = new char[size];
runner->next = static_cast <MemoryPool<T> *> (pNewAlloc);
runner = runner->next;
}
runner->next = 0;
}
class Rational {
public:
Rational (int a = 0, int b = 1 ) : n(a), d(b) {}
void *operator new(size_t size) { return memPool->alloc(size); }
void operator delete(void *doomed,size_t size) { memPool->free(doomed); }
static void newMemPool() {
std::cout << "mempool value is " << Rational::memPool << std::endl;
memPool = new MemoryPool <Rational>;
std::cout << "mempool value is " << Rational::memPool << std::endl;
}
static void deleteMemPool() {
std::cout << "mempool value is " << Rational::memPool << std::endl;
delete memPool;
}
private:
int n; // Numerator
int d; // Denominator
static MemoryPool <Rational> *memPool;
};
MemoryPool <Rational> *Rational::memPool = 0;
int main() {
Rational *array[1000];
Rational::newMemPool();
// Start timing here
for (int j = 0; j < 1; j++) {
for (int i = 0; i < 10; i++) {
array[i] = new Rational(i);
}
for (int i = 0; i < 10; i++) {
delete array[i];
}
}
// Stop timing here
Rational::deleteMemPool();
}
Stacktrace: 堆栈跟踪:
Stack trace:>
ReadParsing.exe!Rational::deleteMemPool() Line 75 C++
ReadParsing.exe!main() Line 107 C++
ReadParsing.exe!__tmainCRTStartup() Line 586 + 0x19 bytes C
ReadParsing.exe!mainCRTStartup() Line 403 C
kernel32.dll!7c817077()
You have a problem with how you're deleting the blocks in your memory pool. 您如何删除内存池中的块有问题。 When you allocate blocks, you use
new char[size]
in expandTheFreeList(i)
. 分配块时,可以在
expandTheFreeList(i)
使用new char[size]
。
But when you delete those blocks, you use delete [] nextPtr
: 但是,当删除这些块时,可以使用
delete [] nextPtr
:
template < class T > MemoryPool < T > :: ~MemoryPool () {
MemoryPool<T> *nextPtr = next;
for (nextPtr = next; nextPtr != NULL; nextPtr = next) {
next = next->next;
delete [] nextPtr; // <-- problem
}
}
And since nextPtr
is a memoryPool<T>*
, you're freeing the wrong thing (and it ends up recursing into this destrcutor to boot). 而且由于
nextPtr
是memoryPool<T>*
,所以您释放了错误的内容(它最终递归到该析构函数中进行引导)。
Changing that problem line to: 将该问题行更改为:
delete [] reinterpret_cast<char*>(nextPtr);
seems to make thing not crash. 似乎使事情不会崩溃。 Of course, there may be other problems.
当然,可能还有其他问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.