jotagrande

I'm getting a strange error when loading two bitmaps and adding them to the image list.

The following code is causing a Access violation exception in the Add method:

Code Snippet

CImageList img;

CBitmap bitmapNormal;

CBitmap bitmapMask;

if (bitmapNormal.LoadBitmap(IDB_GROUPPROFILE) != NULL)

{

...

}

if (bitmapMask.LoadBitmap(IDB_GROUPPROFILE_MASK) != NULL)

{

...

}

if (img.Add(&bitmapNormal, &bitmapMask) >= 0)

{

...

}

(The "..." are just for logging, I do nothing inside those If's)

The application was ported from VC++6 to VC++8, and it is compiling, but when starting it in release mode, that exception is thrown. In debug mode that block of code is no problem.

The exception message is:

0xC0000005: Access violation reading location 0x00000003.

Anyone has a clue why this may happen

Thx



Re: Visual C++ Language 0xC0000005: Access violation in CImageList

Viorel.

Probably the CImageList object has to be created before:

CImageList img;

img.Create( . . . );





Re: Visual C++ Language 0xC0000005: Access violation in CImageList

jotagrande

It's not that... I just forgoted to put here that block of code.

The complete code is:

Code Snippet

CImageList img;

CBitmap *bitmapNormal = new CBitmap();

BITMAP bmInfo;

bitmapNormal->LoadBitmap(IDB_GROUPPROFILE);

bitmapNormal->GetObject(sizeof(bmInfo), &bmInfo);

bitmapNormal->DeleteObject();

img.Create(bmInfo.bmWidth, bmInfo.bmHeight, ILC_MASK, 1, 0);

CBitmap *bitmapMask = new CBitmap();

if (bitmapNormal->LoadBitmap(IDB_GROUPPROFILE) != NULL)

{

...

}

if (bitmapMask->LoadBitmap(IDB_GROUPPROFILE_MASK) != NULL)

{

...

}

if (img.Add(bitmapNormal, bitmapMask) >= 0)

{

...

}

I've already checked that the application is finding the bitmap files, both the mask and bitmap to be loaded are of the same size, and the image list is created with the right size. (all 32x32 in size)
I think the bitmaps are DDB (Devide Dependent Bitmaps), because the bmBits field of the BITMAP struct is a null pointer, I'm not sure if this may cause the exception when I try to add them to the ImageList.
Still the same problem. And the weird thing is that this code compiles and works in VC++6 in a Windows 2000 Environment and in VC++8 Debug mode, the code has no problems, or at least, it doesn't throw an exception in the Add method.





Re: Visual C++ Language 0xC0000005: Access violation in CImageList

Ben Anderson MSFT

Could the call to bitmapNormal->DeleteObject(); be the culprit It looks like you're adding bitmapNormal after you delete the GDI object, and also using the bmInfo struct after the delete.




Re: Visual C++ Language 0xC0000005: Access violation in CImageList

Simple Samples

Are you sure that bitmapNormal and bitmapMask are normal Have you looked at them after the crash to ensure they have been created with valid values An error such as this often occurs after some other error has occured and has been ignored.

So in other words, the problem might be the data, regardless of the version of the compiler and such. If so, then you are assuming the problem is related to use of the newer compiler, and are therefore not diagnosing the problem thoroughly enough. Note that CBitmap::LoadBitmap return BOOL, so it seems unnecessary to check for NULL. More importnat than that, though, is if CBitmap::LoadBitmap is returning 0 and that is being ignored, then that is the reason for the access violation.






Re: Visual C++ Language 0xC0000005: Access violation in CImageList

jotagrande

Hi,

It's not that, I've tried right now to put the DeleteObject after the Create method, and I've created another CBitmap object to store the bitmaps loaded after creating the image list:

Code Snippet

CImageList img;

CBitmap *bitmapNormal = new CBitmap();

BITMAP bmInfo;

bitmapNormal->LoadBitmap(IDB_GROUPPROFILE);

bitmapNormal->GetObject(sizeof(bmInfo), &bmInfo);

img.Create(bmInfo.bmWidth, bmInfo.bmHeight, ILC_MASK, 1, 0);

bitmapNormal->DeleteObject();

CBitmap *bitmapMask = new CBitmap();

CBitmap *bitmap = new CBitmap();

if (bitmap->LoadBitmap(IDB_GROUPPROFILE) != NULL)

{

...

}

if (bitmapMask->LoadBitmap(IDB_GROUPPROFILE_MASK) != NULL)

{

...

}

if (img.Add(bitmap, bitmapMask) >= 0)

{

...

}

Still the same problem... Indifferent





Re: Visual C++ Language 0xC0000005: Access violation in CImageList

jotagrande

I've corrected that "!= NULL" to check true or false, the methods are all returning ok.

I think that the bitmaps have valid values, at least when I call the Add method I don't see any null pointer or data inside the bitmaps.

The crash comes from the AfxImageList_Add method, as you can see in the call stack:

adminAROP.exe!AfxImageList_Add(_IMAGELIST * himl=0x0026c3e8, HBITMAP__ * hbmImage=0x1b0514f3, HBITMAP__ * hbmMask=0x330519bc) Line 43 + 0x1a bytes C++
adminAROP.exe!CImageList::Add(CBitmap * pbmImage=0x01310a10, CBitmap * pbmMask=0x01310a00) Line 497 + 0x25 bytes C++
adminAROP.exe!CadminAROPApp::InitInstance() Line 190 + 0x10 bytes C++
mfc80.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x0024305c, int nCmdShow=1) Line 37 + 0x7 bytes C++
adminAROP.exe!__tmainCRTStartup() Line 589 + 0x1d bytes C
kernel32.dll!76f13833()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!7700a9bd()

