Calling delete causes C++ program to freeze - c++

void FactorySystem::deleteObjectsToBeDeleted()
{
//Delete all objects in the ObjectsToBeDeleted list
std::vector<unsigned>::iterator it = objectsToBeDeleted.begin();
for(unsigned i = 0; i < objectsToBeDeleted.size(); i++)
{
GameObjectIDMapType::iterator it = gameObjectIDMap.find(objectsToBeDeleted[i]);
if(it == gameObjectIDMap.end())
std::cout << "Bad memory or double deletion error!" << std::endl;
else
{
//Delete it and remove its entry in the ID map
std::cout << (it->second->GetID()) << std::endl;
GameObject *object = it->second;
delete object;
gameObjectIDMap.erase(it);
}
}
//All objects to be delete have been deleted
objectsToBeDeleted.clear();
}
At the line where I call delete object, the program just hangs forever, and I can not figure out why. I have googled this problem and it's like nobody has ever encountered this problem ever. I must be doing something bad, but I have no idea what it could be. Any ideas? Thanks.
EDIT:
I was asked to show the destructor for GameObject, here it is:
GameObject::~GameObject()
{
//Delete each component using the component's virtual destructor
//takes care of all resources and memory.
for( ComponentMapIt it = componentMap.begin();it!=componentMap.end();++it)
delete it->second;
}
One other thing to not, I went into the disassembly and was able to step up until:
00488DA9 call GameObject::`scalar deleting destructor' (044055Fh)
and then it just hangs, nothing else happens.
EDIT: Here was my rookie mistake. For some reason I couldn't step into the delete call so I assumed it was in there, but putting a break point in it allowed me to go into it. Thanks everyone, all your suggestions were very helpful.
void PhysicsManager::Unregister(RigidBody *Obj)
{
std::list<RigidBody*>::iterator it = MasterList.begin();
while(it != MasterList.end())
{
if(*it == Obj)
{
MasterList.erase(it);
return;
}
}
}

void PhysicsManager::Unregister(RigidBody *Obj)
{
std::list<RigidBody*>::iterator it = MasterList.begin();
while(it != MasterList.end())
{
if(*it == Obj)
{
MasterList.erase(it);
return;
}
}
}
it is constant and never changes: once the loop start and *it != Obj the loop continues forever

Related

How to delete a node pointer

This is schoolwork. I haven't seen anything that really answers this directly, so I'm having a hard time fixing it. I have to create a linked node implementation of a max heap and I'm having difficulty with the deletion of a node after removing a value.
My Code:
template<class ItemType>
BinaryHeapNode<ItemType>* LinkedMaxHeap<ItemType>::getLastNode()
{
BinaryHeapNode<ItemType>* lastNode = rootPtr->getRightSiblingPtr();
BinaryHeapNode<ItemType>* prevLastNode = rootPtr;
while(lastNode != nullptr)
{
prevLastNode = lastNode;
lastNode = lastNode->getRightSiblingPtr();
}
return prevLastNode;
}
template<class ItemType>
bool LinkedMaxHeap<ItemType>::removeValue(ItemType value)
{
BinaryHeapNode<ItemType>* tempNode = rootPtr;
for (int i = 0; i < itemCount; i++)
{
if(tempNode->getItem() == value)
{
tempNode->setItem(getLastNode()->getItem());//set item
delete getLastNode(); //delete last node
getLastNode() = nullptr; //set last node null
getLastNode()->setRightSiblingPtr(nullptr); //last node should be different
itemCount--; //set it's sibling to null
heapRebuild(tempNode);
}
tempNode = tempNode->getRightSiblingPtr();
}
return true;
}
My issue is with getLastNode() = nullptr. VS is telling me that getLastNode() isn't an lvalue. That doesn't make sense to me because getLastNode is returning a pointer to a BinaryHeapNode, but it can't set that pointer to nullptr?
I thought this might be a problem with my logic of pointers (which is shaky at best) so I thought changing getLastNode() to return just a node would help. That did not. So I tried messing with the & operator and returning an address of the last node. Needless to say I haven't found the solution yet. If anyone can provide some sort of direction it would be appreciated. I'm just not entirely sure why it doesn't work.
EDIT:
Edited the code based on what arynaq mentioned. The errors went away, but now I have a bunch of linker errors I have to fix before I can test it. Will this code do what I want? I feel like it is just going to delete nodeToDelete and not get rid of the node in the heap.
template<class ItemType>
bool LinkedMaxHeap<ItemType>::removeValue(ItemType value)
{
BinaryHeapNode<ItemType>* tempNode = rootPtr;
BinaryHeapNode<ItemType>* nodeToDelete = getLastNode();
for (int i = 0; i < itemCount; i++)
{
if(tempNode->getItem() == value)
{
tempNode->setItem(nodeToDelete->getItem());
delete &nodeToDelete;
nodeToDelete = nullptr;
getLastNode()->setRightSiblingPtr(nullptr);
itemCount--;
heapRebuild(tempNode);
}
tempNode = tempNode->getRightSiblingPtr();
}
return true;
}
Ok, I'll try to help by explaining some things about pointers. Hopefully this will clarify some misconceptions and help you with your assignment.
When you get a copy of the pointer like so: mypointer* p = get_pointer(); and then you delete that, you are deleting the memory. But when you assign nullptr to this local variable, it wont affect the "source" of your pointer.
Here is a detailed example, showing where things can go wrong. If you never set v[0] to nullptr.
#include <iostream>
#include <vector>
struct Object {
~Object() {
std::cout << "Object destructor." << std::endl;
}
int val = 42;
};
struct OtherObj {
int val = 322;
};
void print_vec(const std::vector<Object*>& v) {
for (const auto& x : v) {
std::cout << x << std::endl;
}
}
int main(int, char**) {
// Init vector and print addresses.
std::vector<Object*> v(2);
print_vec(v);
// Init objects in vector and printit.
for (auto& x : v) {
x = new Object();
}
print_vec(v);
// Get a copy of a pointer and delete that. All good so far.
Object* pointer_to_delete = v[0];
delete pointer_to_delete;
// Assign nullptr to the temporary local pointer.
// Does nothing to the pointer in the vector.
pointer_to_delete = nullptr;
// Print the vector to prove it.
print_vec(v);
// On a non debug build, the memory will still have the last value.
// Careful! Cause of headaches here. This should be set to nullptr.
std::cout << v[0]->val << std::endl; // "No problem", certainly not nullptr.
// Now that we allocate a new object, v[0] will be overwritten.
OtherObj* bad_bad_boy = new OtherObj();
// Print the address of the new object, to show it was created at
// the old v[0] address.
std::cout << bad_bad_boy << std::endl;
// Bad things ensue...
std::cout << v[0]->val << std::endl;
return 0;
}
The output on clang is :
0x0
0x0
0x7ffa21c026c0
0x7ffa21c026d0
Object destructor.
0x7ffa21c026c0
0x7ffa21c026d0
42
0x7ffa21c026c0
322
As you can see, setting the local pointer to nullptr is not enough! I hope this clears up some things for you :)
Online version

Remove specific object from vector list

I'm new to C++, sorry if it's a dummy question.
I'm trying to create a method that would remove a given pointer from a vector, and delete its object. Here's what I have right now:
void Engine::destroyObject(GameObject* obj) {
if (obj == nullptr) {
std::cout << "Error: GameObject pointer given is null!" << std::endl;
return;
}
else if (m_GameObjects.empty()) {
std::cout << "Error: Trying to destroy GameObject while list is empty!" << std::endl;
return;
}
auto it = std::remove(m_GameObjects.begin(), m_GameObjects.end(), obj);
if (it != m_GameObjects.end()) {
delete obj;
m_GameObjects.erase(it, m_GameObjects.end());
}
}
GameObject* Engine::findObject(std::string name) {
return *std::find_if(m_GameObjects.begin(), m_GameObjects.end(), [name](GameObject* e) { return e->getName() == name; });
}
I'm calling those methods like this:
GameObject* obj = findObject("Random");
destroyObject(obj);
But for some reason, my program crashes at m_GameObjects.erase(it, m_GameObjects.end()); with the error "vector iterator not dereferencable"
I have tried changing the line to it = m_GameObjects.erase(it, m_GameObjects.end()); but that did not change anything.
Thanks in advance!
Edit: Here's a bit more info
GameObject is just a regular class (with some data such as a name...)
The findProject method works fine, it's the line for erase that's causing an error.
So aschepler found out that my findObject function was not checking for null values (if the object didn't exist/couldn't be found) so I added some checks and it works now. Thanks a lot!
GameObject* Engine::findObject(std::string name) {
auto it = std::find_if(m_GameObjects.begin(), m_GameObjects.end(), [name](GameObject* e) { return e->getName() == name; });
if (it == m_GameObjects.end())
return nullptr;
return *it;
}
I then had to check if the value was a nullptr and not do anything if so.

How to delete elements from two lists upon contact?

I'm having an incredibly difficult time programming Space Invaders for a class. I am supposed to use a list to store my "missiles" and a list to store my "aliens" as well. I press space bar to load in a new alien, and when the two contact, I am attempting to delete both of them, and I can't even get past deleting the "alien". I get a variety of bugs when more than one missile is fired, and the missiles work fine if this function is commented out. I am forced to pass a list of missiles into another class that already contains a list of aliens and delete both.
Here is the offending code:
bool AlienMgr::checkHit(PlayerMgr& playerMgr)
{
bool hit = false; // If the player hits an alien, switches to true and is returned.
list<Missile*>::iterator missileIter;
list<Missile*> missileList = playerMgr.getMissiles();
int missileCount;
FloatRect missileBounds;
FloatRect alienBounds;
iter = myAliens.begin();
while (!myAliens.empty() && iter != myAliens.end())
{
alienBounds = (*iter)->getGlobalBounds();
if (!missileList.empty() && !hit)
{
for (missileIter = missileList.begin(); missileIter != missileList.end() && !hit; missileIter++)
{
missileBounds = (*missileIter)->getMissileBounds();
if (alienBounds.intersects(missileBounds))
{
delete (*iter);
iter = myAliens.erase(iter);
cout << "HIT" << endl;
}
else
{
iter++;
}
}
}
else
{
iter++;
}
}
return hit;
}
You have several problems in your code. I've tried to clean them up. Also, I hope you can use c++ 11.
bool AlienMgr::checkHit(PlayerMgr& playerMgr)
{
bool hit = false; // If the player hits an alien, switches to true and is returned.
list<Missile*> missileList = playerMgr.getMissiles();
int missileCount;
FloatRect missileBounds;
FloatRect alienBounds;
// iter == myAliens.end() if it's empty
for (auto iter = myAliens.begin(); iter != myAliens.end();)
{
bool hitDetected = false;
alienBounds = (*iter)->getGlobalBounds();
// remove all missiles intersecting current alien and set flag
missileList.remove_if([&](Missile* missile)
{
if (!alienBounds.intersects(missile->getMissileBounds()))
return false;
hitDetected = true;
delete missile;
return true;
});
if (hitDetected)
{
hit = true;
delete (*iter);
iter = myAliens.erase(iter);
cout << "HIT" << endl;
else
{
iter++;
}
}
return hit;
}
However, there are more critical problems. Your code is too c++98'ish.
I would advise several improvements:
Use std::vector instead of std::list (it's really hard to find case when list will be faster)
Use smart pointers instead of manual memory management (code would be much simple and easier to understand)

Memory in use issue using marmalade SDK

I have a bullet class. and I try to instantiate it by the following code:
I always get an assertion that there is a memory that is in use.. why?
In another class called ship:
if (g_Input.isKeyDown(s3eKeySpace))// && Canfire)
{
Bullet *bullet = new Bullet();
bullet->Init(SHIP_BULLET);
bullet->setPosition(Position.x, Position.y - 20);
Bullets->push_back(bullet);
Canfire = false;
}
This is called each frame which causes memory still in usage:
for (list<Bullet*>::iterator it = Bullets->begin(); it != Bullets->end();)
{
(*it)->Update(dt);
if ((*it)->IsDestroyed)
{
Canfire = true;
it = Bullets->erase(it);
}
else
{
it++;
Canfire = false;
}
}
the destructor of the Ship class
Ship::~Ship()
{
for (std::list<Bullet*>::iterator it = Bullets->begin(); it != Bullets->end(); ++it)
delete *it;
delete Bullets;
}
class Bullet
{
public:
Bullet();
~Bullet();
public:
void Init(BulletTypes bulletType);
void Update(float dt);
void Render();
CIw2DImage* Image; // curr image
}
void Bullet::Init(BulletTypes bulletType)
{
BulletType = bulletType;
if (BulletType == SHIP_BULLET)
{
Image = Iw2DCreateImage("textures/ship_bullet.png");
if (Image == nullptr)
return;
}
}
Bullet::~Bullet()
{
delete Image;
}
Executing this code causes the leak:
for (list<Bullet*>::iterator it = Bullets->begin(); it != Bullets->end();)
{
(*it)->Update(dt);
if ((*it)->IsDestroyed)
{
Canfire = true;
it = Bullets->erase(it);
}
else
{
it++;
Canfire = false;
}
}
What is going on is basically you removing a dynamically allocated element from a container, losing any reference to it thus you cannot free its memory anymore. Calling Ship destructor only frees the elements that are currently in the list, excluding the ones removed in the iteration, obviously.
I suggest this as a fix:
for (list<Bullet*>::iterator it = Bullets->begin(); it != Bullets->end();)
{
(*it)->Update(dt);
if ((*it)->IsDestroyed)
{
Canfire = true;
delete *it; // it now points to invalid address
it = Bullets->erase(it);
}
else
{
it++;
Canfire = false;
}
}
Another option would be to store all removed bullets in some other container in class Ship in case that a Bullet can be referenced after it was destroyed. Problem with this idea is that you have alot of destroyed bullets taking up memory and have to come up with a solution on how to remove them once they really prove useless.
If you get stuck with that, using std::shared_ptrs instead of raw pointers in the list could solve your issue (for slight performance penalty).

Code breaks when I don't output the variable; works when I do. WHUT?

I don't really know what more to say about this.
I have never, ever seen this happen. And it just boggles me what causes this. I figured you guys might have seen this before and now what silly thing I am doing.
Notes about it: 'temp' is a variable the function is taking in. It is a Binary Tree Node (BTN). temp->d accesses the data. ->l is left child, ->r is right child. ->p is parent
NOTE: THIS IS HOMEWORK. IT IS FOR A PRIORITY QUEUE.
NOTE2: I have looked at similar questions. All have suggested a timing error and the 'cout' 'slows' the code down. What would cause this? I can't think of any really 'costly' things I am doing.
BTN<generic>* nTemp;
cout << "Sdf\n";
if(temp->r != NULL)
{
if(temp->l != NULL)
{
if(*(temp->r->d) > *(temp->l->d))
{
if(*(temp->r->d) > *(temp->d))
{
cout << "ASDfs5: " << *(temp->d) << "\n"; //THIS IS WHAT KEEPS IT FROM BREAKING / SEGFAULTING
nTemp->d = temp->d; //THIS IS WHERE IT BREAKS (SEGFAULT)
cout << "ASDfs\n";
temp->d = temp->r->d;
cout << "ASDfs4\n";
temp->r->d = nTemp->d;
cout << "ASDfs3\n";
if(temp->r != NULL)
{
checkPopValues(temp->r);
cout << "ASDfs2\n";
}
}
}
else
{
if(*(temp->l->d) > *(temp->d))
{
nTemp->d = temp->d;
temp->d = temp->l->d;
temp->l->d = nTemp->d;
checkPopValues(temp->l);
}
}
}
else
{
if(*(temp->r->d) > *(temp->d))
{
nTemp->d = temp->d;
temp->d = temp->r->d;
temp->r->d = nTemp->d;
checkPopValues(temp->r);
}
}
}
else
{
cout << "sdfs\n";
if(temp->l != NULL)
{
if(*(temp->l->d) > *(temp->d))
{
nTemp->d = temp->d;
temp->d = temp->l->d;
temp->l->d = nTemp->d;
checkPopValues(temp->l);
}
}
}
}
You aren't assigning anything to nTemp. It's pointing to garbage.
When you access it on the line that breaks, undefined behavior occurs. If you're lucky, you should get an access violation/segfault. If you're not lucky, it will silently 'work' but probably give you wrong results.
You need to assign a pointer to an existing BIN<Generic> to it or create a new one.
Make sure nTemp is initialized. I bet it isn't. The cout causes something "benign" to be placed in nTemp.
nTemp is not allocated any space so you are writing to random memory.