@@ -57,6 +57,15 @@ type StringDecoder{S<:IO} <: IO
57
57
skip:: Int
58
58
end
59
59
60
+ # This is called during GC, just make sure C memory is returned, don't throw errors
61
+ function finalize (s:: Union{StringEncoder, StringDecoder} )
62
+ if s. cd != C_NULL
63
+ iconv_close (s. cd)
64
+ s. cd = C_NULL
65
+ end
66
+ nothing
67
+ end
68
+
60
69
function iconv! (cd:: Ptr{Void} , inbuf:: Vector{UInt8} , outbuf:: Vector{UInt8} ,
61
70
inbufptr:: Ref{Ptr{UInt8}} , outbufptr:: Ref{Ptr{UInt8}} ,
62
71
inbytesleft:: Ref{Csize_t} , outbytesleft:: Ref{Csize_t} )
@@ -135,7 +144,7 @@ function StringEncoder(ostream::IO, to::ASCIIString, from::ASCIIString="UTF-8")
135
144
s = StringEncoder (ostream, cd, inbuf, outbuf,
136
145
Ref {Ptr{UInt8}} (pointer (inbuf)), Ref {Ptr{UInt8}} (pointer (outbuf)),
137
146
Ref {Csize_t} (0 ), Ref {Csize_t} (BUFSIZE))
138
- finalizer (s, close )
147
+ finalizer (s, finalize )
139
148
s
140
149
end
141
150
@@ -157,11 +166,10 @@ function flush(s::StringEncoder)
157
166
end
158
167
159
168
function close (s:: StringEncoder )
160
- s. cd == C_NULL && return s
161
169
flush (s)
162
170
iconv_reset! (s)
163
- iconv_close (s . cd)
164
- s . cd = C_NULL
171
+ # Make sure C memory/resources are returned
172
+ finalize (s)
165
173
# flush() wasn't able to empty input buffer, which cannot happen with correct data
166
174
s. inbytesleft[] == 0 || error (" iconv error: incomplete byte sequence at end of input" )
167
175
end
@@ -188,7 +196,7 @@ function StringDecoder(istream::IO, from::ASCIIString, to::ASCIIString="UTF-8")
188
196
s = StringDecoder (istream, cd, inbuf, outbuf,
189
197
Ref {Ptr{UInt8}} (pointer (inbuf)), Ref {Ptr{UInt8}} (pointer (outbuf)),
190
198
Ref {Csize_t} (0 ), Ref {Csize_t} (BUFSIZE), 0 )
191
- finalizer (s, close )
199
+ finalizer (s, finalize )
192
200
s
193
201
end
194
202
@@ -221,10 +229,10 @@ function eof(s::StringDecoder)
221
229
end
222
230
223
231
function close (s:: StringDecoder )
224
- s . cd == C_NULL && return s
225
- iconv_close (s . cd)
226
- s . cd = C_NULL
227
- # fill_buffer !() wasn't able to empty input buffer, which cannot happen with correct data
232
+ iconv_reset! (s)
233
+ # Make sure C memory/resources are returned
234
+ finalize (s)
235
+ # iconv_reset !() wasn't able to empty input buffer, which cannot happen with correct data
228
236
s. inbytesleft[] == 0 || error (" iconv error: incomplete byte sequence at end of input" )
229
237
end
230
238
0 commit comments