CommandLineOption callback can easily go out of scope
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
MirAL |
Triaged
|
Undecided
|
Unassigned |
Bug Description
The `CommandLineOption` class takes a callback as its first constructor parameter, defining what should be done with the data received from the given command line flag.
However, this callback is not used directly. Instead, the CommandLineOption class stores a wrapper callback, which invokes the original internally. In the `operator(
In consequence, if the `CommandLineOption` instance goes out of scope before the init (or pre-init) callbacks are actually invoked, the inner callback will no longer be valid and a segfault will result.
This makes it to e.g. use `CommandLineOption` under the hood of other classes which may wish to encapsulate both the addition of a command line option and what to do with the result.
As an example of the use-case described, see e.g. https:/ /github. com/WebDrake/ unity-system- compositor/ pull/1/ commits/ 83f95752717168d 49c4b8d1d67ec7d 111fb1c50a.
Here it was possible to work around the scope issue by storing the `CommandLineOption` instance inside a longer-lived class, but it would be nice if such workarounds were not necessary.