Multimap of pointers as a class member on the heap

89 Views Asked by At

[Edit: I got some friends to look at this, and I'm fairly certain that how I handle some pointers within my member functions are the cause of this. The problem is that all the new/heap/etc. tutorials and documentation I find online deal with super basic new and heap declarations within a 5-line main(), so I'm not sure how to address this.]


Okay, so originally the

std::multimap<Player*, Handle*>* heapTest;

was simply just a multimap class member, named such:

std::multimap<Player*, Handle*> playerHandleMap;

And here's a class function definition:

void DamageBox::OnEnter(Player& Player)  {
  Handle iterHolder = //Function that generates and returns a handle
  auto iteratorPlaceholder = playerHandleMap.emplace(&Player, &iterHolder);
}

Now if I now understand this correctly, this is yielding the memory garbaging, because iterHolder is declared within a class method, and so I smash that address within my playerHandleMap, and as soon as I leave the scope of the class instance, it becomes garbage.

What's the proper syntax here?

Is it, within the class method:

void DamageBox::OnEnter(Player& Player)  {
   Handle* iterHolder = new Handle(\\function that creates a handle)
      auto iteratorPlaceholder = playerHandleMap.emplace(&Player, iterHolder);
    }

But then where do my delete calls? Would performing them as I do my playerHandleMap maintenance be sufficient, e.g. when i .erase a key-value pair, I first call delete on the Handle value?


#pragma once

#include "DetectionVolume.h"
#include "TimedCallback.h"
#include "Player.h"
#include "Clock.h"
#include <vector>

// Describes the information needed to create an FDamageVolume
struct DamageBoxSetup : public DetectionBoxSetup
{
public:
    DamageBoxSetup(const lotsOfArgsHere);

    const float DamagePerSec;
    const Clock::duration DamageInterval;
};

class DamageBox : public DetectionBox
{
public:
    DamageBox();
    DamageBox(const DamageBoxSetup& Setup);
    FDamageBox();

    // This shared instance is used in another method I'm not including here
    static DamageBox& shared_instance() {
        static DamageBox theInstance;
        return theInstance;
    }

protected:

    float DamagePerSec;
    FClock::duration DamageInterval;
    double DamageDonePerInterval;

    std::multimap<Player*, Handle*>* heapTest;


    void QueueDamage(Player& Player, float damageQueued, Clock::duration& theInterval);

    virtual void OnEnter(Player& Player) override;
    virtual void OnExit(Player& Player) override;
};

So, I've been banging my head against the wall, because what was formerly heapTest here was just a regular multimap, not a pointer to one to be declared on the stack. And in hours of debugging and tracking the values within, when the line stepper would leave the class methods, and then come back, a lot of the Handle pointers, the values in those pairs, had turned to garbage in the meantime.

  1. Am I right in presuming that this is most likely a stack vs. heap problem and that I need to get this class member allocated onto the heap?

Naturally, I changed the multimap to a multimap*, added to my constructor the bottom line here

DamageBox::DamageBox(const DamageBoxSetup& Setup)
    : DetectionBox(Setup)
    , DamagePerSec(Setup.DamagePerSec)
    , DamageInterval(Setup.DamageInterval)
{
    auto timeInMs = std::chrono::duration_cast<std::chrono::milliseconds>(DamageInterval).count();
    double IntervalTimeInS = static_cast<double>(timeInMs) / 1000.0;
    DamageDonePerInterval = DamagePerSec * IntervalTimeInS;

    heapTest = new std::multimap<FPlayer*, FTimedCallbackHandle*>;
}

and added a destructor:

DamageVolume::~DamageVolume() {
    delete heapTest;
}

, but after doing these two steps of what I thought was rigor, now I get exceptions/errors thrown when trying to do anything touching the heapTest multimap.

For example:

void DamageBox::OnEnter(Player& Player) {
  ///  Some stuff here
  Handle iterHolder = getTheHandle stuff;

  auto placeholder = heapTest->emplace(&Player,&iterHolder);
}

I get a freakout crash when it hits that emplace there.

In xtree: Exception thrown: read access violation. _Scary->**_Myhead** was nullptr. line 1572

I'm not sure if it matters, but the instances of DamageBox used in the main() loop aren't on the heap - heapTest is the only thing I've tried declaring on the heap.

What am I doing wrong here?

0

There are 0 best solutions below