我的问题是,即使删除删除也不删除我的对象。我很确定我使用的是正确的删除方式,但仍然无法正常工作。我已经查看了我的代码,但是找不到任何可以告诉我为什么它不起作用的信息。我删除了一些我确定与此无关的功能。我怀疑会导致这些问题的函数是removeProjectile函数,但我无法弄清楚原因或原因。最大的问题是Visual Studio报告我存在内存泄漏,如果正确删除所有内容都不会发生。我将不胜感激,如果有人能找到问题,谢谢!
显然这是一个重复的问题,但是我看到的很多问题是为什么它们在删除后仍然可以访问内容,我不在乎我只在析构函数中删除。我希望Visual Studio不再报告我有内存泄漏,甚至很难,我删除了会导致内存泄漏的内容
void Game::render()
{
if (gameState == PLAY) {
if (player->isAlive()) {
this->myWindow.clear();
this->myWindow.draw(backgroundSprite);
for (int i = 0; i < player->getNrOfShots(); i++) {
this->myWindow.draw(*this->projectile[i]);
}
for (int i = 0; i < arraysize; i++) {
this->myWindow.draw(*invaderArray[i]);
}
this->myWindow.draw(*this->ammoIncrease);
this->myWindow.draw(*this->fireIncrease);
this->myWindow.draw(*this->extraLife);
this->myWindow.draw(*this->player);
this->myWindow.draw(text);
this->myWindow.display();
}
}else if(gameState == PAUSE) {
this->myWindow.draw(menu);
this->myWindow.display();
}
else if (gameState == HIGHSCORE) {
onHighScorePress();
}
else if (gameState == GAME_OVER) {
gameOver();
}
}
Game::Game()
:myWindow(sf::VideoMode(WIDTH, HEIGHT), "Space invader"),
player(new Player(7.f, WIDTH, HEIGHT, 3, 30, 0, 100)),
bounds(new Projectiles(0, -100, -100, WIDTH, HEIGHT)),
menu(WIDTH, HEIGHT)
{
gameState = PAUSE;
player->setPosition(WIDTH / 2, 700.f);
ammoIncrease = new PowerUp("../images/ammo.png", 3, WIDTH, HEIGHT, 0);
fireIncrease = new PowerUp("../images/fireRate.png", 3, WIDTH, HEIGHT, 0);
extraLife = new PowerUp("../images/lifeHeart.png", 3, WIDTH, HEIGHT, 0);
projectile = new Projectiles*[player->getBulletCap()];
invaderArray = new Invader*[arraysize];
initInvader();
//loading backgroundtexture
backgroundTexture.loadFromFile("../images/Background.png");
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setScale(0.3, 0.5);
//Loading sounds
if (!buffer.loadFromFile("../sounds/shoot.wav")) {
std::cout << "Error loading shoot sound" << std::endl;
}
if (!pickUpBuffer.loadFromFile("../sounds/pickUp.wav")){
std::cout << "Error loading pick up sound" << std::endl;
}
if (!backgroundSound.openFromFile("../sounds/background.wav")) {
std::cout << "Error loading background music" << std::endl;
}
if (!collisionBuffer.loadFromFile("../sounds/playerCollide.wav")) {
std::cout << "Error loading player collide sound" << std::endl;
}
shootSound.setBuffer(buffer);
shootSound.setVolume(10.f);
pickUpSound.setBuffer(pickUpBuffer);
pickUpSound.setVolume(2.f);
collisionSound.setBuffer(collisionBuffer);
collisionSound.setVolume(2.f);
backgroundSound.setVolume(10.f);
backgroundSound.setLoop(true);
backgroundSound.play();
//Text
font.loadFromFile("C:/Windows/Fonts/Arial.ttf");
text.setFont(font);
text.setCharacterSize(20);
text.setFillColor(sf::Color::Green);
text.setPosition(1, 1);
highScoreFont.loadFromFile("C:/windows/fonts/arial.ttf");
highScoreText.setFont(highScoreFont);
highScoreText.setCharacterSize(30);
highScoreText.setFillColor(sf::Color::Green);
highScoreText.setPosition(1, 1);
}
Game::~Game()
{
delete player;
delete ammoIncrease;
delete fireIncrease;
delete extraLife;
for (int i = 0; i < player->getBulletCap(); i++) {
delete projectile[i];
}
delete[] projectile;
delete bounds;
for (int i = 0; i < arraysize; i++) {
delete invaderArray[i];`
}
delete[] invaderArray;
}
void Game::initInvader()
{
invaderArray[0] = new Invader(0.5, 0.5, smallSpeed, WIDTH, HEIGHT, smallHP);
invaderArray[1] = new Invader(0.5, 0.5, smallSpeed, WIDTH, HEIGHT, smallHP);
invaderArray[2] = new Invader(0.5, 0.5, smallSpeed, WIDTH, HEIGHT, smallHP);
invaderArray[3] = new Invader(1, 1, mediumSpeed, WIDTH, HEIGHT, mediumHP);
invaderArray[4] = new Invader(1, 1, mediumSpeed, WIDTH, HEIGHT, mediumHP);
invaderArray[5] = new Invader(1, 1, mediumSpeed, WIDTH, HEIGHT, mediumHP);
invaderArray[6] = new Invader(1.3, 1.3, largeSpeed, WIDTH, HEIGHT, largeHP);
invaderArray[7] = new Invader(1.3, 1.3, largeSpeed, WIDTH, HEIGHT, largeHP);
invaderArray[8] = new Invader(1.3, 1.3, largeSpeed, WIDTH, HEIGHT, largeHP);
}
void Game::removeProjectile(int position)
{
if (player->getNrOfShots() > 1) {
Projectiles * last = projectile[player->getNrOfShots() - 1];
Projectiles ** copy = &projectile[position];
//delete bullet[position];
//projectile[position] = new Projectiles(5, player->getPosition().x +
bounds->getGlobalBounds().width, player->getPosition().y, WIDTH, HEIGHT);
*copy = last;
projectile[player->getNrOfShots() - 1] = nullptr;
player->removeShot();
}
else if(player->getNrOfShots() == 1) {
projectile[position] = nullptr;
player->removeShot();
}
}
void Game::shootBullet()
{
//shooting
if (player->isFiring()) {
this->shootTimer++;
if (this->shootTimer >= player->getFireRate() && player->getNrOfShots()
<= player->getBulletCap() - 1) {
projectile[player->incrementShots()] = new Projectiles(5, player-
>getPosition().x + bounds->getGlobalBounds().width, player-
>getPosition().y, WIDTH, HEIGHT);
this->shootTimer = 0;
shootSound.play();
}
}
}