Skip to main content
more accurate title
Source Link
user1430
user1430

Memory leak when removing Is the way I remove objects causing a memory leak?

The game I'm working on consumes memory, and the memory used in megabytes increases around 2MB every 4 seconds.

It starts at 5MB and grows to hundreds of megabytes in minutes.

I was careful, and have little to no new variable declarations in the main game loop (and the existing ones are integers and floats, not SDL_Surface's and images).

However, I keep creating new instances of classes, such as bullets and enemies, but I'm also careful with those:

/* Is a bullet out of the screen? Remove it from the list! */ for (i = 0; i < bullets.size(); i++) { if (!bullets[i].is_on_screen(screen)) bullets.erase(bullets.begin() + i); } 

I remove them from the respective vectors when they are no longer needed, and with enemies too:

/* Is the enemy death? Put it out of the array! */ for (i = 0; i < enemies.size(); i++) { if (enemies[i].health <= 0) enemies.erase(enemies.begin() + i); } 

Also, I create new Enemy(ies) with an image, but I only load the image on the beginning of the game, and then pass a pointer of it to the newly created class, so that it can draw it. I'm not creating hundreds of new images of enemies and bullets, am I?

When I erase the instances from the vectors, am I not fully deleting them from RAM?

Thank you!

Memory leak when removing objects

The game I'm working on consumes memory, and the memory used in megabytes increases around 2MB every 4 seconds.

It starts at 5MB and grows to hundreds of megabytes in minutes.

I was careful, and have little to no new variable declarations in the main game loop (and the existing ones are integers and floats, not SDL_Surface's and images).

However, I keep creating new instances of classes, such as bullets and enemies, but I'm also careful with those:

/* Is a bullet out of the screen? Remove it from the list! */ for (i = 0; i < bullets.size(); i++) { if (!bullets[i].is_on_screen(screen)) bullets.erase(bullets.begin() + i); } 

I remove them from the respective vectors when they are no longer needed, and with enemies too:

/* Is the enemy death? Put it out of the array! */ for (i = 0; i < enemies.size(); i++) { if (enemies[i].health <= 0) enemies.erase(enemies.begin() + i); } 

Also, I create new Enemy(ies) with an image, but I only load the image on the beginning of the game, and then pass a pointer of it to the newly created class, so that it can draw it. I'm not creating hundreds of new images of enemies and bullets, am I?

When I erase the instances from the vectors, am I not fully deleting them from RAM?

Thank you!

Is the way I remove objects causing a memory leak?

The game I'm working on consumes memory, and the memory used in megabytes increases around 2MB every 4 seconds.

It starts at 5MB and grows to hundreds of megabytes in minutes.

I was careful, and have little to no new variable declarations in the main game loop (and the existing ones are integers and floats, not SDL_Surface's and images).

However, I keep creating new instances of classes, such as bullets and enemies, but I'm also careful with those:

/* Is a bullet out of the screen? Remove it from the list! */ for (i = 0; i < bullets.size(); i++) { if (!bullets[i].is_on_screen(screen)) bullets.erase(bullets.begin() + i); } 

I remove them from the respective vectors when they are no longer needed, and with enemies too:

/* Is the enemy death? Put it out of the array! */ for (i = 0; i < enemies.size(); i++) { if (enemies[i].health <= 0) enemies.erase(enemies.begin() + i); } 

Also, I create new Enemy(ies) with an image, but I only load the image on the beginning of the game, and then pass a pointer of it to the newly created class, so that it can draw it. I'm not creating hundreds of new images of enemies and bullets, am I?

When I erase the instances from the vectors, am I not fully deleting them from RAM?

Freeing up memory in general (SDL, C++) Memory leak when removing objects

added 256 characters in body
Source Link
David Gomes
  • 575
  • 2
  • 8
  • 15

The game I'm working on consumes memory, and the memory used in megabytes increases around 2MB every 4 seconds.

It starts at 5MB and grows to hundreds of megabytes in minutes.

I was careful, and have little to no new variable declarations in the main game loop (and the existing ones are integers and floats, not SDL_Surface's and images).

However, I keep creating new instances of classes, such as bullets and enemies, but I'm also careful with those:

/* Is a bullet out of the screen? Remove it from the list! */ for (i = 0; i < bullets.size(); i++) { if (!bullets[i].is_on_screen(screen)) bullets.erase(bullets.begin() + i); } 

I remove them from the respective vectors when they are no longer needed, and with enemies too:

/* Is the enemy death? Put it out of the array! */ for (i = 0; i < enemies.size(); i++) { if (enemies[i].health <= 0) enemies.erase(enemies.begin() + i); } 

Also, I create new Enemy(ies) with an image, but I only load the image on the beginning of the game, and then pass a pointer of it to the newly created class, so that it can draw it. I'm not creating hundreds of new images of enemies and bullets, am I?

When I erase the instances from the vectors, am I not fully deleting them from RAM?

Thank you!

The game I'm working on consumes memory, and the memory used in megabytes increases around 2MB every 4 seconds.

It starts at 5MB and grows to hundreds of megabytes in minutes.

I was careful, and have little to no new variable declarations in the main game loop (and the existing ones are integers and floats, not SDL_Surface's and images).

However, I keep creating new instances of classes, such as bullets and enemies, but I'm also careful with those:

/* Is a bullet out of the screen? Remove it from the list! */ for (i = 0; i < bullets.size(); i++) { if (!bullets[i].is_on_screen(screen)) bullets.erase(bullets.begin() + i); } 

I remove them from the respective vectors when they are no longer needed, and with enemies too:

/* Is the enemy death? Put it out of the array! */ for (i = 0; i < enemies.size(); i++) { if (enemies[i].health <= 0) enemies.erase(enemies.begin() + i); } 

When I erase the instances from the vectors, am I not fully deleting them from RAM?

Thank you!

The game I'm working on consumes memory, and the memory used in megabytes increases around 2MB every 4 seconds.

It starts at 5MB and grows to hundreds of megabytes in minutes.

I was careful, and have little to no new variable declarations in the main game loop (and the existing ones are integers and floats, not SDL_Surface's and images).

However, I keep creating new instances of classes, such as bullets and enemies, but I'm also careful with those:

/* Is a bullet out of the screen? Remove it from the list! */ for (i = 0; i < bullets.size(); i++) { if (!bullets[i].is_on_screen(screen)) bullets.erase(bullets.begin() + i); } 

I remove them from the respective vectors when they are no longer needed, and with enemies too:

/* Is the enemy death? Put it out of the array! */ for (i = 0; i < enemies.size(); i++) { if (enemies[i].health <= 0) enemies.erase(enemies.begin() + i); } 

Also, I create new Enemy(ies) with an image, but I only load the image on the beginning of the game, and then pass a pointer of it to the newly created class, so that it can draw it. I'm not creating hundreds of new images of enemies and bullets, am I?

When I erase the instances from the vectors, am I not fully deleting them from RAM?

Thank you!

Source Link
David Gomes
  • 575
  • 2
  • 8
  • 15
Loading