title: C++ custom memory manager error when deleting objects
body: <p>there.</p>
  
  <p>I'm developing a custom virtual machine, and I'm using custom memory managed classes (every object will reside in one big block of memory that the virtual machine will manage).</p>
  
  <p>The problem is: valgrind gives me "Jump to the invalid address stated on the next line" error when trying to delete one of these objects and I can't figure out how to fix it.</p>
  
  <p>Does anyone have a clue? Thanks in advance.</p>
  
  <p><strong>The valgrind error:</strong></p>
  
  <pre><code>[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==
  </code></pre>
  
  <p><strong>The delete operator:</strong></p>
  
  <pre><code>void MemoryManagedClass::operator delete ( pointer objectPointer ) noexcept
  {
      MemoryAllocator* memoryAllocator = static_cast&lt;MemoryManagedClass*&gt;( objectPointer )-&gt;memoryAllocator;
      memoryAllocator-&gt;Free( DATA( objectPointer ) );
  }
  </code></pre>
  
  <p><strong>The memory allocator "Free" method:</strong></p>
  
  <pre><code>void BasicMemoryAllocator::Free( data dataBlock )
  {
      // Find the allocated block on the list.
  
      u64 allocatedBlockIndex = Kernel.Any;
  
      for ( u64 blockIndex = 0; blockIndex &lt; this-&gt;numberOfAllocatedBlocks; blockIndex++ )
          if ( this-&gt;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 &lt; this-&gt;numberOfFreeBlocks; blockIndex++ )
          if ( ( this-&gt;freeBlocks[ blockIndex ].start == this-&gt;allocatedBlocks[ allocatedBlockIndex ].end + 1 ) ||
               ( this-&gt;freeBlocks[ blockIndex ].end == this-&gt;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-&gt;numberOfFreeBlocks == this-&gt;freeBlocksCapacity )
          {
              BlockInfo* newFreeBlocks = new (std::nothrow) BlockInfo[ this-&gt;freeBlocksCapacity + BasicMemoryAllocator::BlocksCapacityIncrement ];
  
              if ( !newFreeBlocks )
              {
                  ERROR( Txt::CouldNotExpandFreeBlocksList );
                  return;
              }
  
              memcpy( newFreeBlocks, this-&gt;freeBlocks, this-&gt;freeBlocksCapacity * sizeof( BlockInfo ) );
  
              delete [] this-&gt;freeBlocks;
              this-&gt;freeBlocks = newFreeBlocks;
              this-&gt;freeBlocksCapacity += BasicMemoryAllocator::BlocksCapacityIncrement;
  
              DEBUG( Dbg::FreeBlocksListExpanded, this-&gt;freeBlocksCapacity );
          }
  
          memcpy( &amp;this-&gt;freeBlocks[ this-&gt;numberOfFreeBlocks++ ], &amp;this-&gt;allocatedBlocks[ allocatedBlockIndex ], sizeof( BlockInfo ) );
      }
      else
          {
              // Join the freed block to the adjacent free block.
  
              this-&gt;freeBlocks[ freeBlockIndex ].size += this-&gt;allocatedBlocks[ allocatedBlockIndex ].size;
  
              if ( this-&gt;freeBlocks[ freeBlockIndex ].start == this-&gt;allocatedBlocks[ allocatedBlockIndex ].end + 1 )
                  this-&gt;freeBlocks[ freeBlockIndex ].start = this-&gt;allocatedBlocks[ allocatedBlockIndex ].start;
              else
                  this-&gt;freeBlocks[ freeBlockIndex ].end = this-&gt;allocatedBlocks[ allocatedBlockIndex ].end;
          }
  
  
      // Remove the allocated block from the allocated blocks list.
  
      u64 blockStart = this-&gt;allocatedBlocks[ allocatedBlockIndex ].start;
      u64 blockEnd = this-&gt;allocatedBlocks[ allocatedBlockIndex ].end;
  
      this-&gt;numberOfAllocatedBlocks--;
  
      if ( allocatedBlockIndex &lt; this-&gt;numberOfAllocatedBlocks )
          memcpy( &amp;this-&gt;allocatedBlocks[ allocatedBlockIndex ], &amp;this-&gt;allocatedBlocks[ this-&gt;numberOfAllocatedBlocks ], sizeof( BlockInfo ) );
  
      DEBUG( Dbg::BlockFreed, blockStart, blockEnd );
  }
  </code></pre>