#Memory leaks

44 messages · Page 1 of 1 (latest)

odd nebula
#

hey so I'm working on my final exam for c++ game development, and I have memory leaks😭 I was wondering if anyone had tips/tricks to easily find them back, since I already went over my code a million times (extra info: we're not alloweed to use smart/auto pointers or anything. I do use inheritance and object composition/aggregation. aside from that, I push stuff into vectors as pointers from a pure base (I use polymoprhisme there)). I'm just wondering if perhaps I made a mistake with that. thank for the help!!! also here is part of the output: Detected memory leaks!
Dumping objects ->
{163} normal block at 0x000001ACC6ECA880, 16 bytes long.
Data: < s > A8 89 73 D6 F6 7F 00 00 00 00 00 00 00 00 00 00
{162} normal block at 0x000001ACC6EC9F70, 16 bytes long.
Data: < s > D8 89 73 D6 F6 7F 00 00 00 00 00 00 00 00 00 00
{161} normal block at 0x000001ACC6EC9D40, 16 bytes long.
Data: <` s > 60 89 73 D6 F6 7F 00 00 00 00 00 00 00 00 00 00
Object dump complete. // I only get this after I close my code, so yeahhh :/

wary patioBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

fierce rampart
#

there is no actually, just run one by one modules

#

(assumed you already heard about) if you can, use valgrind, it could help

odd nebula
#

I actually haven't heard of it, THANK YOU SO MUCH!!

naive meadow
#

Post the full code

wary patioBOT
#

@odd nebula Has your question been resolved? If so, type !solved :)

odd nebula
#

I would, but this is a big project, so I have many different classes. idk if it would even fit on here. this is also a specialized engine that the school ahs proveded (with even more code and stuff that I'm not sure I can even legally spread)

#

thanks for the help btw, I'll try using valgrind and hopefully figure it out :)

#

!solved

wary patioBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

naive meadow
#

OK but one last thing: Make sure the base class has a virtual destructor (otherwise there could be object splicing when deleting it which causes memory leaks)

dire mountain
#

not allowed to use smart pointers but can use vectors

fierce rampart
#

just use
static globalStore{
everySingleObjectInProgram
}

hollow kelp
#

if you cant use smart pointers, write your own trolol

rugged tapir
#
struct ControlBlock
{
   atomic<uint64_t> use_count;
};

template<typename T, typename Deleter>
class SharedPtr
{
  pair<T*, Deleter> data;
  ControlBlock* block;
}

and now write rule of 5 for this thing.

#

it's not as brutally efficient as std::shared_ptr<T> but it will get the job done

#
I push stuff into vectors as pointers from a pure base (I use polymoprhisme there)).

So you mean like...

vector<HumanBase*> v = {new Human1(), new Human2(), new Human3()};

//use v

for(HumanBase* h : v)
{
  delete h;
  //virtual ~HumanBase() is required to avoid leaks
}

this sh*t?

#

oh and, when 2 vectors need to share the same set of raw resources, you are kinda screwed, or do you run the helper virutal function clone to riun a heap allocated deep copy?

odd nebula
#

omg imma be really honest, I'm very new to programming, so whatever I learn, I learn from school (so i don't even know what smart pointer are (but I could def do my research)). aside from that, I thinnk not using them is to teach us the importance of manually deleting everything, so by creating my own versions of smart pointers, I am kinda not following the assignment :/ (this sucks but starting next year, we get a more openminded teacher who is going to teach us smart pointers.) and with this "I push stuff into vectors as pointers from a pure base (I use polymoprhisme there" I mean that I create a abstract base class. i derive from that class and push them into vectors as base class pointers to use polymorphisme. that way I can draw them all at the same time in a loop + use type casting to call specific functions

#

wait here is an example: *if (m_pRoomIndex >= 0 && m_pRoomIndex < m_pAllRooms.size())
{
for (int index = 0; index < m_pAllRooms[m_pRoomIndex].size(); ++index)
{
Interactable
pInteractable = m_pAllRooms[m_pRoomIndex][index];
if (pInteractable)
{
pInteractable->CheckCollision(player);

        Key* pKey = dynamic_cast<Key*>(pInteractable);
        if (pKey != nullptr)
        {
            pKey->CheckKeyUsed(player);
        }
    }
}

}**

odd nebula
#

I do do this kinda stuff *** InteractableManager:: InteractableManager(Player* PLayer, LevelManager* Manager, int startroom):
m_pPlayer {PLayer}, m_pManager{Manager}, m_pRoomIndex{startroom}
{
m_pRoom1.reserve(sizeRoom1);
m_pRoom1.push_back(new Key ( Vector2f(426.f, 530.f), Vector2f(395.f, 70.f) ));
m_pRoom1.push_back(new Chest(Vector2f(770.f, 645.f), Vector2f(705.f, 645.f), Vector2f(834.f, 70.f)));
m_pRoom1.push_back(new Checkpoint(Vector2f(24.f, 70.f)));

m_pRoom2.reserve(sizeRoom2);
m_pRoom2.push_back(new Checkpoint(Vector2f(166.f, 175.f)));

m_pRoom3.reserve(sizeRoom3);
m_pRoom3.push_back(new Checkpoint(Vector2f(40.f, 235.f)));
m_pRoom3.push_back(new Key ( Vector2f(0.f, 230.f), Vector2f(737.f, 230.f) ));

m_pRoom4.reserve(sizeRoom4);
m_pRoom4.push_back(new Chest(Vector2f(113.f, 570.f), Vector2f(95, 570), Vector2f(2834.f, 70.f)));

m_pRoom6.reserve(sizeRoom6);
m_pRoom6.push_back(new Checkpoint(Vector2f(550.f, 83.f)));

m_pRoom7.reserve(sizeRoom7);
m_pRoom7.push_back(new Chest(Vector2f(740.f, 30.f), Vector2f(205.f, 645.f), Vector2f(2834.f, 70.f)));//change later for chest + amulet


m_pAllRooms.reserve(AmountRooms);

m_pAllRooms.push_back(m_pDummyRoom);
m_pAllRooms.push_back(m_pRoom1);
m_pAllRooms.push_back(m_pRoom2);
m_pAllRooms.push_back(m_pRoom3);
m_pAllRooms.push_back(m_pRoom4);
m_pAllRooms.push_back(m_pDummyRoom);
m_pAllRooms.push_back(m_pRoom6);
m_pAllRooms.push_back(m_pRoom7);

}

InteractableManager::~InteractableManager()
{

for (int index = 0; index < m_pAllRooms.size(); ++index)
{
    for (int jndex = 0; jndex < m_pAllRooms[index].size(); ++jndex)
    {
        delete m_pAllRooms[index][jndex];
        m_pAllRooms[index][jndex] = nullptr;
    }
    m_pAllRooms[index].clear();
}
m_pAllRooms.clear();

}***

#

(idk how to put code in those squares stuff in discord SORRY)

wary patioBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
rugged tapir
rugged tapir
# odd nebula I don't really understand what you mean here😭 but in my derived classes I over...

by sharing I mean

vector<HumanBase*> v = {new Human1(), new Human2(), new Human3()};

vector<HumanBase*> vCopy = v; // both vectors point to the same elements, so who the hell needs to run operator delete now!? nobody knows. this is bad

vector<HumanBase*> vCopySafe;
for(size_t i = 0 i < v.size(); ++i)
{
  vCopySafe[i] = v[i]->clone(); 
  /*
    The helper function clone is defined as:
    class HumanBase
    {
      virtual HumanBase* clone() const = 0;
      virtual ~HumanBase(){};
    };

    class Human1 : public HumanBase
    {
      HumanBase* clone() const override
      {
        return new Human1(*this); // runs the constructor Human1(const Human1&);
      }
    };
  */
}
rugged tapir
rugged tapir
#

omg imma be really honest, I'm very new to programming, so whatever I learn, I learn from school
big oof
so, if you want to fix the leaks you need to really learn how to control memory in C++ in a vector

rugged tapir
#

(so i don't even know what smart pointer are (but I could def do my research))
that will take 1 month, skip the smart pointers you don't need them here

odd nebula
#

Hey, first of all I want to really thank you for your help so far, butI'm in the middle of my exam period (so I have to study for math, etc). I decided to leave the errors as is, and hopfully find the errors later on (als I legallycan't send all my code, because it uses a schools engine + that enginge is apperently shit so yeah...)

odd nebula
jolly mortar
#

for example using the RTTI (Run-Time Type Information) System to see which type it is

#

and you can overload delete too

#
    void operator delete(void * p, size_t size) // Basically normally the delete operator does not need size but here for debugging purposes you can use it
    {
        // Do some calculation for example check If the created object was freed If not print the type or atleast what it is

        free(p);
    }
odd nebula
#

you guys are all so nice, thank you so much for helping me🌟

jolly mortar
#

np

#

If you need example code to implement I can give you some

odd nebula
#

That's very nice of you! for now I'll just focus on my math exam and look into my programming exam later :3