fschuetz04 / simcpp20 Goto Github PK
View Code? Open in Web Editor NEWDiscrete-event simulation in C++20 using coroutines
License: MIT License
Discrete-event simulation in C++20 using coroutines
License: MIT License
Do you know anything more about the memory leaks?
I would like to use your code but don't really know, how much I should worry about the leaks. Do they only happen at program termination, after which the OS will reclaim everything regardless or can they accumulate?
In the end, if I use your project, I'm prepared to find and fix the leaks but it's rather opaque to me right now.
If an event awaited by a process is never triggered, the process coroutine will stay suspended until the program exits. This means that all events referenced by the process and the process are leaked. Example:
#include <cstdio>
#include "fschuetz04/simcpp20.hpp"
simcpp20::event<> process(simcpp20::simulation<> &sim) {
printf("[%.0f] start\n", sim.now());
co_await sim.event();
printf("[%.0f] end\n", sim.now());
}
int main() {
simcpp20::simulation<> sim;
process(sim);
sim.run();
}
In this case, the event created in process
is never triggered, so a memory leak occurs.
Just a suggestion for a possible enhancement (I'm sure you already thought of it, but just in case): It might be possible to also support yield events from coroutines.
I tried the latest changes, things seem to work, then I tried your example, 'value_process'.
I think before it displayed 42, now it crashes, somewhere in event::process, on a call on resume: promise->process_handle().resume()
.
There is a null pointer access violation, I guess that could be overcome by checking the 'address' of the handle, but I'm not sure it will entirely solve the problem.
Adding this:
if (!promise->process_handle().address())
continue;
in the for loop before checking the promise avoids the crash, but still does not work ok, as expected, nothing is displayed.
Hi!
I forked your repository (my only repository actually) and found that clang now works more or less out of the box, except for ping-pong, where clang apparently fails
to recognize some template magic. Maybe you understand more from the error message, if you're interested.
FAILED: examples/CMakeFiles/ping_pong.dir/ping_pong.cpp.o
ccache /usr/bin/clang++ -D_GLIBCXX_DEBUG -I/home/user/git/simcpp20/include -g -stdlib=libc++ -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -O0 -Werror -pedantic-errors -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wundef -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wsign-promo -Wstrict-overflow=5 -Wunused -Wno-error=unused-parameter -Wno-error=unused -Wno-error=unused-variable -Wno-error=unused-value -Wdelete-non-virtual-dtor -Wno-error=undef -Wunreachable-code -fcolor-diagnostics -Wno-error=unused-private-field -Wno-error=unknown-warning-option -std=c++20 -MD -MT examples/CMakeFiles/ping_pong.dir/ping_pong.cpp.o -MF examples/CMakeFiles/ping_pong.dir/ping_pong.cpp.o.d -o examples/CMakeFiles/ping_pong.dir/ping_pong.cpp.o -c /home/user/git/simcpp20/examples/ping_pong.cpp
In file included from /home/user/git/simcpp20/examples/ping_pong.cpp:6:
In file included from /home/user/git/simcpp20/include/fschuetz04/simcpp20.hpp:6:
In file included from /home/user/git/simcpp20/include/fschuetz04/simcpp20/simulation.hpp:16:
/home/user/git/simcpp20/include/fschuetz04/simcpp20/value_event.hpp:194:31: error: no matching constructor for initialization of 'ev_type'
casted_data->value_ = new Value(std::forward<Args>(args)...);
^ ~~~~~~~~~~~~~~~~~~~~~~~~
/home/user/git/simcpp20/include/fschuetz04/simcpp20/value_event.hpp:44:5: note: in instantiation of function template specialization 'simcpp20::value_event<ev_type>::set_value<simcpp20::value_event<ev_type> &>' requested here
set_value(std::forward<Args>(args)...);
^
/home/user/git/simcpp20/examples/ping_pong.cpp:21:17: note: in instantiation of function template specialization 'simcpp20::value_event<ev_type>::trigger<simcpp20::value_event<ev_type> &>' requested here
their_event.trigger(my_event);
^
/home/user/git/simcpp20/examples/ping_pong.cpp:10:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'simcpp20::value_event<ev_type>' to 'const ev_type' for 1st argument
struct ev_type {
^
/home/user/git/simcpp20/examples/ping_pong.cpp:10:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'simcpp20::value_event<ev_type>' to 'ev_type' for 1st argument
/home/user/git/simcpp20/examples/ping_pong.cpp:10:8: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
In file included from /home/user/git/simcpp20/examples/ping_pong.cpp:6:
In file included from /home/user/git/simcpp20/include/fschuetz04/simcpp20.hpp:6:
/home/user/git/simcpp20/include/fschuetz04/simcpp20/simulation.hpp:67:8: error: no matching member function for call to 'set_value'
ev.set_value(std::forward<Args>(args)...);
~~~^~~~~~~~~
/home/user/git/simcpp20/examples/ping_pong.cpp:28:25: note: in instantiation of function template specialization 'simcpp20::simulation<double>::timeout<ev_type, simcpp20::value_event<ev_type> &>' requested here
auto ping_event = sim.timeout<ev_type>(0, pong_event);
^
/home/user/git/simcpp20/include/fschuetz04/simcpp20/value_event.hpp:192:36: note: candidate template ignored: substitution failure [with Args = <simcpp20::value_event<ev_type> &>]
template <typename... Args> void set_value(Args &&...args) const {
^
2 errors generated.
ninja: build stopped: subcommand failed.
Apparently, when using the value_event
class the system incurs in a double free error when exiting the program (i.e., all processes have been stopped).
value_process(64525,0x100f6c580) malloc: *** error for object 0x600001844050: pointer being freed was not allocated
Unfortunately I cannot go too much in depth for debugging, but it seems that the error is related to the following destructor (and possibly to this phenomenon) in event.hpp
:
class data {
public:
...
/// Destructor.
virtual ~data() {
for (auto &handle : handles_) {
handle.destroy();
}
}
}
A possible workaround might be to use a std::suspend_always
instead of a std::suspend_never
for the final_suspend
method, however I am not able to understand the implications for the simulation.
My system is a MacOS 12.3.1 running on Arm M1 Pro. The code has been compiled with g++-11 (Homebrew GCC 11.2.0_3) 11.2.0
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.