Recursion over a mutable binary tree: `already borrowed: BorrowMutError` - memory-management

I'm beginning with a Vec of sorted nodes, then using this sorting to link these nodes together in a binary tree and then returning the base struct
// Test name
#[derive(Clone)]
struct Struct {
parent: Option<Rc<RefCell<Struct>>>,
superscript: Option<Rc<RefCell<Struct>>>,
subscript: Option<Rc<RefCell<Struct>>>,
height: u32,
center: u32,
symbols: VecDeque<u8>
}
Ending up with a binary tree formed by the above Structs. At this point, these Structs are uniquely owned, so I think I could convert from using Rc<RefCell<Struct>> to RefCell<Struct> (think Box<Struct> doesn't work due to internal mutability?), but I'm not sure how or if this helps with the problem I'm encountering.
After this, I need to iterate in a novel manner through the Structs, and mutate the various symbols belonging to the various Structs throughout the recursion, via calling .pop_front().
My current implementation doing this leads to various instances of thread 'main' panicked at 'already borrowed: BorrowMutError'.
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=636c93088f5a431d0d430d42283348f3
The function for it (please excuse the convoluted logic):
fn traverse_scripts(row: Rc<RefCell<Struct>>) {
if let Some(superscript_row) = &row.borrow().superscript {
if let Some(superscript_symbol) = superscript_row.borrow().symbols.front() {
if let Some(current_row_symbol) = row.borrow().symbols.front() {
if superscript_symbol < current_row_symbol {
println!("^{{{}",*superscript_symbol);
superscript_row.borrow_mut().symbols.pop_front();
traverse_scripts(Rc::clone(superscript_row));
}
}
else {
println!("^{{{}",*superscript_symbol);
superscript_row.borrow_mut().symbols.pop_front();
traverse_scripts(Rc::clone(superscript_row));
}
}
}
if let Some(subscript_row) = &row.borrow().subscript {
if let Some(subscript_symbol) = subscript_row.borrow().symbols.front() {
if let Some(current_row_symbol) = row.borrow().symbols.front() {
if subscript_symbol < current_row_symbol {
print!("_{{{}",*subscript_symbol);
subscript_row.borrow_mut().symbols.pop_front();
traverse_scripts(Rc::clone(subscript_row));
}
}
else {
print!("_{{{}",*subscript_symbol);
subscript_row.borrow_mut().symbols.pop_front();
traverse_scripts(Rc::clone(subscript_row));
}
}
}
if let Some(current_row_symbol) = row.borrow().symbols.front() {
if let Some(parent_row) = &row.borrow().parent {
if let Some(parent_symbol) = parent_row.borrow().symbols.front() {
if current_row_symbol < parent_symbol {
print!(" {}",*current_row_symbol);
row.borrow_mut().symbols.pop_front();
traverse_scripts(Rc::clone(&row));
}
}
}
else {
print!(" {}",*current_row_symbol);
row.borrow_mut().symbols.pop_front();
traverse_scripts(Rc::clone(&row));
}
}
if let Some(parent_row) = &row.borrow().parent {
if let Some(parent_symbol) = parent_row.borrow().symbols.front() {
print!("}} {}",*parent_symbol);
row.borrow_mut().symbols.pop_front();
traverse_scripts(Rc::clone(parent_row));
} else {
print!("}}");
traverse_scripts(Rc::clone(parent_row));
}
}
}
I've considered using Arc<Mutex<Struct>> instead for the traversal, but given its not multi-threaded I don't think it's neccessary?
I imagine I might be missing a relatively simple idea here, I would really appreciate any help.
If I've missed anything in my question please drop a comment and I'll try to add it.

When you call borrow or borrow_mut on a RefCell, a guard object (Ref or RefMut) is created that grants access to the inner value for as long as it exists. This guard will lock the RefCell until it goes out of scope and is destroyed. Let's look at a portion of traverse_scripts:
if let Some(superscript_row) = &row.borrow().superscript { // row is borrowed
if let Some(superscript_symbol) = superscript_row.borrow().symbols.front() { // superscript_row is borrowed
if let Some(current_row_symbol) = row.borrow().symbols.front() { // row is borrowed again
if superscript_symbol < current_row_symbol {
println!("^{{{}", *superscript_symbol);
superscript_row.borrow_mut().symbols.pop_front(); // superscript_row is borrowed mutably (ERROR)
traverse_scripts(Rc::clone(superscript_row)); // recursive call while row and superscript_row are borrowed (ERROR)
}
} else {
println!("^{{{}", *superscript_symbol);
superscript_row.borrow_mut().symbols.pop_front(); // superscript_row is borrowed mutably (ERROR)
traverse_scripts(Rc::clone(superscript_row)); // recursive call while row and superscript_row are borrowed (ERROR)
} // row is no longer borrowed twice
} // superscript_row is no longer borrowed
} // row is no longer borrowed
In the first line, for example, row.borrow() returns a Ref<Struct>. This Ref can't be dropped immediately, because it's being borrowed during the if let body by superscript_row. So it stays alive that whole time -- until the final }.
This is a problem for recursively calling traverse_scripts because the Struct is borrowed during the entire recursive call. Any attempt to borrow the same Struct mutably deeper in the call stack will fail. (Borrowing it immutably does still work.)
In the second line superscript_row is borrowed. This has the same problem, but it also has a more immediate one: it's borrowed mutably later in the same function, even before hitting the recursive call. That borrow_mut can never succeed because superscript_row is always already borrowed at that point.
To fix both problems, we'll do two things:
Store each Ref or RefMut guard in a variable of its own and re-use that guard, instead of calling borrow() or borrow_mut() on the same variable again.
Before recursing, drop each of the still-living guards so that nothing is still borrowed inside the recursive call.
Here's what that section might look like rewritten:
{ // This scope will constrain the lifetime of row_ref
let row_ref = row.borrow();
if let Some(superscript_row) = &row_ref.superscript {
let mut child = superscript_row.borrow_mut(); // use borrow_mut here because we know we'll need it later
if let Some(superscript_symbol) = child.symbols.front() {
if let Some(current_row_symbol) = row_ref.symbols.front() {
if superscript_symbol < current_row_symbol {
println!("^{{{}", *superscript_symbol);
child.symbols.pop_front();
drop(child); // child is no longer needed, so drop it before recursing
// Since superscript_row borrows from row_ref, we must Rc::clone it before
// dropping row_ref so that we can still pass it to traverse_scripts.
let superscript_row = Rc::clone(superscript_row);
drop(row_ref); // row_ref is no longer needed, so drop it before recursing
traverse_scripts(superscript_row);
}
} else {
println!("^{{{}", *superscript_symbol);
child.symbols.pop_front();
// see comments earlier
drop(child);
let superscript_row = Rc::clone(superscript_row);
drop(row_ref);
traverse_scripts(superscript_row);
}
}
} // child is dropped here (if it wasn't already). superscript_row is no longer borrowed
} // row_ref is dropped here (if it wasn't already). row is no longer borrowed
Full-program playground.
This looks complicated because it is complicated. Traversing over a data structure while mutating it is a common source of bugs (in most languages, not just Rust). It looks like, in traverse_scripts at least, the only reason for needing mutation is to call pop_front on symbols, so if you can redesign the data structure such that only symbols is in a RefCell, you could do the traversal with only & references, which would be much easier. Another common approach is to write functions that return new data structures instead of mutating them in place.

