vendredi 23 juin 2023

c hard 'std::bad_alloc' issue

Below is the MRE.

#include <iostream>
#include <string>
#include <cstdarg>

#define Level                2
#define Event_Client_Status  3

class Event
{
public:
   Event(const int ll, const int ty, const char* f, const int ln) :
      level(ll), type(ty), func(f), line(ln)
   {}
private:
   int level;
   int type;
   std::string func;
   int line;
};

class ClientStatusEvent : public Event
{
public:
   ClientStatusEvent(const int ty, const char* f, const int ln, va_list arguments) :
      Event(Level, ty, f, ln),
      client_name(va_arg(arguments, std::string))
   {}
private:
   std::string client_name;
};


void test_create_event(const int type, const char* func, const int line, ...)
{
   Event* event = nullptr;

   va_list arguments;
   va_start(arguments, line);
   event = new ClientStatusEvent(type, func, line, arguments);
   va_end(arguments);
}


int main()
{
   test_create_event(Event_Client_Status, "", 0, "client name");
}

The is the error when running it.

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

Aucun commentaire:

Enregistrer un commentaire