Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by Bozebo May 12, 2011 at 2:18 PM 1 replies 3.5k views
Original Post
Bozebo
Bozebo
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
#include

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

class ribMover{
int id;
std::queue 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 movers;

public:
//creates a new mover
void newMover(int moverId){
std::pair 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).
rip-off
rip-off
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.
Bozebo
Bozebo

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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.