@@ -118,7 +118,7 @@ static ERL_NIF_TERM zstd_nif_init_compression_stream(ErlNifEnv* env, int argc, c
118118 ZSTD_CStream * * pzcs ;
119119
120120 /* extract the stream */
121- if (!(enif_get_resource (env , argv [0 ], zstd_compression_stream_type , (void * * )& pzcs )))
121+ if (!(enif_get_resource (env , argv [0 ], zstd_compression_stream_type , (void * * )& pzcs )) && * pzcs != NULL )
122122 return enif_make_tuple2 (env , zstd_atom_error , zstd_atom_invalid );
123123
124124 /* extract the compression level if any */
@@ -211,7 +211,7 @@ static ERL_NIF_TERM zstd_nif_flush_compression_stream(ErlNifEnv* env, int argc,
211211 ZSTD_CStream * * pzcs ;
212212
213213 /* extract the stream */
214- if (!(enif_get_resource (env , argv [0 ], zstd_compression_stream_type , (void * * )& pzcs )))
214+ if (!(enif_get_resource (env , argv [0 ], zstd_compression_stream_type , (void * * )& pzcs )) && * pzcs != NULL )
215215 return enif_make_tuple2 (env , zstd_atom_error , zstd_atom_invalid );
216216
217217 /* allocate binary buffer */
@@ -263,7 +263,7 @@ static ERL_NIF_TERM zstd_nif_compress_stream(ErlNifEnv* env, int argc, const ERL
263263 ZSTD_CStream * * pzcs ;
264264
265265 /* extract the stream */
266- if (!(enif_get_resource (env , argv [0 ], zstd_compression_stream_type , (void * * )& pzcs )) ||
266+ if (( !(enif_get_resource (env , argv [0 ], zstd_compression_stream_type , (void * * )& pzcs )) && ( * pzcs ) != NULL ) ||
267267 !(enif_inspect_iolist_as_binary (env , argv [1 ], & in )))
268268 return enif_make_tuple2 (env , zstd_atom_error , zstd_atom_invalid );
269269
@@ -386,11 +386,13 @@ static ERL_NIF_TERM zstd_nif_decompress_stream(ErlNifEnv* env, int argc, const E
386386static void zstd_compression_stream_destructor (ErlNifEnv * env , void * stream ) {
387387 ZSTD_CStream * * handle = stream ;
388388 ZSTD_freeCStream (* handle );
389+ * handle = NULL ;
389390}
390391
391392static void zstd_decompression_stream_destructor (ErlNifEnv * env , void * stream ) {
392393 ZSTD_DStream * * handle = stream ;
393394 ZSTD_freeDStream (* handle );
395+ * handle = NULL ;
394396}
395397
396398static int zstd_init (ErlNifEnv * env ) {
0 commit comments