Win Rtas showdown heap corruption


#1

I’ve just downloaded the latest Juce from git (23rd Feb 2010) and built the Debug Win Rtas JuceDemoPlugin using Visual C++ 2008 against the PT_80_SDK. I loaded the plugin just fine in Win XP SP3 using Debug version Pro Tools LE 8.0.3, but I get the following message (as shown in the attachment) when I unload the plugin from the track: "Debug Error! Program: C:\Program Files\Digidesign\Pro Tools\ProToolsLE.exe HEAP CORRUPTION DETECTED: after Normal block (#365) at 0x0FD7F980. CRT detected that the application wrote to memory after end of heap buffer. (Press Retry to debug the application)

I have compiled the debug version of the “SimplePlugIn” example in the Digidesign SDK using the same linker settings at the JuceDemoPlugin and I don’t get this message. Anyone have any ideas?


#2

See http://www.rawmaterialsoftware.com/viewtopic.php?f=2&t=1116&start=0&hilit=UseDebuggingNewOperatorLEAK

I added the following define ( juce_UseDebuggingNewOperatorLEAK(X) ) to juce_Memory.h (see whole file (23/2/2010’s GIT-tip) below)

{The relevant clever bits involving _CrtSetBreakAlloc(X); }

cf. http://msdn.microsoft.com/en-us/library/4wth1ha5(VS.80).aspx

/*
  ==============================================================================

   This file is part of the JUCE library - "Jules' Utility Class Extensions"
   Copyright 2004-9 by Raw Material Software Ltd.

  ------------------------------------------------------------------------------

   JUCE can be redistributed and/or modified under the terms of the GNU General
   Public License (Version 2), as published by the Free Software Foundation.
   A copy of the license is included in the JUCE distribution, or can be found
   online at www.gnu.org/licenses.

   JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  ------------------------------------------------------------------------------

   To release a closed-source product which uses JUCE, commercial licenses are
   available: visit www.rawmaterialsoftware.com/juce for more information.

  ==============================================================================
*/

#ifndef __JUCE_MEMORY_JUCEHEADER__
#define __JUCE_MEMORY_JUCEHEADER__

//==============================================================================
/*
    This file defines the various juce_malloc(), juce_free() macros that should be used in
    preference to the standard calls.
*/

#if defined (JUCE_DEBUG) && JUCE_MSVC && JUCE_CHECK_MEMORY_LEAKS
  #ifndef JUCE_DLL
    //==============================================================================
    // Win32 debug non-DLL versions..

    /** This should be used instead of calling malloc directly.
        Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
    */
    #define juce_malloc(numBytes)                 _malloc_dbg  (numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)

    /** This should be used instead of calling calloc directly.
        Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
    */
    #define juce_calloc(numBytes)                 _calloc_dbg  (1, numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)

    /** This should be used instead of calling realloc directly.
        Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
    */
    #define juce_realloc(location, numBytes)      _realloc_dbg (location, numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)

    /** This should be used instead of calling free directly.
        Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
    */
    #define juce_free(location)                   _free_dbg    (location, _NORMAL_BLOCK)

  #else
    //==============================================================================
    // Win32 debug DLL versions..

    // For the DLL, we'll define some functions in the DLL that will be used for allocation - that
    // way all juce calls in the DLL and in the host API will all use the same allocator.
    extern JUCE_API void* juce_DebugMalloc (const int size, const char* file, const int line);
    extern JUCE_API void* juce_DebugCalloc (const int size, const char* file, const int line);
    extern JUCE_API void* juce_DebugRealloc (void* const block, const int size, const char* file, const int line);
    extern JUCE_API void juce_DebugFree (void* const block);

    /** This should be used instead of calling malloc directly.
        Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
    */
    #define juce_malloc(numBytes)                 JUCE_NAMESPACE::juce_DebugMalloc (numBytes, __FILE__, __LINE__)

    /** This should be used instead of calling calloc directly.
        Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
    */
    #define juce_calloc(numBytes)                 JUCE_NAMESPACE::juce_DebugCalloc (numBytes, __FILE__, __LINE__)

    /** This should be used instead of calling realloc directly.
        Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
    */
    #define juce_realloc(location, numBytes)      JUCE_NAMESPACE::juce_DebugRealloc (location, numBytes, __FILE__, __LINE__)

    /** This should be used instead of calling free directly.
        Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
    */
    #define juce_free(location)                   JUCE_NAMESPACE::juce_DebugFree (location)
  #endif

  #if ! defined (_AFXDLL)
    /** This macro can be added to classes to add extra debugging information to the memory
        allocated for them, so you can see the type of objects involved when there's a dump
        of leaked objects at program shutdown. (Only works on win32 at the moment).
    */
    #define juce_UseDebuggingNewOperator \
      static void* operator new (size_t sz)           { void* const p = juce_malloc ((int) sz); return (p != 0) ? p : ::operator new (sz); } \
      static void* operator new (size_t, void* p)     { return p; } \
      static void operator delete (void* p)           { juce_free (p); } \
      static void operator delete (void*, void*)      { }

     #define juce_UseDebuggingNewOperatorLEAK(X) \
      static void* operator new (size_t sz)           { void* const p = juce_malloc ((int) sz); _CrtSetBreakAlloc(X); return (p != 0) ? p : ::operator new (sz); } \
      static void* operator new (size_t sz, void* p)  { _CrtSetBreakAlloc(X); return ::operator new (sz, p); } \
      static void operator delete (void* p)           { juce_free (p); } \
      static void operator delete (void*, void*)      { }

  #endif

