Jump to content
  • Advertisement
Sign in to follow this  
dwfait

Segmentation Fault with push_back() on vector

This topic is 3735 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

Hi. I have a class, with a vector declared like this: std::vector<Block*> blockList; Now, in a method of that class, I have the following code: Block* newBlock = new Block(); blockList.push_back(newBlock); Now, this code compiles and runs fine. As expected. However, when I run the debug on code::blocks (using mingw btw) I get a segmentation fault on the push_back line, with the call stack showing this as the last called function: #0 00443004 std::vector<Block*, std::allocator<Block*> >::push_back(this=0xbaadf011, __x=@0x27fc58) (C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_vector.h:558) Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
I don't think you've provided enough information, but I'll take a stab at it. Are you using the static keyword by any chance?

EDIT: I ask this because you could very well be trying to access the vector before it has been initialized.

Share this post


Link to post
Share on other sites
The problem is probably somewhere else where you are corrupting the heap and then it crashes on push_back.
Check your array bounds and other suspicious areas.These kind of bugs are the hardest to find.THe problem is elsewhere but it crashes somewhere it shouldn't or it crashes on debug and not on release etc.
Also watch out for the pointers in the vector.
If you free the newBlock later the pointer in the vector will be pointing to nowhere.

Share this post


Link to post
Share on other sites
Quote:
Original post by Wavarian
I don't think you've provided enough information, but I'll take a stab at it. Are you using the static keyword by any chance?

EDIT: I ask this because you could very well be trying to access the vector before it has been initialized.


No, no static members / methods in either of those classes (Block or the class that code is in).



Quote:
Original post by Black Knight
The problem is probably somewhere else where you are corrupting the heap and then it crashes on push_back.
Check your array bounds and other suspicious areas.These kind of bugs are the hardest to find.THe problem is elsewhere but it crashes somewhere it shouldn't or it crashes on debug and not on release etc.
Also watch out for the pointers in the vector.
If you free the newBlock later the pointer in the vector will be pointing to nowhere.



Are there any tools or anything that could help me locate the actual problem?

Share this post


Link to post
Share on other sites
Quote:
Original post by dwfait
#0 00443004 std::vector<Block*, std::allocator<Block*> >::push_back(this=0xbaadf011, __x=@0x27fc58) (C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_vector.h:558)
The this pointer is 0xbaadf011, which is 4 bytes from 0xbaadf00d.

Looks to me like this vector is a class member 4 bytes from the start of a class (one int or pointer is before it), and you've delete'd the class. That'll cause the class's data to be invalid, which would make the vector's this pointer invalid when a function is called on it.

Make sure that you're not trying to access the class after it's been delete'd, and I'd recommend that you set all pointers to NULL after deleting them, which should make issues like this easier to spot.

Share this post


Link to post
Share on other sites
Quote:
Original post by dwfait
Are there any tools or anything that could help me locate the actual problem?


You can download the trial version of PurifyPlus from IBM, which does a great job of catching memory errors.

Or better yet, go read about auto_ptr and shared_ptr, then convert your code to use them. Raw pointers in C++ should be used very, very rarely. Likewise with C-style arrays.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!