nitro icon indicating copy to clipboard operation
nitro copied to clipboard

fix: Add `FastVectorCopy` again but avoid using already-deleted `data()` for fast-copying primitive vectors

Open mrousavy opened this issue 2 months ago • 1 comments

Previously we passed the std::vector<double> to void* get_data_vector_double(const std::vector<double&).

As a C++ developer, you might expect this to return a valid void* to the given std::vector<double>.

However because Swift does not really support const-ref types, it copies the std::vector<double> into the function arguments slot of get_data_vector_double, which then returns the void* to not our vector, but the temporary copy that was made. This is super stupid because obviously that temporary copy is no longer valid after that method returns, so we were reading corrupted memory!

There is now a test that covered exactly this case (array of numbers (primitives) inside a struct, which was again packaged into an array).

The solution to this is now ContiguousArray+fastCopy(vector: ...) which is written entirely in Swift.

I think it's also worth noting that accessing any prop on a struct runs the getter each time, so e.g. this:

let someNumbers = myStructFromJS.someNumbers
let someNumbers2 = myStructFromJS.someNumbers

This creates 2 array copies, and someNumbers != someNumbers2.

I'm not sure if we want to cache this, we'll see.

  • Fixes https://github.com/mrousavy/nitro/issues/977

mrousavy avatar Oct 24 '25 08:10 mrousavy

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
nitro-docs Skipped Skipped Oct 24, 2025 10:08am

vercel[bot] avatar Oct 24 '25 08:10 vercel[bot]