2016-11-15 62 views
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); 
} 
+0

你的程序表现出不确定的行为,通过访问对象其生命周期结束后的方式。 'operator delete'在析构函数之后运行;现在死对象的成员变量消失了,其中一个名为'memoryAllocator'。 –

+0

有趣的,但这是唯一的MemoryManagedClass后裔行为像这样,其他类工作正常。在我的理解中,由于memoryAllocator在类被销毁时不会被销毁,所以它仍然处于和以前一样的内存位置,并且由于我在管理内存块,所以我应该没有理由无法访问它。但无论如何,谢谢,也许我会使用全局分配器。 – patrickMelo

+0

这个有问题的后代(我们称之为'D')有其他的基类吗?虚拟基类也许?我的猜测是,你遇到了麻烦,因为'MemoryManagedClass'子对象不在D中的偏移量0处,所以'static_cast (objectPointer)'给你一个错误的地址。实际上,你正在执行'reinterpret_cast (static_cast (objectPointer))',它表现出未定义的行为(出于不同的原因),但只要“MemoryManagedClass”刚好位于偏移量0在'D'里面。 –

回答

0

编辑:

有问题的类有一个虚拟的析构函数,但基类没有。向基类添加虚拟析构函数似乎可以解决这个问题。


好了,对象之前分配额外的Int64,节省了内存分配地址似乎有要解决的问题:删除对象用于分配对象的内存分配的地址时,可以让它在被发现记忆。

新:

pointer MemoryManagedClass::operator new (size_t dataSize, MemoryAllocator* memoryAllocator) noexcept 
{ 
    pointer objectAddress = memoryAllocator->Get(dataSize + sizeof(i64)); 

    if (!objectAddress) 
     return NULL; 

    static_cast<MemoryManagedClass*>(objectAddress)->memoryAllocator = memoryAllocator; 

    i64 allocatorAddress = intpointer(memoryAllocator); 
    memcpy(objectAddress, &allocatorAddress, sizeof(i64)); 

    return DATA(objectAddress) + sizeof(i64); 
} 

删除:

void MemoryManagedClass::operator delete (pointer objectPointer) noexcept 
{ 
    i64 allocatorAddress = *reinterpret_cast<i64*>(DATA(objectPointer) - sizeof(i64)); 
    MemoryAllocator* memoryAllocator = reinterpret_cast<MemoryAllocator*>(allocatorAddress); 

    memoryAllocator->Free(DATA(objectPointer) - sizeof(i64)); 
}