There may be a problem with the hbmImage and hbmMask parameters of the AfxImageList_Add method, because in the locals window they appear with a red "exclamation mark".

himl 0x0026c3e8 _IMAGELIST *

hbmImage 0x1b0514f3 {unused= } HBITMAP__ *
unused CXX0030: Error: expression cannot be evaluated

hbmMask 0x330519bc {unused= } HBITMAP__ *
unused CXX0030: Error: expression cannot be evaluated

Is the error related to this The values seem valid in the Add method.

That message in the call stack ("no symbols loaded for kernel32.dll"), can be a problem (If this is a noob question, sorry, last month I was developing C++ for HP-UX!)

Thx

Joao





Re: Visual C++ Language 0xC0000005: Access violation in CImageList

Ben Anderson MSFT

What happens if you remove DeleteObject completely




Re: Visual C++ Language 0xC0000005: Access violation in CImageList

Simple Samples

jotagrande wrote:
That message in the call stack ("no symbols loaded for kernel32.dll"), can be a problem

No, that is not a problem. That is only information.






Re: Visual C++ Language 0xC0000005: Access violation in CImageList

jotagrande

Well, one useless day lost around this problem and I think I've finally found what was causing this.

I think it has to do with the following define (#define _AFX_NO_DAO_SUPPORT), I've commented this in stdafx.h and the application is now running with no problems.

I found the solution in one thread here in the MSDN forums, I had already searched several times, but didn't find it. Anyway, the link to the thread is http://forums.microsoft.com/msdn/showpost.aspx postid=189795&siteid=1&sb=0&d=1&at=7&ft=11&tf=0&pageid=2 it might be usefull to someone with the same problem.

Do you know of any other defines that can cause problems in VS2005 I hope there are not many, because finding them is a headache.

Thanks for your replies.

Joao





Re: Visual C++ Language 0xC0000005: Access violation in CImageList

Simple Samples

jotagrande wrote:
Do you know of any other defines that can cause problems in VS2005 I hope there are not many, because finding them is a headache.

Just don't use undocumented stuff. Developers must expect problems such as that when they use undocumented solutions. Many times people think they are smart by getting around things such as MFC with solutions such as #define _AFX_NO_DAO_SUPPORT and then they need help when something bizarre happens. The KB article lists other undocumented defines that can cause problems; I hope you saw that. Other than that, don't blame Microsoft if your software uses MFC in a manner it was not intended to be used and then strange things go bump.






Re: Visual C++ Language 0xC0000005: Access violation in CImageList

jotagrande

I'm not blamming Microsoft! I think they are doing a great job with the new technologies that they are developing/releasing at the moment (.Net 3.0, WCF, WPF, Vista, etc...).

And unfortunately it's not my software... I just switched company about a month ago, and I'm now working in a new project. An ECM application developed in Visual C++ 97... The guy who originally developped this got out of the company and now nobody knows how this works, and how this is done! There is no documentation, there was no knowledge transfer... It's a big mess now.

I now have to make this work in VS 2005 because I need to build a wrapper in C++\CLI for the C++ native code, so that .Net applications can use it, the problem is that bringing this thing from VC97 to VC2005 won't be easy... With the adittional handicap that I don't know this code, neither the libraries and all the defines that it may use.