Jump to content
  • Advertisement
Sign in to follow this  
Bozebo

(c++) std::map.insert causing segfault

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

I have used maps in this way before without any issues, but now I am encountering a segmentation fault that I can't find a solution to.


I have 2 classes, and one of them holds a map of pointers to objects in the other class, with ints as keys (I tried unsigned shorts before then reverted to ints trying to fix it, I will stick with ints for now)

[source lang=cpp]
#include <queue>
#include <map>

struct moverLocation{
float x,y;
unsigned long tick;
unsigned int msAfter;
};

class ribMover{
int id;
std::queue<moverLocation> locations;

public:
ribMover(int setId);

void addLocation(float sX, float sY, unsigned long tick, unsigned int msAfter){
moverLocation newLocation;
newLocation.x = sX;
newLocation.y = sY;
newLocation.tick = tick;
newLocation.msAfter = msAfter;
locations.push(newLocation);
}
};

class ribMoverController{
std::map<int,ribMover*> movers;

public:
//creates a new mover
void newMover(int moverId){
std::pair<int,ribMover*> mapPair(moverId,new ribMover(moverId)); //make a pair
movers.insert(mapPair); //insert the pair into the map
fprintf(logFile,"new mover created with id %d\n",moverId);
}

//deletes a mover
void deleteMover(int moverId){
movers.erase(moverId); //remove the mover
fprintf(logFile,"mover with id %d deleted\n",moverId);
}
};

[/source]

It crashes on the line:
movers.insert(mapPair);

Why? I have used maps like this in the past without any problems at all. Also, I wasn't originally storing pointers to objects in the map, but when that didn't work I switched to pointers incase that would help somehow.

I've checked my code many times. The only difference between it and another instance of maps I programmed recently is that it uses ints whereas I was using SOCKET types (also an int I believe).

Share this post


Link to post
Share on other sites
Advertisement
Is the ribMoverController object (the one you call insertMover() on) valid?

Note that your class lacks a copy constructor and assignment operator, this could cause trouble if you have a destructor.

Share this post


Link to post
Share on other sites

Is the ribMoverController object (the one you call insertMover() on) valid?


No it wasn't! Thanks. I forgot to even spawn an instance of ribMoverController in the pointer I had declared for it and was using to access it.

That was a silly mistake.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!