Memory leaks in Magick++ ???

10bxjfhf <[email protected]> Thu, 27 Feb 2020 13:39:54 -0800
Newsgroups gmane.comp.video.graphicsmagick.apis
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============1762997808496546265==
Content-Type: multipart/alternative;
 boundary="------------5B43F89C15B2B9333411170F"
Content-Language: en-US

This is a multi-part message in MIME format.
--------------5B43F89C15B2B9333411170F
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit

Now that I have my app creating thumbnails from my TIFF files, I have 
run into a very  puzzling memory leak.


My test file directory tree looks something like

   --\testFiles\---\Tiff1\file1.tif
               |         \file2.tif
               |
               \---\Tiff2\file3.tif
                         \file4.tif

In the main app file I have
-------------

#if defined( WANT_MAGICK_PLUSPLUS )

#include "Magick++.h"

using namespace Magick;

#endif

main()

{
#if defined( __VISUALC__ )
   _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

.....

#if defined( WANT_MAGICK_PLUSPLUS )

   // the module is initialized when the CORE DLL is initialized

   Magick::InitializeMagick( NULL );

#endif

.....

}


-----------------------
The function which is is invoked once per TIFF file is:

bool MyIcCatalogVolDlg::GetTiffThumbnailMagickPP( const wxString a_wsFullFilePath )

{

  /**

   * Moved Magick++ initializationt to wxIC-MT.cpp

   */

   wxFileName wfnInFile = a_wsFullFilePath;

   wxFileName wfnOutFile = a_wsFullFilePath;

   wfnOutFile.SetExt( _T("jpg") );

   wxString wsNewName = wfnInFile.GetName() + _("_jj");

   std::string sinFile = a_wsFullFilePath.ToStdString();

   std::string sInFileName = wfnInFile.GetFullName().ToStdString();

   wfnOutFile.SetName( wsNewName );

   std::string sOutFile = wfnOutFile.GetFullPath().ToStdString();

   try {

     Image thumbnail;

     thumbnail.size( Geometry(  256, 256 ) );

     thumbnail.read( "NULL:black" );

     thumbnail.label( sInFileName );

     thumbnail.read( sinFile );

     thumbnail.thumbnail( Geometry( 256, 256) );

     thumbnail.write( sOutFile );

   }

   catch( exception &error_ )

   {

     wxLogMessage( _("Caught exception: %s"), error_.what() );

     //cout << "Caught exception: " << error_.what() << endl;

     return false;

   }

   return true;

}


When the app exits, I get one memory leak per scan cycle of the 
directory testFiles, not per thumbnail creation.
The portion of the IDE debug output is the result of running the scan twice

.....

The thread 0x6318 has exited with code 0 (0x0).

20200227131241 7:17.166589 42.078 20008 magick.c DestroyMagick 168 Configure Event Destroy Magick

20200227131241 7:17.166779 42.078 20008 module.c UnloadModule 2117 Configure Event Unloading "JPEG" module ...

20200227131241 7:17.166848 42.078 20008 module.c UnloadModule 2117 Configure Event Unloading "NULL" module ...

20200227131241 7:17.166917 42.078 20008 module.c UnloadModule 2117 Configure Event Unloading "TIFF" module ...

Detected memory leaks!

Dumping objects ->

D:\pkg\C++\MSVC2019\GM\gm-c031-d3ed\magick\memory.c(241) : {447928} normal block at 0x07DDE698, 135 bytes long.

  Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD

D:\pkg\C++\MSVC2019\GM\gm-c031-d3ed\magick\memory.c(241) : {213797} normal block at 0x07DDEAE8, 135 bytes long.

  Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD

Object dump complete.

The program '[0x4E28] wxIC-MT.exe' has exited with code 0 (0x0).


==============================

All of my code files include the MS memory allocation check code

At the top, preceding all headers

