Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8608341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:35:16+00:00 2026-06-12T03:35:16+00:00

In my simple game engine I have a vector containing pointers to entities. Each

  • 0

In my simple game engine I have a vector containing pointers to entities. Each entity is allocated with the new keyword. To call functions on my entities I use an iterator and so when deleting my entities I figured I’d use an iterator as well, like so:

vector<Entity*>::iterator iter;
for (iter = gameEntitys.begin(); iter != gameEntitys.end();) {
    delete (*iter);
    ++iter;
}

But I now have a horrific memory leak and all profiling tools point towards the line delete (*iter);

Clearly there is something wrong but what is the correct way for me to delete the entities contained in the vector (and clear the vector for another scene). Using gameEntitys.clear(); is useless to my knowledge as it only removes the elements of the vector not actually calling delete on them as they are pointers rather than actual data.

Edit: I have bee watching the comments. The Entity class is not polymorphic and does not have any subclasses. Would it make more sense if I stopped using dynamic memory and had a normal array of non-pointer entities?

The reason I know there is a memory leak is because when the application starts the memory use shoots up to more than 2gb before crashing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:35:17+00:00Added an answer on June 12, 2026 at 3:35 am

    Most likely, the memory leak is coming from the destructor of your Entity or one of its subclasses: you are performing your delete operations correctly, so the destructor itself must be at fault. One common problem is not making destructors virtual in a polymorphic class hierarchy.

    You are also absolutely right about uselessness of calling gameEntitys.clear(): it will leak your objects “wholesale” by “forgetting” the references to them.

    If you are on C++11, consider changing the definition to use std::unique_ptr

    std::vector<std::unique_ptr<Entity> > gameEntitys;
    

    This will free you from the need to manage the memory of your entries manually, while letting you keep pointers to objects of derived classes in your vector. If you use unique_ptr, calls to gameEntitys.clear() will destroy the items pointed to by vector’s elements.

    Dealing with the vector of unique_ptr is somewhat different (for example, inserting new items requires extra care, see this answer for details) but I think the positives of simplified memory management compensate for the slight inconveniences.

    EDIT : Since your Entity class is not polymorphic, consider switching to std::vector<Entity>, unless you plan to switch to a polymorphic hierarchy later on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple game engine, and I have the EntityComponent.h file: #ifndef Psycho2D_Core_EntityComponent_
I have been working on a simple component based c++ and lua game engine.
I'm attempting to make a simple homebrew game engine/framework in android. I have the
I am thinking of making a simple game engine for my course final year
I'm currently writing a very simple game engine for an assignment and to make
In college, my senior project was to create a simple 2D game engine complete
Can anyone recommend a good Java game engine for developing simple tile-based games? I'm
In simple game, where map (terrain) is divided in grid cell I have soldier
I'm writing a simple game and I'm going to have the mouse control the
I am programming a simple 2d game engine. I've decided how I'd like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.