@@ -16,10 +16,9 @@ import CDispatch
16
16
// importer via Dispatch.apinote when the platform has Objective-C support
17
17
18
18
public class DispatchObject {
19
- // TODO: add deinit method to invoke dispatch_release on wrapped()
20
19
21
20
internal func wrapped( ) -> dispatch_object_t {
22
- assert ( false , " should be override in subclass " )
21
+ fatalError ( " should be overriden in subclass " )
23
22
}
24
23
25
24
public func setTarget( queue: DispatchQueue ) {
@@ -43,14 +42,18 @@ public class DispatchObject {
43
42
public class DispatchGroup : DispatchObject {
44
43
internal let __wrapped : dispatch_group_t ;
45
44
46
- internal override func wrapped( ) -> dispatch_object_t {
45
+ final internal override func wrapped( ) -> dispatch_object_t {
47
46
return unsafeBitCast ( __wrapped, to: dispatch_object_t. self)
48
47
}
49
48
50
49
public override init ( ) {
51
50
__wrapped = dispatch_group_create ( )
52
51
}
53
52
53
+ deinit {
54
+ _swift_dispatch_release ( wrapped ( ) )
55
+ }
56
+
54
57
public func enter( ) {
55
58
dispatch_group_enter ( __wrapped)
56
59
}
@@ -63,19 +66,23 @@ public class DispatchGroup : DispatchObject {
63
66
public class DispatchSemaphore : DispatchObject {
64
67
internal let __wrapped : dispatch_semaphore_t ;
65
68
66
- internal override func wrapped( ) -> dispatch_object_t {
69
+ final internal override func wrapped( ) -> dispatch_object_t {
67
70
return unsafeBitCast ( __wrapped, to: dispatch_object_t. self)
68
71
}
69
72
70
73
public init ( value: Int ) {
71
74
__wrapped = dispatch_semaphore_create ( value)
72
75
}
76
+
77
+ deinit {
78
+ _swift_dispatch_release ( wrapped ( ) )
79
+ }
73
80
}
74
81
75
82
public class DispatchIO : DispatchObject {
76
83
internal let __wrapped : dispatch_io_t
77
84
78
- internal override func wrapped( ) -> dispatch_object_t {
85
+ final internal override func wrapped( ) -> dispatch_object_t {
79
86
return unsafeBitCast ( __wrapped, to: dispatch_object_t. self)
80
87
}
81
88
@@ -98,6 +105,10 @@ public class DispatchIO : DispatchObject {
98
105
__wrapped = queue
99
106
}
100
107
108
+ deinit {
109
+ _swift_dispatch_release ( wrapped ( ) )
110
+ }
111
+
101
112
public func barrier( execute: ( ) -> ( ) ) {
102
113
dispatch_io_barrier ( self . __wrapped, execute)
103
114
}
@@ -118,7 +129,7 @@ public class DispatchIO : DispatchObject {
118
129
public class DispatchQueue : DispatchObject {
119
130
internal let __wrapped : dispatch_queue_t ;
120
131
121
- internal override func wrapped( ) -> dispatch_object_t {
132
+ final internal override func wrapped( ) -> dispatch_object_t {
122
133
return unsafeBitCast ( __wrapped, to: dispatch_object_t. self)
123
134
}
124
135
@@ -134,6 +145,10 @@ public class DispatchQueue : DispatchObject {
134
145
__wrapped = queue
135
146
}
136
147
148
+ deinit {
149
+ _swift_dispatch_release ( wrapped ( ) )
150
+ }
151
+
137
152
public func sync( execute workItem: @noescape ( ) -> ( ) ) {
138
153
dispatch_sync ( self . __wrapped, workItem)
139
154
}
@@ -146,13 +161,17 @@ public class DispatchSource : DispatchObject,
146
161
DispatchSourceWrite {
147
162
internal let __wrapped : dispatch_source_t
148
163
149
- internal override func wrapped( ) -> dispatch_object_t {
164
+ final internal override func wrapped( ) -> dispatch_object_t {
150
165
return unsafeBitCast ( __wrapped, to: dispatch_object_t. self)
151
166
}
152
167
153
168
internal init ( source: dispatch_source_t ) {
154
169
__wrapped = source
155
170
}
171
+
172
+ deinit {
173
+ _swift_dispatch_release ( wrapped ( ) )
174
+ }
156
175
}
157
176
158
177
#if HAVE_MACH
@@ -295,3 +314,6 @@ internal enum _OSQoSClass : UInt32 {
295
314
}
296
315
}
297
316
}
317
+
318
+ @_silgen_name ( " _swift_dispatch_release " )
319
+ internal func _swift_dispatch_release( _ obj: dispatch_object_t ) -> Void
0 commit comments