#if defined( _MSC_VER )
     #if defined ( _DEBUG )
       #define _CRTDBG_MAP_ALLOC
       #include <stdlib.h>
       #include <crtdbg.h>
     #endif
#endif

and before any code, but after all headers:

// this block needs to go AFTER all headers

// only good for MSVC - see note above re __VISUALC__

#if defined( _MSC_VER )

   #include <stdlib.h>

   #include <crtdbg.h>

   #ifdef _DEBUG

     #ifndef DBG_NEW

       #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )

       #define new DBG_NEW

     #endif

   #endif

#endif

This is the code which check for memory leaks and has served me well and 
it seems that a similar approach was/is considered/implemented in 
Magick++, because in studio.h,  I found

#include <stdarg.h>
#include <stdio.h>
#if defined(MSWINDOWS) && defined(_DEBUG)
#  define _CRTDBG_MAP_ALLOC
#endif

The leak is small and more or less insignificant and in no way will it 
interfere with my using the library, still ...

Arnold


--------------5B43F89C15B2B9333411170F
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Now that I have my app creating thumbnails from my TIFF files, I
      have run into a very  puzzling memory leak.</p>
    <p><br>
      My test file directory tree looks something like<br>
      <br>
        --\testFiles\---\Tiff1\file1.tif<br>
                    |         \file2.tif<br>
                    |<br>
                    \---\Tiff2\file3.tif<br>
                              \file4.tif<br>
                              <br>
      In the main app file I have <br>
      -------------<br>
    </p>
    <pre>#if defined( WANT_MAGICK_PLUSPLUS )</pre>
    <pre>#include "Magick++.h"</pre>
    <pre>using namespace Magick; </pre>
    <pre>#endif</pre>
    <pre>
main() </pre>
    <pre>{
#if defined( __VISUALC__ )
  _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
</pre>
    <pre>.....</pre>
    <pre>#if defined( WANT_MAGICK_PLUSPLUS )</pre>
    <pre>  // the module is initialized when the CORE DLL is initialized</pre>
    <pre>  Magick::InitializeMagick( NULL );</pre>
    <pre>#endif</pre>
    <pre>.....        </pre>
    <pre>}    </pre>
    <p><br>
      -----------------------    <br>
      The function which is is invoked once per TIFF file is:           
                  <br>
                              <br>
    </p>
    <pre>bool MyIcCatalogVolDlg::GetTiffThumbnailMagickPP( const wxString a_wsFullFilePath )</pre>
    <pre>{</pre>
    <pre> /**</pre>
    <pre>  * Moved Magick++ initializationt to wxIC-MT.cpp</pre>
    <pre>  */</pre>
    <pre>  wxFileName wfnInFile = a_wsFullFilePath;</pre>
    <pre>  wxFileName wfnOutFile = a_wsFullFilePath;</pre>
    <pre>  wfnOutFile.SetExt( _T("jpg") );</pre>
    <pre>  wxString wsNewName = wfnInFile.GetName() + _("_jj");</pre>
    <pre>  std::string sinFile = a_wsFullFilePath.ToStdString();</pre>
    <pre>  std::string sInFileName = wfnInFile.GetFullName().ToStdString();</pre>
    <pre>  wfnOutFile.SetName( wsNewName );</pre>
    <pre>  std::string sOutFile = wfnOutFile.GetFullPath().ToStdString();</pre>
    <pre>
  try {</pre>
    <pre>    Image thumbnail;</pre>
    <pre>    thumbnail.size( Geometry(  256, 256 ) );</pre>
    <pre>    thumbnail.read( "NULL:black" );</pre>
    <pre>    thumbnail.label( sInFileName );</pre>
    <pre>    thumbnail.read( sinFile );</pre>
    <pre>    thumbnail.thumbnail( Geometry( 256, 256) );</pre>
    <pre>    thumbnail.write( sOutFile );</pre>
    <pre>  }</pre>
    <pre>  catch( exception &amp;error_ )</pre>
    <pre>  {</pre>
    <pre>    wxLogMessage( _("Caught exception: %s"), error_.what() );</pre>
    <pre>    //cout &lt;&lt; "Caught exception: " &lt;&lt; error_.what() &lt;&lt; endl;</pre>
    <pre>    return false;</pre>
    <pre>  }</pre>
    <pre>  return true;</pre>
    <pre>}                        </pre>
    <p><br>
      When the app exits, I get one memory leak per scan cycle of the
      directory testFiles, not per thumbnail creation.<br>
      The portion of the IDE debug output is the result of running the
      scan twice<br>
      <br>
    </p>
    <pre>.....</pre>
    <pre>The thread 0x6318 has exited with code 0 (0x0).</pre>
    <pre>20200227131241 7:17.166589 42.078 20008 magick.c DestroyMagick 168 Configure Event Destroy Magick</pre>
    <pre>20200227131241 7:17.166779 42.078 20008 module.c UnloadModule 2117 Configure Event Unloading "JPEG" module ...</pre>
    <pre>20200227131241 7:17.166848 42.078 20008 module.c UnloadModule 2117 Configure Event Unloading "NULL" module ...</pre>
    <pre>20200227131241 7:17.166917 42.078 20008 module.c UnloadModule 2117 Configure Event Unloading "TIFF" module ...</pre>
    <pre>Detected memory leaks!</pre>
    <pre>Dumping objects -&gt;</pre>
    <pre>D:\pkg\C++\MSVC2019\GM\gm-c031-d3ed\magick\memory.c(241) : {447928} normal block at 0x07DDE698, 135 bytes long.</pre>
    <pre> Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD </pre>
    <pre>D:\pkg\C++\MSVC2019\GM\gm-c031-d3ed\magick\memory.c(241) : {213797} normal block at 0x07DDEAE8, 135 bytes long.</pre>
    <pre> Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD </pre>
    <pre>Object dump complete.</pre>
    <pre><pre>The program '[0x4E28] wxIC-MT.exe' has exited with code 0 (0x0).</pre>
