When ever to run the following piece of code in my game it crashes. I have narrowed down the problem to being initiating mEnemies[i] = NULL and mSpaceship.mBullets[c] = NULL. Got no idea why. Please help

for(int i=0; i<mEnemies.size(); i++)
    {
        // cycle of bullets
        for(int c=0; c<mSpaceship.mBullets.size(); c++)
        {
            if(mSpaceship.mBullets[c]->checkCollision( *mEnemies[i] ))
            {
                delete mEnemies[i];
                mEnemies[i] = NULL;
                delete mSpaceship.mBullets[c];
                mSpaceship.mBullets[c] = NULL;

                breakLoop = true;
            }

            if(breakLoop)
                break;
        }

        if(breakLoop)
            break;
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

By leaving the nullified pointers in the vectors, the second time around you iterate over the vectors, a crash is more than likely to happen (assuming these indices remain NULL pointers). Also you may want to check what the destructors are doing.

[EDIT]
Oh, and please read What are code tags

Edited 6 Years Ago by mitrmkar: n/a

thanks mate, its the first time i have used danweb and i did ti in a rush but i shall read 'What code tags are'.

Hi,
I'm making this 3d zombie shoot em up and it was all going fine until I added a vector of bullets and them when ever I add a new bullet to the vector the camera position and angle is changed. There is nothing I have written to do this, any help would be appreciated.

Gun class:

#pragma once

#include "Object.h"
#include "Bullet.h"
#include <vector>
using std::vector;

class Gun : public Object
{
private:
	int    mFired;
	int    mHeat;
	bool   mWaitCool;
	vector <Bullet> mBullets;

public:
	Gun ();
	~Gun();

	void update();

	void shoot ();
	void reload();
};
#include "DarkGDK.h"
#include "Gun.h"

Gun::Gun() : Object("Models//Gun//Colt.x", 5400, 1000, -770, 0), mFired(0), mHeat(0), mWaitCool(false)
{
	scale(8, 8, 8);

	// gun fire sound
	dbLoadSound    ("Sounds//Gun//GunFiring.wav", 1);
	dbSetSoundSpeed(1, 90000);
}

Gun::~Gun()
{
}

void Gun::update()
{
	// display mesage if the gun is too hot
	if(mWaitCool)
		dbText(20, 20, "Overheated");

	// cooling down the gun
	if(mHeat != 0)
		mHeat--;

	// checking if the gun has cooled
	if(mHeat == 0 && mWaitCool == true)
		mWaitCool = false;

	// checking if the gun if too hot
	if(mHeat >= 300)
		mWaitCool = true;

	for(int i=0; i<mBullets.size(); i++)
	{
		mBullets[i].move();

		if(mBullets[i].outOfRange())
		{
			mBullets.erase(mBullets.begin() + i);
		}
	}
}

void Gun::shoot()
{
	// if the gun if not cooling or too hot
	if(!mWaitCool)
	{
		if(!dbSoundPlaying(1))
			dbPlaySound(1);

		mHeat += 2;

		mBullets.push_back(Bullet());
	}
}

Bullet class:

#pragma once

#include "Object.h"

class Bullet : public Object
{
	static const int mRange = 3000;
	int mTravelled;

public:
	Bullet ();
	~Bullet();

	void move();
	bool outOfRange();
};
Bullet::Bullet() : Object("Models//Bullets//Bullet.x", 5400, 1000, -770, 20), mTravelled(0)
{ 
	scale(8, 8, 8);
	rotate(0, 270, 0); 
	hide();
}

Bullet::~Bullet()
{
}

void Bullet::move()
{
	dbMoveObject(getID(), getSpeed());
	mTravelled += getSpeed();
}

bool Bullet::outOfRange()
{
	if(mTravelled >= mRange)
	{
		dbText(20, 40, "out of range");
		mTravelled = 0;
		return true;
	}
	return false;
}

As for the camera problem, maybe focus on the parts of code that call Gun::shoot() and see if youl find anything suspicious/problem-related. Have you considered posting to a Dark GDK forum?

In the Gun::update() , the for-loop is not quite right. It only works correctly when no bullets end up being out-of-range after the call to move() .

PS. In the future, rather start new threads than continue the old ones.
This is to avoid the threads from turning into 'lengthy discussions' more or less unrelated to the initial problem.

This question has already been answered. Start a new discussion instead.