Lifetime of VtArrays

I work on a C++ geometry library where some input mesh information may come from USD. For arrays that define the mesh topology as well as those that describe attributes, instead of copying the information, we can optionally use non-owning structures which are initialized with buffer pointer (and size). When dealing with USD we’re unsure about the life span of things. More concretely, let’s say, if I take a VtArray that contains the data of an attribute and want to pass its buffer address as a constant pointer in some constructions, then would holding on to the VtArray itself be sufficient for ensuring that the data remains valid. It was suggested that we need to hold on to the USD stage for this. A definitive answer would be a great help.

[Edit] I mistakenly thought that the crate mechanism was responsible for lifetime management of the underlying storage, but in fact they cooperate under the hood so that VtArray memory is safe even if the stage is released.

That’s not correct. You neither have to ensure that the stage stays alive nor do some kind of operation to cause a copy-on-write detach. VtArray behaves as a regular data type and its data should be valid for its entire lifetime.

So to answer the original question, you can hand out the underlying pointer so long as you keep the original VtArray alive, and do not invoke any non-const member functions on it, which could cause it to copy-on-write detach from shared data.