Map as member variable throwing read access violation on insert

0

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});
  }
}
c++
c++17
asked on Stack Overflow Jun 18, 2020 by Kenny Castro • edited Jun 18, 2020 by Kenny Castro

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0