Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Louie

Pointers in vectors

This topic is 5958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone have experience with trying to store pointers in STL vectors? I have vectors that store both char* arrays and function pointers - when it comes to clear the vectors out and close the executable down, I get an assertion failure - which I guess stands to reason as the vectors would be left full of dangling pointers at that point (I was hoping the vectors own destructor would tidy this up, but evidently not). I''ve tried cycling through the items in the vector, setting the pointers to 0 and ''delete''ing them before closing down, but the assertion failure is still there afterwards. Any suggestions as to what I''m missing out on?

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Louie
Does anyone have experience with trying to store pointers in STL vectors?

Yes.
quote:

I have vectors that store both char* arrays and function pointers - when it comes to clear the vectors out and close the executable down, I get an assertion failure - which I guess stands to reason as the vectors would be left full of dangling pointers at that point (I was hoping the vectors own destructor would tidy this up, but evidently not).

This sounds more like you''re doing something wrong elsewhere.
quote:

I''ve tried cycling through the items in the vector, setting the pointers to 0 and ''delete''ing them before closing down, but the assertion failure is still there afterwards.

That''s because the vector is not the problem, from the sounds of it.
quote:

Any suggestions as to what I''m missing out on?

Well, you seem to be missing out on smart pointers, but that''s not related to your actual problem. You''re going to have create a small demo that recreates your problem and post the code. A program can speak a thousand words.

Share this post


Link to post
Share on other sites
Have you traced down the assertion ? Did you check why it failed ? Usually that will hint to what is wrong in your code.

Pointers do not have destructors, therefore the objects they point to are not deleted when the pointer is (which is the WHOLE point of having pointers). Have you considered using boost::shared_ptr to manage the life-time of your objects ?

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
quote:
Original post by Louie
Does anyone have experience with trying to store pointers in STL vectors?

They work just the same as storing anything else in there.

quote:
I have vectors that store both char* arrays and function pointers - when it comes to clear the vectors out and close the executable down, I get an assertion failure - which I guess stands to reason as the vectors would be left full of dangling pointers at that point (I was hoping the vectors own destructor would tidy this up, but evidently not).

Well, no. The vector is responsible for freeing whatever is stored in the vector. Not what the items are stored point to. That''s your problem. As far as a container is concerned, a pointer is just a number like an integer.

quote:
I''ve tried cycling through the items in the vector, setting the pointers to 0 and ''delete''ing them before closing down, but the assertion failure is still there afterwards.

I hope you don''t mean that you set them to zero and then call delete on them, as that is totally pointless and does nothing.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!