In some case user still want to be able to call opj_stream_destroy.
Fixes issue 227
opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
if (l_stream) {
+ if (l_stream->m_free_user_data_fn) {
+ l_stream->m_free_user_data_fn(l_stream->m_user_data);
+ }
opj_free(l_stream->m_stored_data);
l_stream->m_stored_data = 00;
opj_free(l_stream);
void OPJ_CALLCONV opj_stream_destroy_v3(opj_stream_t* p_stream)
{
- opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
-
- if (l_stream) {
- FILE *fp = (FILE*)l_stream->m_user_data;
- if(fp)
- fclose(fp);
-
- opj_free(l_stream->m_stored_data);
- l_stream->m_stored_data = 00;
- opj_free(l_stream);
- }
+ opj_stream_destroy(p_stream);
}
void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function)
l_stream->m_skip_fn = p_function;
}
-void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data)
+void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data, opj_stream_free_user_data_fn p_function)
{
opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
if (!l_stream)
return;
l_stream->m_user_data = p_data;
+ l_stream->m_free_user_data_fn = p_function;
}
void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, OPJ_UINT64 data_length)
*/
void * m_user_data;
+ /**
+ * Pointer to function to free m_user_data (NULL at initialization)
+ * when destroying the stream. If pointer is NULL the function is not
+ * called and the m_user_data is not freed (even if non-NULL).
+ */
+ opj_stream_free_user_data_fn m_free_user_data_fn;
+
/**
* User data length
*/
return NULL;
}
- opj_stream_set_user_data(l_stream, p_file);
+ opj_stream_set_user_data(l_stream, p_file, (opj_stream_free_user_data_fn) fclose);
opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
*/
typedef OPJ_BOOL (* opj_stream_seek_fn) (OPJ_OFF_T p_nb_bytes, void * p_user_data) ;
+/*
+ * Callback function prototype for free user data function
+ */
+typedef void (* opj_stream_free_user_data_fn) (void * p_user_data) ;
+
/*
* JPEG2000 Stream.
*/
*
* @param p_stream the stream to destroy.
*/
-OPJ_DEPRECATED(OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream));
+OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream);
/**
* Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream.
* Sets the given data to be used as a user data for the stream.
* @param p_stream the stream to modify
* @param p_data the data to set.
+ * @param p_function the function to free p_data when opj_stream_destroy() is called.
*/
-OPJ_API void OPJ_CALLCONV opj_stream_set_user_data (opj_stream_t* p_stream, void * p_data);
+OPJ_API void OPJ_CALLCONV opj_stream_set_user_data (opj_stream_t* p_stream, void * p_data, opj_stream_free_user_data_fn p_function);
/**
* Sets the length of the user data for the stream.