File tree 3 files changed +22
-3
lines changed
3 files changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -197,7 +197,7 @@ extension Cursor {
197
197
198
198
/// Returns the translation unit that a cursor originated from.
199
199
public var translationUnit : TranslationUnit {
200
- return TranslationUnit ( clang: clang_Cursor_getTranslationUnit ( asClang ( ) ) )
200
+ return TranslationUnit ( clang: clang_Cursor_getTranslationUnit ( asClang ( ) ) , owned : false )
201
201
}
202
202
203
203
/// Retrieves all the children of the provided cursor.
Original file line number Diff line number Diff line change @@ -126,9 +126,11 @@ public struct TranslationUnitSaveOptions: OptionSet {
126
126
127
127
public class TranslationUnit {
128
128
let clang : CXTranslationUnit
129
+ private let owned : Bool
129
130
130
- init ( clang: CXTranslationUnit ) {
131
+ init ( clang: CXTranslationUnit , owned : Bool ) {
131
132
self . clang = clang
133
+ self . owned = owned
132
134
}
133
135
134
136
@@ -162,6 +164,7 @@ public class TranslationUnit {
162
164
}
163
165
return unit!
164
166
}
167
+ self . owned = true
165
168
}
166
169
167
170
/// Creates a `TranslationUnit` by parsing the source code passed,
@@ -236,6 +239,7 @@ public class TranslationUnit {
236
239
}
237
240
238
241
self . clang = unit!
242
+ self . owned = true
239
243
}
240
244
241
245
/// Retrieve the cursor that represents the given translation unit.
@@ -471,6 +475,8 @@ public class TranslationUnit {
471
475
}
472
476
473
477
deinit {
474
- clang_disposeTranslationUnit ( clang)
478
+ if owned {
479
+ clang_disposeTranslationUnit ( clang)
480
+ }
475
481
}
476
482
}
Original file line number Diff line number Diff line change @@ -235,6 +235,19 @@ class ClangTests: XCTestCase {
235
235
XCTFail ( " \( error) " )
236
236
}
237
237
}
238
+
239
+ func testDisposeTranslateUnit( ) {
240
+ do {
241
+ let filename = " input_tests/init-ast.c "
242
+ let unit = try TranslationUnit ( filename: filename)
243
+ let cursor = unit. cursor
244
+ for _ in 0 ..< 2 {
245
+ _ = cursor. translationUnit
246
+ }
247
+ } catch {
248
+ XCTFail ( " \( error) " )
249
+ }
250
+ }
238
251
239
252
static var allTests : [ ( String , ( ClangTests ) -> ( ) throws -> Void ) ] {
240
253
return [
You can’t perform that action at this time.
0 commit comments