0
。我正在开发一个自定义的虚拟机,并且使用自定义的内存管理类(每个对象都将驻留在虚拟机将要管理的一块大内存中)。删除对象时出现C++自定义内存管理器错误
问题是:当试图删除其中一个对象时,valgrind给我“跳到下一行指出的无效地址”错误,我找不出如何解决它。
有没有人有线索?提前致谢。
的Valgrind的错误:
[Stub] static void CeliVM::MemoryManagedClass::operator delete(pointer) in /home/patrick/Projects/CeliVM/Source/Memory.cxx @ 507
==4812== Jump to the invalid address stated on the next line
==4812== at 0x310040529CBA0040: ???
==4812== by 0x401CA2: main (Main.cxx:89)
==4812== Address 0x310040529cba0040 is not stack'd, malloc'd or (recently) free'd
==4812==
delete运算符:
void MemoryManagedClass::operator delete (pointer objectPointer) noexcept
{
MemoryAllocator* memoryAllocator = static_cast<MemoryManagedClass*>(objectPointer)->memoryAllocator;
memoryAllocator->Free(DATA(objectPointer));
}
内存分配器 “自由” 的方法:
void BasicMemoryAllocator::Free(data dataBlock)
{
// Find the allocated block on the list.
u64 allocatedBlockIndex = Kernel.Any;
for (u64 blockIndex = 0; blockIndex < this->numberOfAllocatedBlocks; blockIndex++)
if (this->allocatedBlocks[ blockIndex ].block == dataBlock)
{
allocatedBlockIndex = blockIndex;
break;
}
if (allocatedBlockIndex == Kernel.Any)
{
ERROR(Txt::CouldNotFindRequestedAllocatedBlock, intpointer(dataBlock));
return;
}
// Check if we have an adjacent free block we can expand or we need to create a new one.
u64 freeBlockIndex = Kernel.Any;
for (u64 blockIndex = 0; blockIndex < this->numberOfFreeBlocks; blockIndex++)
if ((this->freeBlocks[ blockIndex ].start == this->allocatedBlocks[ allocatedBlockIndex ].end + 1) ||
(this->freeBlocks[ blockIndex ].end == this->allocatedBlocks[ allocatedBlockIndex ].start - 1))
{
freeBlockIndex = blockIndex;
break;
}
if (freeBlockIndex == Kernel.Any)
{
// If we do not have more "free blocks" to use, we must expand the free blocks list.
if (this->numberOfFreeBlocks == this->freeBlocksCapacity)
{
BlockInfo* newFreeBlocks = new (std::nothrow) BlockInfo[ this->freeBlocksCapacity + BasicMemoryAllocator::BlocksCapacityIncrement ];
if (!newFreeBlocks)
{
ERROR(Txt::CouldNotExpandFreeBlocksList);
return;
}
memcpy(newFreeBlocks, this->freeBlocks, this->freeBlocksCapacity * sizeof(BlockInfo));
delete [] this->freeBlocks;
this->freeBlocks = newFreeBlocks;
this->freeBlocksCapacity += BasicMemoryAllocator::BlocksCapacityIncrement;
DEBUG(Dbg::FreeBlocksListExpanded, this->freeBlocksCapacity);
}
memcpy(&this->freeBlocks[ this->numberOfFreeBlocks++ ], &this->allocatedBlocks[ allocatedBlockIndex ], sizeof(BlockInfo));
}
else
{
// Join the freed block to the adjacent free block.
this->freeBlocks[ freeBlockIndex ].size += this->allocatedBlocks[ allocatedBlockIndex ].size;
if (this->freeBlocks[ freeBlockIndex ].start == this->allocatedBlocks[ allocatedBlockIndex ].end + 1)
this->freeBlocks[ freeBlockIndex ].start = this->allocatedBlocks[ allocatedBlockIndex ].start;
else
this->freeBlocks[ freeBlockIndex ].end = this->allocatedBlocks[ allocatedBlockIndex ].end;
}
// Remove the allocated block from the allocated blocks list.
u64 blockStart = this->allocatedBlocks[ allocatedBlockIndex ].start;
u64 blockEnd = this->allocatedBlocks[ allocatedBlockIndex ].end;
this->numberOfAllocatedBlocks--;
if (allocatedBlockIndex < this->numberOfAllocatedBlocks)
memcpy(&this->allocatedBlocks[ allocatedBlockIndex ], &this->allocatedBlocks[ this->numberOfAllocatedBlocks ], sizeof(BlockInfo));
DEBUG(Dbg::BlockFreed, blockStart, blockEnd);
}
你的程序表现出不确定的行为,通过访问对象其生命周期结束后的方式。 'operator delete'在析构函数之后运行;现在死对象的成员变量消失了,其中一个名为'memoryAllocator'。 –
有趣的,但这是唯一的MemoryManagedClass后裔行为像这样,其他类工作正常。在我的理解中,由于memoryAllocator在类被销毁时不会被销毁,所以它仍然处于和以前一样的内存位置,并且由于我在管理内存块,所以我应该没有理由无法访问它。但无论如何,谢谢,也许我会使用全局分配器。 – patrickMelo
这个有问题的后代(我们称之为'D')有其他的基类吗?虚拟基类也许?我的猜测是,你遇到了麻烦,因为'MemoryManagedClass'子对象不在D中的偏移量0处,所以'static_cast(objectPointer)'给你一个错误的地址。实际上,你正在执行'reinterpret_cast (static_cast (objectPointer))',它表现出未定义的行为(出于不同的原因),但只要“MemoryManagedClass”刚好位于偏移量0在'D'里面。 –