Skip to content

Commit

Permalink
doc: warn about SuppressDestruct() (#926)
Browse files Browse the repository at this point in the history
Not sure how this ever made its way into the API, but people should *never* use this.
  • Loading branch information
addaleax committed Mar 17, 2021
2 parents 71494a4 + 69d0d98 commit 87b7aae
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Holds a counted reference to a [`Napi::Value`](value.md) object; initially a wea

The referenced `Napi::Value` is not immediately destroyed when the reference count is zero; it is merely then eligible for garbage-collection if there are no other references to the `Napi::Value`.

`Napi::Reference` objects allocated in static space, such as a global static instance, must call the `SuppressDestruct` method to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid.
`Napi::Reference` objects allocated in static space, such as a global static instance, must call the `SuppressDestruct` method to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. Avoid using this if at all possible.

The following classes inherit, either directly or indirectly, from `Napi::Reference`:

Expand Down Expand Up @@ -109,3 +109,5 @@ void Napi::Reference::SuppressDestruct();
```

Call this method on a `Napi::Reference` that is declared as static data to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid.

Avoid using this if at all possible. If you do need to use static data, **MAKE SURE** to warn your users that your addon is **NOT** threadsafe.
8 changes: 5 additions & 3 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1175,9 +1175,11 @@ namespace Napi {
void Reset();
void Reset(const T& value, uint32_t refcount = 0);

// Call this on a reference that is declared as static data, to prevent its destructor
// from running at program shutdown time, which would attempt to reset the reference when
// the environment is no longer valid.
// Call this on a reference that is declared as static data, to prevent its
// destructor from running at program shutdown time, which would attempt to
// reset the reference when the environment is no longer valid. Avoid using
// this if at all possible. If you do need to use static data, MAKE SURE to
// warn your users that your addon is NOT threadsafe.
void SuppressDestruct();

protected:
Expand Down

0 comments on commit 87b7aae

Please sign in to comment.