Related

Calling parent struct inherited methods

I have a struct A that inherits from other classes (which I'm not allowed to change). Inside A and it's methods I can call inherited methods (lets say A_method(int i), for example) without problem but when I tried to write a nested struct (lets say In) and call A_method(int i) and there is were I'm stuck.
The initial code looks like this, and I can't change it, is some kind of college assigment.
#include "Player.hh"
struct A : public Player {
static Player* factory () {
return new A;
}
virtual void play () {
}
};
RegisterPlayer(PLAYER_NAME);
Then I tried this:
#include "Player.hh"
struct A : public Player {
static Player* factory () {
return new A;
}
//My code
struct In {
int x;
void do_smthing() {
A_method(x);
}
}
virtual void play () {
}
};
RegisterPlayer(PLAYER_NAME);
Ok, from a beginning I knew I could't do this, for In to see it's parent class it should have a pointer to it but In is a often instantiated object in my code and I wanted to avoid passing this constantly to a constructor so I tried this aproach:
#include "Player.hh"
struct A : public Player {
static Player* factory () {
return new A;
}
//My code
static struct Aux
A* ptr;
Aux(A* _p) { ptr = _p; }
} aux;
struct In {
int x;
void do_smthing() {
aux.ptr->A_method(x);
}
}
virtual void play () {
//the idea is to call do_smthing() here.
}
};
RegisterPlayer(PLAYER_NAME);
What I want to avoid (if possible) is something like this:
struct In {
int x;
A* ptr;
In (A* _p) : ptr(_p) {}
void do_smthing() {
ptr->A_method(x);
}
}
The main reason for this: I have more struct definitions and they they are instantiated multiple times through the rest of the (omitted) code, and I don't like the idea of seeing In(this) so many times.
I don't know if I'm completly missing something or what I want to do it's just not possible... Please ask for clarifications if necessary.
(Also, performance is kind of critical, my code will be tested with limited CPU time so I kinda have to avoid expensive approachs if possible. Using C++11)
There is no way you can skip passing the this pointer. Instead, you could create a helper function in A:
template <typename InnerType, typename ...Params>
InnerType makeInner(Params&&... params)
{
return InnerType(this, std::forward<Params>(params)...);
}
Then you can use
auto * a = A::factory();
auto inner = a->makeInner<A::In>();
I have some suggestions which are not directly related to you question but may help:
A::facotry() returns a std::unique_ptr<A> instead of raw pointer
Try to describe what problem you are trying to solve. I have a strong feeling that there can be a better design other than creating many nested structs.
I don't see passing a this pointer could have any impact on the performance. The more important thing is to identify the path that is latency-sensitive and move expensive operations out of those paths.

