So I've been having this weird issue, I have maps as member variables in a class, but when I go to insert the first element in the map, I get a Exception has occurred: W32/0xc0000005 Unhandled exception thrown: read access violation. _Scary was nullptr.
error. I've got the relevant code below, please help.
Edit: No exception is actually thrown at runtime, or at least it's not caught in the catch block. I have to go into debug mode to read the exception, any idea why that is?
main.cpp
try {
auto server = std::make_shared<express::Server>(8080);
server
->get("/",
[](express::request req, express::response res) {
std::string message =
"<html><title>Hello World!</title></html>";
res.send(message);
})
.get("/file", [](express::request req, express::response res) {
res.sendFile("./html/index.html");
});
server->listen("0.0.0.0", "/");
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
class Server : public std::enable_shared_from_this<Server> {
public:
Server(unsigned short port);
void acceptConnection();
void listen(std::string address, std::string doc_root);
// Can throw runtime exceptions
Server& get(const std::string& path, reqHandler callback);
private:
boost::asio::io_context m_ioc{MAX_THREADS};
tcp::acceptor m_acceptor;
unsigned short m_port;
std::string m_docRoot;
std::shared_ptr<requestHandler> m_handler;
// {urlPath, lambda}
};
Server& Server::get(const std::string& path, reqHandler callback) {
m_handler->addPath(verb::GET, path, callback);
return *this;
}
class requestHandler {
std::map<const std::string, reqHandler> m_get;
public:
requestHandler() = default;
reqHandler getHandler(verbs verb, const std::string& path);
void addPath(verbs verb, const std::string& path, reqHandler callback);
void addAll(std::string& path, reqHandler callback);
};
void requestHandler::addPath(verbs verb, const std::string& path,
reqHandler callback) {
switch (verb) {
case verbs::GET:
m_get.insert({path, callback});
}
}
User contributions licensed under CC BY-SA 3.0