#elif defined (JUCE_DLL)
  //==============================================================================
  // Win32 DLL (release) versions..

  // For the DLL, we'll define some functions in the DLL that will be used for allocation - that
  // way all juce calls in the DLL and in the host API will all use the same allocator.
  extern JUCE_API void* juce_Malloc (const int size);
  extern JUCE_API void* juce_Calloc (const int size);
  extern JUCE_API void* juce_Realloc (void* const block, const int size);
  extern JUCE_API void juce_Free (void* const block);

  /** This should be used instead of calling malloc directly.
      Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  */
  #define juce_malloc(numBytes)                 JUCE_NAMESPACE::juce_Malloc (numBytes)

  /** This should be used instead of calling calloc directly.
      Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  */
  #define juce_calloc(numBytes)                 JUCE_NAMESPACE::juce_Calloc (numBytes)

  /** This should be used instead of calling realloc directly.
      Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  */
  #define juce_realloc(location, numBytes)      JUCE_NAMESPACE::juce_Realloc (location, numBytes)

  /** This should be used instead of calling free directly.
      Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  */
  #define juce_free(location)                   JUCE_NAMESPACE::juce_Free (location)

  #define juce_UseDebuggingNewOperator \
    static void* operator new (size_t sz)           { void* const p = juce_malloc ((int) sz); return (p != 0) ? p : ::operator new (sz); } \
    static void* operator new (size_t, void* p)     { return p; } \
    static void operator delete (void* p)           { juce_free (p); } \
    static void operator delete (void*, void*)      { }

     #define juce_UseDebuggingNewOperatorLEAK(X) \
      static void* operator new (size_t sz)           { void* const p = juce_malloc ((int) sz); return (p != 0) ? p : ::operator new (sz); _CrtSetBreakAlloc(X);} \
      static void* operator new (size_t sz, void* p)  { return ::operator new (sz, p); _CrtSetBreakAlloc(X);} \
      static void operator delete (void* p)           { juce_free (p); } \
      static void operator delete (void*, void*)      { }

#else

  //==============================================================================
  // Mac, Linux and Win32 (release) versions..

  /** This should be used instead of calling malloc directly.
      Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  */
  #define juce_malloc(numBytes)                 malloc (numBytes)

  /** This should be used instead of calling calloc directly.
      Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  */
  #define juce_calloc(numBytes)                 calloc (1, numBytes)

  /** This should be used instead of calling realloc directly.
      Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  */
  #define juce_realloc(location, numBytes)      realloc (location, numBytes)

  /** This should be used instead of calling free directly.
      Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  */
  #define juce_free(location)                   free (location)

#endif

//==============================================================================
/** This macro can be added to classes to add extra debugging information to the memory
    allocated for them, so you can see the type of objects involved when there's a dump
    of leaked objects at program shutdown. (Only works on win32 at the moment).

    Note that if you create a class that inherits from a class that uses this macro,
    your class must also use the macro, otherwise you'll probably get compile errors
    because of ambiguous new operators.

    Most of the JUCE classes use it, so see these for examples of where it should go.
*/
#ifndef juce_UseDebuggingNewOperator
  #define juce_UseDebuggingNewOperator
  #define juce_UseDebuggingNewOperatorLEAK(X)
#endif

//==============================================================================
#if JUCE_MSVC
  /** This is a compiler-independent way of declaring a variable as being thread-local.

      E.g.
      @code
      juce_ThreadLocal int myVariable;
      @endcode
  */
  #define juce_ThreadLocal    __declspec(thread)
#else
  #define juce_ThreadLocal    __thread
#endif

//==============================================================================
#if JUCE_MINGW
  /** This allocator is not defined in mingw gcc. */
  #define alloca              __builtin_alloca
#endif

//==============================================================================
/** Clears a block of memory. */
inline void zeromem (void* memory, size_t numBytes)             { memset (memory, 0, numBytes); }

/** Clears a reference to a local structure. */
template <typename Type>
inline void zerostruct (Type& structure)                        { memset (&structure, 0, sizeof (structure)); }

/** A handy function that calls delete on a pointer if it's non-zero, and then sets
    the pointer to null.
*/
template <typename Type>
inline void deleteAndZero (Type& pointer)                       { delete pointer; pointer = 0; }

#endif   // __JUCE_MEMORY_JUCEHEADER__

#3

Sorry for not getting it, but how does this fix the bug or help? It’s not a memory leak.


#4

Sorry. Looks like I leapt in before reading.


#5

no probs dub, I’d rather have someone try to help than not bother

Anyway, I found the problem. I tried to compile the release version with VC Express 2008 (VC9) and it wouldn’t even load. I think it was just a fluke that it worked in debug at all. So I downloaded VC Express 2005 (VC8) and the windows sdk for it and it is all working fine. I think it’s to do with the libs that digidesign provide without any projects for not matching up right during linking, since they would have been complied with a different version of the compiler.

So, hopefully in the future I can work out how to compile those dependent libs, or at least get digidesign to, but for now I’m stuck with VS8 instead of VS9.


#6

Well, that is some good info for those of us who are working on RTAS. Thanks for posting your solution.

Sean Costello


#7

Thanks Andy, that’s good to know. I’ve always used VC2005 to build RTAS, because I couldn’t even get it to link in VC2008…