Skip to content

Commit

Permalink
The calls to gi.array_operator_index already return a copy of the
Browse files Browse the repository at this point in the history
Variant, so we do not need to call the constructor that makes a copy -
introduce a non-copy constructor.

Fixes the first leak in #513
  • Loading branch information
migueldeicaza committed Aug 17, 2024
1 parent 17bef0f commit d1d6ca4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Sources/SwiftGodot/Core/GArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ extension GArray {

public subscript (index: Int) -> Variant {
get {
// array_operator_index returns a copy of the Variant, we own it.
guard let ret = gi.array_operator_index (&content, Int64 (index)) else {
return Variant()
}
let ptr = ret.assumingMemoryBound(to: Variant.ContentType.self)
return Variant(fromContent: ptr.pointee)
return Variant(fromContentNoCopy: ptr.pointee)
}
set {
guard let ret = gi.array_operator_index (&content, Int64 (index)) else {
Expand Down
7 changes: 6 additions & 1 deletion Sources/SwiftGodot/Variant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public class Variant: Hashable, Equatable, CustomDebugStringConvertible {
gi.variant_new_copy (&content, &copy)
}

/// Initializes from the raw contents of another Variant, this will not make a copy of the variant contents
init (fromContentNoCopy: ContentType) {
content = fromContentNoCopy
}

/// Initializes from the raw contents of another Variant, this will make a copy of the variant contents
init (fromContentPtr: inout ContentType) {
gi.variant_new_copy (&content, &fromContentPtr)
Expand Down Expand Up @@ -247,7 +252,7 @@ public class Variant: Hashable, Equatable, CustomDebugStringConvertible {
if valid == 0 || oob != 0 {
return nil
}
return Variant(fromContent: _result)
return Variant(fromContentNoCopy: _result)
}
set {
guard let newValue else {
Expand Down

0 comments on commit d1d6ca4

Please sign in to comment.