Skip to content

Commit dfe17e4

Browse files
committed
Be consistent about deallocating memory allocated from C++ in C++
Fixes a crash on Windows due to mismatched allocators.
1 parent 1da2631 commit dfe17e4

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ SWIFT_NAME("BridgedASTContext.langOptsGetCompilerVersion(self:_:)")
246246
SwiftInt BridgedASTContext_langOptsGetCompilerVersion(BridgedASTContext cContext,
247247
SwiftInt* _Nullable * _Nonnull cComponents);
248248

249+
/* Deallocate an array of Swift int values that was allocated in C++. */
250+
void deallocateIntBuffer(SwiftInt * _Nullable cComponents);
251+
249252
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedCanImportVersion : size_t {
250253
CanImportUnversioned,
251254
CanImportVersion,

lib/AST/ASTBridging.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ namespace {
195195
}
196196
}
197197

198+
void deallocateIntBuffer(SwiftInt * _Nullable cComponents) {
199+
free(cComponents);
200+
}
201+
198202
SwiftInt BridgedASTContext_langOptsGetLanguageVersion(BridgedASTContext cContext,
199203
SwiftInt** cComponents) {
200204
auto theVersion = cContext.unbridged().LangOpts.EffectiveLanguageVersion;

lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ final class CompilerBuildConfiguration: BuildConfiguration {
123123
var bitWidthsBuf: UnsafeMutablePointer<SwiftInt>? = nil
124124
let count = ctx.langOptsGetTargetAtomicBitWidths(&bitWidthsBuf)
125125
let bitWidths = Array(UnsafeMutableBufferPointer(start: bitWidthsBuf, count: count))
126-
bitWidthsBuf?.deallocate()
126+
deallocateIntBuffer(bitWidthsBuf);
127127
return bitWidths
128128
}
129129

@@ -140,7 +140,7 @@ final class CompilerBuildConfiguration: BuildConfiguration {
140140
let version = VersionTuple(
141141
components: Array(UnsafeMutableBufferPointer(start: componentsBuf, count: count))
142142
)
143-
componentsBuf?.deallocate()
143+
deallocateIntBuffer(componentsBuf);
144144
return version
145145
}
146146

@@ -150,7 +150,7 @@ final class CompilerBuildConfiguration: BuildConfiguration {
150150
let version = VersionTuple(
151151
components: Array(UnsafeMutableBufferPointer(start: componentsBuf, count: count))
152152
)
153-
componentsBuf?.deallocate()
153+
deallocateIntBuffer(componentsBuf);
154154
return version
155155
}
156156
}

0 commit comments

Comments
 (0)