views:

68

answers:

2

How do I debug the above error? I'm using C++ with Microsoft Visual Studio.

Below is part of the code:

HDC dc =*mMemDC;
X->SelectPalette(dc);

When I debug the code, it crashes on the line X->selectPallete(dc);

A: 

Set a breakpoint at X->SelectPalette(dc);. Inspect X. If it doesn't look like a valid pointer(i.e. in this case an address around 0x0b9ec715) or is 0, then it's definitely a bad pointer. Also provide more code, as what you've given doesn't really answer any questions.

Igor Zevaka
it is unlikely it would be 4, usually it is null, and the problem happens, not simply because you are calling a member function on a null pointer - that by itself it fine - but because that function tries to access the member that has an offset of 4 in X's class. thus: NULL + 4 = 4
matt
Yeah, that's true, i ll edit.
Igor Zevaka
@matt, interesting - but then why would it be 4? shouldn't it be class-instance's-address + 4 instead? If X was NULL, then the read location in the exception should have been 0, right?
Zabba
I canot place breakpoint in line x->SelectPalete(dc),sine this line is inside WM_PAINT method.If i place breakpoint,it continuosly lands in WM_PAINT msg.Note:when i click an icon in the toolbar,a window which it has to open,first time its well and fine,second time,i.e i close the window which i opened,and agan click on the icon.then this crash at specifiled line
Vishu
So what happens the first time it hits the breakpoint? What is the value of X? If you can get VS window and you target window side by side, you can just hit continue every time it lands on the breakpoint, eventually it'll settle.
Igor Zevaka
Vvalues of X are not null.
Vishu
what is this VS window?please tell.How will breakpoint settle
Vishu
and values of x are no t null
Vishu
What is the value of X? VS = Visual Studio. By settle I mean that it wouldn't enter an infinite loop once the window finishes drawing.
Igor Zevaka
yes yes i got that.i placed window side by side,breakpoint settled.You want to know values of X?
Vishu
Yep, also please post the code that initializes X.
Igor Zevaka
@Rag, I think you recently found out about stackoverflow, so let me tell you that the site is great - many great minds here to help you; but they can't if you're not detailed enough in the facts of your problem.
Zabba
intializing X is as below X = StaticX::CreateX(Frme); and createX method is below X* StaticX::CreateX(HWND hwnd) { StaticUseCount++; delete Static_X; return (Static_X = new StaticX(hwnd)); } i tried debuggin,X is not null,it is a valid pointer. Supposing before the line X->SelectPallete(dc) if i add call any method belonging to X, ie X->GetBkgColor();,so while debugging,this line never gives any problem,only when i reach the next line i.e X->selectPallete(dc),it throws the exception. Please note:This error is randomly coming.Not everytime crash happens.
Vishu
yes,recently i found this site.Now i seek help.its related to devicecontexts.GDI.
Vishu
Hmm, perhaps add `ASSERT(::IsWindow(hwnd));` to make sure it's a valid window. Can you provide some code for `StaticX` as well?
Igor Zevaka
A: 

Use the debugger to check what the values in mMemDC and X are. Some code at memory address 0x0b9ec715 is trying to read memory at the invalid location of 0x00000004. 0xC0000005 is the code that signifies an error when trying to read memory at an invalid location.

Also, you may get a few ideas about other exceptions with weird memory "addresses" here: http://blog.sina.com.cn/s/blog_598c00790100a6a0.html

Zabba
sometimes i get this error in the same line."Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."I checked,values of mMemDC and X are not NULL
Vishu
If the values are not NULL, what are they? We're all trying to help you here, so please be detailed. From this latest info you provide, it seems like there is a bug in your code. Fyi, the "ESP" is "Extended Stack pointer" (a 32-bit value, as opposed to "SP" which is a 16-bit value). I think you should post more of your code, especially the exports you've made from the DLL and the imports in the EXE. Also, is there any multi-threading or timers that you are using in the app?
Zabba
Sounds like you have an unitialized pointer. Please post code that initializes X.
Igor Zevaka
intializing X is as below X = StaticX::CreateX(Frme);and createX method is belowX* StaticX::CreateX(HWND hwnd){ StaticUseCount++; delete Static_X; return (Static_X = new StaticX(hwnd));}i tried debuggin,X is not null,it is a valid pointer.Supposing before the line X->SelectPallete(dc) if i add call any method belonging to X, ie X->GetBkgColor();,so while debugging,this line never gives any problem,only when i reach the next line i.e X->selectPallete(dc),it throws the exception.Please note:This error is randomly coming.Not everytimecallstack when crash happens:
Vishu