Heap corruptions when compiling 32Bit project under XP64

For Windows specific issues

Heap corruptions when compiling 32Bit project under XP64

Postby zamrate » Thu Jan 13, 2011 9:54 am

I've just updated to Windows XP64, and I've noticed that compiling my old project that uses JUCE146 with VS2003 leads to Heap Corruptions upon execution. It seems like the fact that the OS is 64Bit somehow breaks something in the compiler's process. The problem really happens during the build, because when I compile the app on a 32Bit XP system and then let it run on XP64, then everything's fine. Does anyone know what possible reasons can lead to such problems? I really don't know where to start looking at.

[EDIT: The problem was actually the fact that the system had a multiprocessor, which made the multithreading-related bug in my app happen more often, not the XP64/VS2003 combo as previously stated.]
Last edited by zamrate on Fri Jan 14, 2011 12:19 pm, edited 2 times in total.
User avatar
zamrate
JUCE UberWeenie
 
Posts: 1081
Joined: Mon Sep 24, 2007 5:33 pm

Re: Heap corruptions when compiling 32Bit project under XP64

Postby TheVinn » Thu Jan 13, 2011 3:33 pm

Run the Debug version of your application, and turn on MSVC debug heap checking. Make sure you are linking with the Debug runtimes.

Here is some magic code that you should add in your app initialization:

Code: Select all
      #include <crtdbg.h>
      int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);

      // check leaks at exit
      flag |= _CRTDBG_LEAK_CHECK_DF;

      // check heap at every heap call
      flag|= _CRTDBG_CHECK_ALWAYS_DF;      // on
      //flag&=~_CRTDBG_CHECK_ALWAYS_DF;      // off

      flag = _CrtSetDbgFlag( flag );


Shortly after your heap gets corrupted, you should get a breakpoint, and this will help you narrow down the place.
Now Juce has some conflicts with the MSVC debug heap interfaces (crtdbg.h) due to macros, so you will need to wrap the place where you include juce.h include like this:

Code: Select all
#ifdef _CRTDBG_MAP_ALLOC
# pragma push_macro("calloc")
# pragma push_macro("malloc")
# pragma push_macro("realloc")
# pragma push_macro("free")
# undef calloc
# undef malloc
# undef realloc
# undef free
#endif
//#include "src/juce_WithoutMacros.h"
#include "src/juce.h"
#ifdef _CRTDBG_MAP_ALLOC
# pragma pop_macro("calloc")
# pragma pop_macro("malloc")
# pragma pop_macro("realloc")
# pragma pop_macro("free")
#endif
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2976
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: Heap corruptions when compiling 32Bit project under XP64

Postby zamrate » Thu Jan 13, 2011 10:36 pm

Thanks, but all these lines did was slow down the program by a factor of 100 or so, but I couldn't figure out much.

I found an app called BoundsChecker and using that one I seem to have found one place in the JUCE code of JUCE146 that shows a problem:

Code: Select all
KERNINGPAIR* getKerningPairs (int& numKPs_) throw()
    {
        if (kps == 0)
        {
            numKPs = GetKerningPairs (dc, 0, 0);
            kps = (KERNINGPAIR*) juce_calloc (sizeof (KERNINGPAIR) * numKPs);    // <------ overrun detected in this block
            GetKerningPairs (dc, numKPs, kps);
        }

        numKPs_ = numKPs;
        return kps;
    }

It seems like this block gets overrun. But I don't know where and how to find out.
User avatar
zamrate
JUCE UberWeenie
 
Posts: 1081
Joined: Mon Sep 24, 2007 5:33 pm

Re: Heap corruptions when compiling 32Bit project under XP64

Postby zamrate » Thu Jan 13, 2011 11:00 pm

Adding a check if numKPs == 0 fixed the problem.
User avatar
zamrate
JUCE UberWeenie
 
Posts: 1081
Joined: Mon Sep 24, 2007 5:33 pm

Re: Heap corruptions when compiling 32Bit project under XP64

Postby jules » Thu Jan 13, 2011 11:33 pm

Ah, that bit of code's really old - I haven't used a malloc for a long long time now. Best to stick to the latest version if you're doing 64-bit stuff.
User avatar
jules
Fearless Leader
 
Posts: 17229
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Re: Heap corruptions when compiling 32Bit project under XP64

Postby zamrate » Fri Jan 14, 2011 11:15 am

Jules, I found 2 other recurring "problems" in JUCE (in the tip) using BoundsChecker. I don't know if they are important (if they can lead to other problems). But I'm just reporting them.

Problem 1 (in FontDCHolder::loadFont()) :

if (dc != 0)
{
DeleteDC (dc); // <----------- Argument 1 in DeleteDC (HDC__ hdc = 0xE1010FEE) still contains non default/stock objects. Font (0x7E0A1034).
DeleteObject (fontH);
kps.free();
}

DeleteDC Contains Selected Objects


Description
It is improper to DeleteDC a DeviceContext that still contains non-default objects.

Sample Code

void CMainFrame::OnCreateBrush() {
CBrush brush;
Brush.CreateSolidBrush(RGB(0xff,0x00,0xff));
if (true) {
CClientDC dc(this);
CBrush* pOldBrush = dc.SelectObject(&brush);
}
}


Repair
To repair the sample above, add this line before the closing brackets:

dc.SelectObject(pOldBrush);

The repaired code looks like this:

void CMainFrame::OnCreateBrush() {
CBrush brush;
Brush.CreateSolidBrush(RGB(0xff,0x00,0xff));
if (true) {
CClientDC dc(this);
CBrush* pOldBrush = dc.SelectObject(&brush);
dc.SelectObject(pOldBrush);
}
}

Note: The destructor of CDC will DeleteDC the DeviceContext at the end of scope for the if statement.





Problem 2 (in WindowsBitmapImage::~WindowsBitmapImage)

~WindowsBitmapImage()
{
DeleteDC (hdc); // <------------ Argument 1 in DeleteDC (HDC__ hdc = 0x07011387) still contains non default/stock objects. Bitmap (0xB1051267).
DeleteObject (hBitmap);
}
User avatar
zamrate
JUCE UberWeenie
 
Posts: 1081
Joined: Mon Sep 24, 2007 5:33 pm

Re: Heap corruptions when compiling 32Bit project under XP64

Postby jules » Fri Jan 14, 2011 11:40 am

Thanks! I don't think those actually matter - it's been running happily like that for years without any problems or leaks, but I'll take a look and see if I can avoid the warnings!
User avatar
jules
Fearless Leader
 
Posts: 17229
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK


Return to Windows

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron