You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a crave::randv object is constructed with a non-nullptr value for the parent pointer, it registers itself with said parent (through randv_base::add_base_child). However, upon destruction it doesn't deregister itself from the parent. This causes dangling pointers and the undefined behavior that comes with it when rand_base::next() is called on the parent (so far I've seen SIGSERV and "abstract virtual called").
Furthermore, since there's copy constructors defined for the randv_base and randv types, move construction and assignment are both implicitly disabled. Just "thinking out loud", it seems to me that when a parent class is moved, it should be able to call some sort of "move_parent()" on the randv child objects. Such move_parent functionality would be the equivalent of remove_base_child (which currently doesn't exist) followed by add_base_child. This move_parent could then also be called by a user's custom objects where appropriate.
Similar things can most likely be said about rand_obj and its add_obj_child function (although I didn't check this thoroughly yet).
Below is a minimalistic example that triggers UB through derefencing pointers to destructed objects. I know crave::rand_vec exists, but I don't see why this example using std::vector shouldn't be able to work if the above issues are solved.
We did not intend to support manipulation of the randv/rand_obj object tree from user code, but it would have been useful to detach objects on their destruction to prevent such unwanted UBs. May I ask you to continue your evaluation on the experimental API (which does support some form of RAII if I remember correctly) since the 2.0 one is going to be deprecated soon?
As for your example, I would just use a vector<int> and a single randv<int> to fill the vector with generated values in a loop. This also helps to avoid constructing/deleting multiple randv at run-time.
When a
crave::randv
object is constructed with a non-nullptr
value for theparent
pointer, it registers itself with said parent (throughrandv_base::add_base_child
). However, upon destruction it doesn't deregister itself from the parent. This causes dangling pointers and the undefined behavior that comes with it whenrand_base::next()
is called on the parent (so far I've seen SIGSERV and "abstract virtual called").Furthermore, since there's copy constructors defined for the
randv_base
andrandv
types, move construction and assignment are both implicitly disabled. Just "thinking out loud", it seems to me that when a parent class is moved, it should be able to call some sort of "move_parent()" on therandv
child objects. Suchmove_parent
functionality would be the equivalent ofremove_base_child
(which currently doesn't exist) followed byadd_base_child
. Thismove_parent
could then also be called by a user's custom objects where appropriate.Similar things can most likely be said about
rand_obj
and itsadd_obj_child
function (although I didn't check this thoroughly yet).Below is a minimalistic example that triggers UB through derefencing pointers to destructed objects. I know
crave::rand_vec
exists, but I don't see why this example usingstd::vector
shouldn't be able to work if the above issues are solved.The text was updated successfully, but these errors were encountered: