Memory Leak converting JP2 to JPG

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
getherspoon
Posts: 1
Joined: 2012-08-20T04:27:13-07:00
Authentication code: 67789

Memory Leak converting JP2 to JPG

Post by getherspoon » 2012-08-20T04:38:37-07:00

Hello - I am experiencing a memory leak when trying to convert JP2 frames into JPG frames.

I am pretty new to ImageMagick, so i'm fairly certain the problem is on my end, but i'm not sure what i am doing wrong. From everything i've found, it seems like i'm doing things correctly, but i feel like i must be missing a step somewhere.

The leak appears to be rooted at line 106 of jas_malloc.c

I am using ImageMagick-6.7.7, build into static libraries from source.

If anyone has any advice, it would be much appreciated.

Code: Select all

// p_pBufferJP2: Input JP2 Image BLOB
// p_iSizeJP2: Input size of JP2 Image BLOB
// p_pBufferJPG: Output buffer for JPG Image
// p_iSizeJPG: Size of Output buffer
bool ConvertJP2toJPG( BYTE*& p_pBufferJP2, const int p_iSizeJP2, BYTE*& p_pBufferJPG, int& p_iSizeJPG )
{
	bool bResult = false;
	MagickBooleanType bStatus = MagickFalse;
	BYTE* pBuffJPG = NULL;
	size_t sLength = 0;
	
	MagickWandGenesis();
	MagickWand* pIMWand = NewMagickWand();		

	if( pIMWand && p_pBufferJP2 && p_iSizeJP2 > 0 )
	{
		try
		{		
			// Set Wand Format
			bStatus = MagickSetFormat( pIMWand, "JP2");
			
			// read the blob
			bStatus = MagickReadImageBlob( pIMWand, p_pBufferJP2, p_iSizeJP2 );			
			if( bStatus == MagickFalse )
			{
				ExceptionType exType;
				std::string strException( MagickGetException( pIMWand, &exType ) );
				ISCOUT_VIDEO_TRANSCODER_TRACE1( "==============  ImageMagicException: %s\n", strException.c_str() );				
			}
			else
			{
				// convert to a jpg
				bStatus = MagickSetFormat( pIMWand, "JPG" );
				if( bStatus == MagickFalse )
				{
					ExceptionType exType;
					std::string strException( MagickGetException( pIMWand, &exType ) );
					ISCOUT_VIDEO_TRANSCODER_TRACE1( "==============  ImageMagicException: %s\n", strException.c_str() );				
				}
				else
				{
					// read out the converted image
					pBuffJPG = MagickGetImageBlob( pIMWand, &sLength );
					if( pBuffJPG == NULL )
					{
						ExceptionType exType;
						std::string strException( MagickGetException( pIMWand, &exType ) );
						ISCOUT_VIDEO_TRANSCODER_TRACE1( "==============  ImageMagicException: %s\n", strException.c_str() );				
					}
					else
					{
						// ptr points to a buffer of size len bytes w/ the jpeg image. 
						// Needs to be copied to another buffer of size len so that ImageMagick can relinquish the memory.
						p_pBufferJPG = new BYTE[ sLength ];
						memcpy( p_pBufferJPG, pBuffJPG, sLength );
						p_iSizeJPG = sLength;
						
						MagickRelinquishMemory( pBuffJPG );

						bResult = true;
					}
				}
			}
		}
		catch( std::exception& ex )
		{
			std::string strEx( ex.what() );
			ISCOUT_VIDEO_TRANSCODER_TRACE3("		%s:%d: Exception: %s\n", __FUNCTION__, __LINE__, strEx.c_str() );
		}
		catch( ... )
		{			
			ISCOUT_VIDEO_TRANSCODER_TRACE2("		%s:%d: Exception\n", __FUNCTION__, __LINE__ );
		}
	}
	return bResult;
}
Many thanks!

-gS

User avatar
magick
Site Admin
Posts: 11015
Joined: 2003-05-31T11:32:55-07:00

Re: Memory Leak converting JP2 to JPG

Post by magick » 2012-08-20T05:34:43-07:00

We traced a small memory leak to the Jasper delegate library which we did not write. ImageMagick utilizes the Jasper API to decode JP2 images. Try contacting the Jasper authors and see if they have any advice.

Post Reply