0

hello guys I have a problem with an Event Manager

first I have A Lambda that responsible for sending the event to my event Manager

glfwSetWindowSizeCallback(m_Window, [](GLFWwindow* window , int Width , int Height) {

WindowData Data = *(WindowData*)glfwGetWindowUserPointer(window);

WindowResize ReSize(Data.ID, Width, Height);

EventsManager::Get().PushEvent(&ReSize);

});

PushEvent Function should push back the pointer to the event queue which is on std:: vector<IEvent*>

void EventsManager::PushEvent(IEvent* e)
{
    m_EventsQueue.push_back(e);
}

actually this method is working fine when I have a single event but when I have multiple events in a row I got an access violation.

I just want to understand why this is happening and how I can work around this?

Ali New
  • 9
  • 2
  • 5
  • 2
    `EventsManager::Get().PushEvent(&ReSize);` -- You are storing the address of a local variable. What happens when that function block exits? That local variable is no longer valid, thus you now have stored the address of an invalid `Resize`. – PaulMcKenzie Sep 06 '20 at 23:24
  • *actually this method is working fine when I have a single event* -- It really wasn't working fine. The behavior was undefined all along. – PaulMcKenzie Sep 06 '20 at 23:34
  • @PaulMcKenzie thanks, you are right But what should do in this case, I can't pass a complete copy and store it on Vector because IEvent is a Pure abstract Class? what do you think the best option here without dynamic allocation, dose stack memory allocator would be overkill? – Ali New Sep 07 '20 at 00:58

0 Answers0