Open AnthonyVH opened 6 years ago
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.
Sure thing, I'll try any new experiments against the experimental API.
Using a single randv<int>
to populate the vector would definitely be more efficient in this case.
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.