What caused the memory leak in this code?

I am inspecting the code that may cause memory leak. I know something is wrong with std::set.erase(this) and the destructor of SomeObject. So how to fix it?
class SomeObject;
////....
std::set<SomeObject*> managedObjects;
///.....
class SomeObject{
public:
SomeObject(){ managedObjects.insert(this); }
SomeObject(SomeObject&& S)/*move cter*/{ managedObjects.insert(this); }
virtual ~SomeObject() { managedObjects.erase(this); }
////....
};
////....
void clearAllObjects() {
for(auto p : managedObjects){
if(p){
delete p;
}
}
managedObjects.clear();
}
////....
When you delete inside clearAllObjects() it will result in managedObjects.erase(this) which is the same as managedObjects.erase(p).
This means that the internal iterator in the range based for-loop may be invalidated (I'm not sure). If it is, it'll try to do ++internal_iterator; on an invalid iterator - with undefined behavior as a result.
To be safe, you could copy the iterator and step that to the next in the set before doing erase.
Also note: There's no need to check if what you delete is a nullptr or not. It's mandated by the standard to have no effect if that's the case.
Example:
void clearAllObjects() {
for(auto pit = managedObjects.begin(); pit != managedObjects.end();) {
delete *pit++ // postfix ++ returns a copy of the old iterator
}
managedObjects.clear();
}
A side effect by having this managedObjects set is that you can't have automatic variables of SomeObject.
int main() {
SomeObject foo;
clearAllObjects(); // deletes the automatic object "foo" (not allowed)
} // <- the automatic object is destroyed here

RX - how to use it in a performant way?

I am trying to understand how to structure my program to use RX in a performant matter.My app has a vector of objects in the 3D world. each object occupied a box, and have a 'hit' stream, which represent a mouse hover over it. I thought of two options of how to structure:
Option 1
struct object_t
{
string name_;
box bounding_box_;
observable<bool> hit_;
};
struct scene_t
{
scene_t(observable<point> mouse) : hit_(hit(mouse))
{
add({"background", {/* ... */}, {}};
}
object_t& add(object_t o)
{
int object_index = objects_.size();
o.hit_ = hit_
.map([=](int index){ return index == object_index; })
.distinct_until_changed();
objects_.push_back(o);
return objects_.back();
}
//! given a stream of mouse points,
//! calculate on which object index(in objects_) the mouse is hover over.
//! 0 if its over the background.
observable<int> hit(observable<point> mouse);
using objects_t = std::vector<object_t>;
objects_t objects_;
observable<int> hit_
};
Option 2
struct object_t
{
string name_;
box bounding_box_;
void signal_hit(boot is_hit) { hit_.get_observer().on_next(is_hit); }
observable<bool> hit() const { return hit_.get_observable(); }
private:
subject<bool> hit_;
};
struct scene_t
{
scene_t(observable<point> mouse) : hit_(hit(mouse))
{
add({"background", {/* ... */}, {}};
hit_
.start_with(0)
.buffer(2, 1) // take two hits together, the current and the previos
.subscribe([this](std::vector<int> indices) {
objects_[indices[1]].signal_hit(false); // we leave this one
objects_[indices[0]].signal_hit(true); // and entering this one
});
}
object_t& add(object_t o)
{
objects_.push_back(o);
return objects_.back();
}
//! ... as above
};
Now the question is how to chain the result of the hit function to the object_t::hit stream. I see two ways:
Option 1, is fully functional, but very poorly performing, since for every mouse point, all objects stream will need to calculate their value.
Option 2. is not fully functional, as I use subject to push the values to the right stream, in an imperative way. but is very performant as only the right (two) object(s) hit stream get to fire.
Note:
The implementation is in rxcpp, but its general to any language we have RX in it, or general FRP paradigm, this is why I tagged rxjs\rx.net\frp etc.
thanks in advance :-)
If there is one source observable and N subscribers then there will have to be at least N computations every time the source emits. There is no way around that which I can think of.

Adding a condition to Amibroker code

This is my Amibroker code for a 2 bar swing chart, I need to add a condition that if price falls below the previous swing low in one bar, then to treat it as a two bar move. The problem I have is, holding the last swing low price variable to check against todays low. I have commented the problem lines in caps. What I have I thought would work but the condition is not showing up on the swing chart. Can someone tell me what I am doing wrong.Thanks.
_SECTION_BEGIN("2 day swing");
upBar = H>Ref(H,-1);
dnBar = L<Ref(L,-1);
HighBarPrice=LowBarPrice=Null;
inLong=inShort=upCount=dnCount=fupbar=fdnbar=0;
for( i=1; i<BarCount; i++ )
{
if(inLong==0 AND inShort==0)
{
if(upBar[i])
{
upCount=upCount+1;
if(upCount==2)
{
fupbar[i] = 1;
inLong=1;
dnCount=0;
}
}
if(dnBar[i])
{
dnCount=dnCount+1;
if(dnCount==2)
{
fdnbar[i] = 1;
inShort=1;
upCount=0;
}
}
if(inLong==1)
{
if(dnBar[i])
{
dnCount=dnCount+1;
if(L[i]<LowBarPrice) {dnCount=2;} //THIS IS THE PROBLEM
if(dnCount==2)
{
fdnbar[i]=1;
inShort=1;
if(upBar[i])
{
upCount=1;
}
else
{
upCount=0;
}
continue;
}
}
if(upBar[i]) {HighBarPrice=H[i];}
if(upBar[i] AND NOT dnBar[i]){ dnCount=0;}
}
if(inShort==1)
{
if(upBar[i])
{
upCount=upCount+1;
if(H[i]>HighBarPrice) {upCount=2;}
if(upCount==2)
{
fupbar[i]=1;
inLong=1;
if(dnBar[i])
{
dnCount=1;
}
else
{
dnCount=0;
}
continue;
}
}
if(dnBar[i]) {LowBarPrice=L[i];}// DOWN BAR IN SHORT SWING SHOULD GIVE NEW LOW
if(dnBar[i] AND NOT upBar[i]){ upCount=0;}
}
}
// Swing chart drawn here
_SECTION_END();
Your LowBarPrice doesn't have an array indexer on it. Also, you initialize it as null and it stays that way because you never assign any value to it after initialization. So technically, in your condition, you're saying, if L[i] < null.
Write your conditions outside the loop. That'll create an array that will hold your price until you reference it in the loop.
So, for example, initialize LowBarPrice like this:
LowBarPrice = ValueWhen(DownBar, Ref(L,-1));
Thereafter, you'll get the price when you reference it in the loop.
if(L[i] < LowBarPrice[i])
This article really helped me get my head around looping in AmiBroker. It might give some context around your issue. The part that relates specifically to your question is under the section "Array Indexing
http://www.amibrokerforum.com/index.php?topic=50.0

Immutable object pattern

I keep hearing that using immutable data structures and immutable objects is a good pattern for thread safety and preventing race conditions without needing to use semaphores, but I still can't think of a way to use them. Even for the most simple scenarios. For example
int a = 0;
Semaphore s = new Semaphore();
void thread1() {
s.wait();
if (a == 2) {
// do something
}
a = 1;
s.signal();
}
void thread2() {
s.wait();
if (a == 1) {
// do something
}
a = 2;
s.signal();
}
How can I change this code to use an immutable object for a?

Resources