==============================
</pre>
    <p>All of my code files include the MS memory allocation check code</p>
    <p>At the top, preceding all headers<br>
    </p>
    <pre>#if defined( _MSC_VER )
    #if defined ( _DEBUG )
      #define _CRTDBG_MAP_ALLOC
      #include &lt;stdlib.h&gt;
      #include &lt;crtdbg.h&gt;
    #endif
#endif</pre>
    <p>and before any code, but after all headers:</p>
    <pre>// this block needs to go AFTER all headers</pre>
    <pre>// only good for MSVC - see note above re __VISUALC__</pre>
    <pre>#if defined( _MSC_VER )</pre>
    <pre>  #include &lt;stdlib.h&gt;</pre>
    <pre>  #include &lt;crtdbg.h&gt;</pre>
    <pre>  #ifdef _DEBUG    </pre>
    <pre>    #ifndef DBG_NEW</pre>
    <pre>      #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )</pre>
    <pre>      #define new DBG_NEW    </pre>
    <pre>    #endif  </pre>
    <pre>  #endif</pre>
    <pre>#endif</pre>
    <p>This is the code which check for memory leaks and has served me
      well and it seems that a similar approach was/is
      considered/implemented in Magick++, because in studio.h,  I found</p>
    <pre>#include &lt;stdarg.h&gt;
#include &lt;stdio.h&gt;
#if defined(MSWINDOWS) &amp;&amp; defined(_DEBUG)
#  define _CRTDBG_MAP_ALLOC
#endif
</pre>
    <p>The leak is small and more or less insignificant and in no way
      will it interfere with my using the library, still ...</p>
    <p>Arnold<br>
    </p>
  </body>
</html>

--------------5B43F89C15B2B9333411170F--


--===============1762997808496546265==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============1762997808496546265==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Graphicsmagick-apis mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/graphicsmagick-apis

--===============1762997808496546265==--