I have an issue were a program of mine throws an access violation. Through debugging I found that for some reason the comparator-function is null. However I am not sure on why that is or how to fix it.
I created a minimum working example to reproduce the issue:
#include "Tryout.h"
#include <queue>
struct Event {
uint64_t mv_timeout;
};
bool CompareEvents(const Event& a, const Event& b) {
return a.mv_timeout < b.mv_timeout;
}
int main() {
std::priority_queue<Event, std::vector<Event>, decltype(&CompareEvents)> mt_eventQueue;
Event lo_event1{
.mv_timeout = 1,
};
Event lo_event2{
.mv_timeout = 2,
};
mt_eventQueue.push(lo_event1);
mt_eventQueue.push(lo_event2);
return 0;
}
When executing it crashes when adding the second event.
Exception thrown at 0x0000000000000000 in Tryout.exe: 0xC0000005: Access violation executing location 0x0000000000000000.
As mentioned it seems the comparing function is null, even tough I passed it via template.
What is going wrong here?
You need to specify the comparison function explicitly in the constructor
std::priority_queue<Event, std::vector<Event>, decltype(&CompareEvents)> mt_eventQueue( CompareEvents );
Otherwise the default argument will be used for the comparison function that will yield a null pointer.
The used constructor has the following declaration with default arguments
explicit priority_queue(const Compare& x = Compare(), Container&& = Container());