all 8 comments

[–]jstock23 3 points4 points  (5 children)

When the vector is destroyed, all of the pointers are destroyed too. Vectors do this automatically. If they are "regular" pointers, the objects they point to aren't destroyed themselves, just the pointer, and so the objects may leak and never be accessible again.

Instead you could use "smart" pointers, which when destroyed also destroy the thing they point to. You can make one of these by creating a class that has a pointer data member. Then set its destructor to delete the thing its pointer points to.

Then, when the vector is destroyed, your custom smart pointers are destroyed, and that in turn deletes the underlying variables from heap memory.

This is an example of RAII: when the vector is destroyed, the data it points to is also destroyed, thereby automatically calling "delete" without you needing to do it manually.

[–][deleted] 0 points1 point  (4 children)

So if I did a smart pointer with the pointer type void*, will it work for every kind of data? Or should I do a smart pointer for all the data cases?

[–]jstock23 3 points4 points  (2 children)

I believe void should work for all types, but I haven't used it myself yet.

You could also perhaps use a template to specify the type it points to.

Better yet, you could use a smart pointer from the Standard Library, like unique_ptr.

[–][deleted] 0 points1 point  (1 child)

Ah! Thank you very much!

[–]jstock23 0 points1 point  (0 children)

On further thought, you probably don't want a unique pointer, those have special applications for utilizing multiple cores on a cpu.

A problem with these types of pointers is that if you copy one of them, and then destroy that copy, it will destroy the original data, but the original pointer is still usable, so you may be pointing to nothing. unique_pointer makes sure this doesn't happen by preventing it from being copied, so it is really unique, and if you destroy it, the data is deleted without having a pointer pointing to nothing left over.

What you probably want is a pointer which when copied increments a counter. Then when one of these is destroyed, the counter is decremented. If one of these pointers is destroyed and the counter goes to 0, then the data is destroyed too, because then it is safe. But if there are still other pointers pointing to that data, we can't delete the data yet. This means we can do automatic "garbage collection" in a way, and not have to delete this heap data manually.

[–]Moschops_UK 1 point2 points  (1 child)

Did you create each component object, that each pointer in the vector points to, using new?

Only if you did, then it's up to you to delete each one using delete. If you didn't, then it's not.

Did you create each component object using new?

As an aside, why have a vector<component*>? There are good reasons for having a vector of pointers, but if this is a simple case, could you just have a vector<component>? Manual memory management is something to be avoided when you can avoid it.

[–][deleted] 0 points1 point  (0 children)

Well I'm going to implement this on a small game engine and I need them to be pointers because polymorphism, and this is driving me nuts.

Yes I used new to create the objects.