Skip to content

Release JSPropertyNameArrayRef. Retain string. #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,12 @@ impl Iterator for JSObjectPropertyNameIter {
if self.idx < unsafe { sys::JSPropertyNameArrayGetCount(self.raw) } {
let name = unsafe { sys::JSPropertyNameArrayGetNameAtIndex(self.raw, self.idx) };
self.idx += 1;
Some(JSString { raw: name })
// GetNameAtIndex doesn't retain the name, so since we're going to release it
// when we release the property name array, but this JSString may outlive that,
// we need to retain the string to keep it alive.
Some(JSString {
raw: unsafe { sys::JSStringRetain(name) },
})
} else {
None
}
Expand All @@ -462,6 +467,12 @@ impl Iterator for JSObjectPropertyNameIter {
}
}

impl Drop for JSObjectPropertyNameIter {
fn drop(&mut self) {
unsafe { sys::JSPropertyNameArrayRelease(self.raw) }
}
}

#[cfg(test)]
mod tests {
use crate::{JSContext, JSException, JSValue};
Expand Down