Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Inconsistent Output in Demo #26

Closed
Sfinx opened this issue Nov 8, 2021 · 10 comments
Closed

[BUG] Inconsistent Output in Demo #26

Sfinx opened this issue Nov 8, 2021 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@Sfinx
Copy link

Sfinx commented Nov 8, 2021

The first event is never printed. This is because following do not work:

first_event evt{ "hello from dummy event" };
evt_bus.fire_event(&evt);

but following works:

evt_bus.fire_event(third_event{ 13.0 });

And test SIGSEGV's at aarch64 in DeregisterWhileDispatching

@DeveloperPaul123
Copy link
Owner

Hi, thank you for the bug report.

Can you provide the entire error output as well as info on your configuration? What operating system are you using?

@Sfinx
Copy link
Author

Sfinx commented Nov 8, 2021

Stock Ubuntu 20.04. The demo outputs the following:

I just do stuff when a third_event is fired.
my third event handler: 13
I just do stuff when a third_event is fired.
third event handler says: oh boy...
event: oh boy...
Firing other event
second other event handler says: 2 hello there
first other event handler says: hello there
Firing other event
second other event handler says: 2 hello there
I just do stuff when a third_event is fired.
callback count: 1
handler count: 6
removing handlers...

So no first event message

@DeveloperPaul123
Copy link
Owner

What compiler are you using?

@DeveloperPaul123 DeveloperPaul123 added bug Something isn't working and removed investigate labels Nov 9, 2021
@DeveloperPaul123
Copy link
Owner

@Sfinx I believe I fixed the issue in 8375a7d. Can you give it a try?

@DeveloperPaul123 DeveloperPaul123 changed the title demo is buggy in latest git [BUG] Inconsistent Output in Demo Nov 9, 2021
@Sfinx
Copy link
Author

Sfinx commented Nov 9, 2021

As already said - this works when pushing all the object to stack - but it is still do not works when passing by reference. The correct fix must work for both. As pushing the only object is somewhat ugly.

P.S. I'm using gcc-11 and this needed to compile:

diff --git a/eventbus/include/eventbus/event_bus.hpp b/eventbus/include/eventbus/event_bus.hpp
index 56c665d..7165687 100644
--- a/eventbus/include/eventbus/event_bus.hpp
+++ b/eventbus/include/eventbus/event_bus.hpp
@@ -10,6 +10,7 @@
 #include <atomic>
 #include <thread>
 #include <utility>
+#include <mutex>
 
 namespace dp
 {

And there is compile warnings:

n file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp: In function ‘int main()’:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~

@Sfinx
Copy link
Author

Sfinx commented Nov 9, 2021

And there is design problem concerning return value of register_handler() - assuming you have the class:

class test {
 public:
  test() {
    auto reg = evt_bus.register_handler();
  }
 ~test() {
    // how to remove the handler ???
 }
};

The first problem is that if reg var is scope-auto-deleted - the handler will never invoke. I think that scope problems of return value can never affect the exact function. This is confusing behavior at least.
The second problem is that standard approach initializing the reg reference in constructor do not work too so no way to remove the handler on-demand. The only way is to use "static" for reg which is really ugly too.

@DeveloperPaul123
Copy link
Owner

As already said - this works when pushing all the object to stack - but it is still do not works when passing by reference. The correct fix must work for both. As pushing the only object is somewhat ugly.

In the demo, it was not passing by reference, it was passing a pointer of first_event to the fire_event function. At this time, I don't really want to support pointers as event types. This is why the event was not being called, because in the demo it was passing first_event* as the event type, and no registration handlers were available that handle events of that type.

P.S. I'm using gcc-11 and this needed to compile:

diff --git a/eventbus/include/eventbus/event_bus.hpp b/eventbus/include/eventbus/event_bus.hpp
index 56c665d..7165687 100644
--- a/eventbus/include/eventbus/event_bus.hpp
+++ b/eventbus/include/eventbus/event_bus.hpp
@@ -10,6 +10,7 @@
 #include <atomic>
 #include <thread>
 #include <utility>
+#include <mutex>

Noted and fixed.

And there is compile warnings:

n file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp: In function ‘int main()’:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:229:19: warning: ‘handle’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |                 : handle_(handle), event_bus_(bus)
      |                   ^~~~~~~~~~~~~~~
In file included from eventbus/demo/main.cpp:4:
eventbus/eventbus/include/eventbus/event_bus.hpp:47:37: note: ‘handle’ was declared here
   47 |                         const void* handle;
      |                                     ^~~~~~

I do not see these warnings with GCC 11 on Ubuntu 20.04. Have you changed the compilation flags?

@Sfinx
Copy link
Author

Sfinx commented Nov 9, 2021

CFLAGS are "-Wall -O2"

@DeveloperPaul123
Copy link
Owner

And there is design problem concerning return value of register_handler() - assuming you have the class:

class test {
 public:
  test() {
    auto reg = evt_bus.register_handler();
  }
 ~test() {
    // how to remove the handler ???
 }
};

The first problem is that if reg var is scope-auto-deleted - the handler will never invoke. I think that scope problems of return value can never affect the exact function. This is confusing behavior at least. The second problem is that standard approach initializing the reg reference in constructor do not work too so no way to remove the handler on-demand. The only way is to use "static" for reg which is really ugly too.

The handler_registration class is move constructible, so you can in fact initialize it in the constructor like so:

class test_class {
    dp::handler_registration reg;
public:
    test_class(dp::event_bus& bus) : reg(std::move(bus.register_handler<first_event>([](const first_event& evt) {
        std::cout << "test class: " << evt.message << "\n";
        }))) {

    }
};

The registration will automatically de-register when test_class goes out of scope.

@Sfinx
Copy link
Author

Sfinx commented Nov 9, 2021

Thanks, will try

@Sfinx Sfinx closed this as completed Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants