vendredi 6 novembre 2020

EXC_BAD_ACCESS for shared

I'm trying to create a flow where each node might have an optional counter for debugging purposes.

The source of the flow will receive a counter that will be moved and children will reference that counter to make sure we can point to the same item.

Node.h

struct Node {

  std::shared_ptr<std::atomic_int> _counter;  // on iOS this causes EXC_BAD_ACCESS (code=1)
  
  Node() -> default;
  
  Node(std::shared_ptr<std::atomic_int> counter);

  Node(Node other);
};

Node.ccp

  Node::Node(std::shared_ptr<std::atomic_int> counter) : _counter(std::move(counter)) {}

  Node::Node(Node other) : _counter(other.counter) {}

The creator of the counter, which is a sort of "Node manager" will do something like the following:

auto counter = DEBUG ? std::make_shared<std::atomic_int>(1) : nullptr;
Node a = Node {counter};

queue->add([a]() {
  Node b = Node {a};
})

(I've omitted to keep it simple but you can expect the counter to be increased/decreased upon visitation of the node)

I've tried running some unit tests + the code on android and everything works perfectly fine. On iOS, for some reason the same code ends up with EXC_BAD_ACCESS (code=1, address=0x0) and I have no idea how to debug it.

Anyone with any idea? Are there better solutions for this type of usecase? Maybe more efficient and simpler?

Basically all the above is a conversion of what in Java I could achieve with an AtomicInteger in the following way:

class Node {

  @Nullable
  final AtomicInteger counter;

  Node(@Nullable AtomicInteger counter) {
    this.counter = counter;
  }

  Node(Node parent) {
    this.counter = parent.counter;
  }
}

Aucun commentaire:

Enregistrer un commentaire