marian-nmt / marian-dev

Fast Neural Machine Translation in C++ - development repository

Home Page:https://marian-nmt.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

intrusive_ptr not threadsafe

kpu opened this issue · comments

The reference counter is a bare size_t

#define ENABLE_INTRUSIVE_PTR(type) \
size_t references_{0}; \

and the pointer type is a bare pointer:

If two threads destroy their references simultaneously, then both enter this function:

inline friend void intrusivePtrRelease(type* x) { \
if(x != 0 && --x->references_ == 0) { \
delete x; \
x = 0; \
} \
} \

It's possible both threads will read x != 0 and x->references_ == 2 then commit 1 back and leak the object.

Aside: this line doesn't do anything since it's in a function that takes type* x