jeudi 30 novembre 2017

Seg Fault on unordered_map.size()

I'm having a lot of trouble with this one- I have a server app that receives a heartbeat from all the other server apps. When a heartbeat is received, the primary server (lets call it cortex) will update an unordered_map with that server.

When I have servers other than the Cortex running, the Cortex will crash almost immediately when I call unordered_map.size(). I have a mutex locking the map prior to this call too, so when it enters this function, the mutex is locked if nothing else is updating it first.

The Key for the map is a custom hash-

unordered_map<AppIdentity, AppResource> appMap; //This is in cortex's header

AppIdentity is a struct:

struct AppIdentity {
    AppType name;
    string ip;
    ushort port;
};

namespace std {
    template <>
    struct hash< AppIdentity > {
        size_t operator( )( const AppIdentity& id ) const {
            return ( hash< string >( )( toString( id.name ) ) ^ hash< string >( )( id.ip ) ) ^ hash< ushort >( )( id.port );
        }
    };
}

(toString simply converts the name to a string version)

And this is where Cortex is crashing:

try {
        lock_guard< mutex > lck( appMapMtx );
        cout << "Checking AppMap Size...\n";
        cout << "AppMap Size: " << appMap.size( ) << endl; //Crashes here
}

Any clue as to what would be causing this? I read it might be related to the hash function since I'm using an object as the key, but this app only crashes when Cortex is launched AFTER the other apps- and it doesn't happen all the time either.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire