Solved

i have big error in my c++ program ?

Posted on 2010-08-17
24
401 Views
Last Modified: 2013-12-14
hello,

my code  was written in c++  and  after i changed the charter set  to  Use Unicode Character Set

it compiled correct

but when  i run the application  it crashes and appear this error

unhandled exception at 0x0047 cdc in .exe 0xC0000005: Access violation reading location 0x00000000

please can  any one help ??

thank  you
0
Comment
Question by:sa3q
  • 10
  • 6
  • 5
  • +1
24 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 33453192
Are you able to post the code (maybe a whittled down version that still crashes).Possibly a null pointer?
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 33453195
Hi sa3q,

this most probably indicates accessing a invalid (not-initialized) pointer. Without knowledge of your code we have no chance to really help you.

Using the debugger you should be able to find the place in your code where this happens. If you found the place please post the code where the crash happens.

ZOPPO

0
 
LVL 32

Expert Comment

by:phoffric
ID: 33453204
If you build and run in Debug mode, and you get the unhandled exception popup. If you hit the Break button, then the debugger may take you to the location of the crash, where you may be able to inspect the local variables. If you do see a pointer in that vicinity, then you can test for NULL and throw an exception which you can trap with the debugger. (For more information on this, see http://www.experts-exchange.com/Programming/Languages/CPP/A_3530-Breakpoint-Tips-for-C-C-Visual-Studio-2008-Debugger-Part-2.html)
0
 
LVL 1

Author Comment

by:sa3q
ID: 33453241
i made message box  in first line  on   the main    

but the crash  appear before the message box

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
//int main()
{
MessageBox(NULL, (LPCWSTR)"1", (LPCWSTR)"bonjour(s)", MB_OK);

Open in new window

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 33453269
Hi sa3q,

IMO you have to declare hardcoded strings different for use with UNICODE instead of just casting them to 'LPCWSTR' - use the '_T' macro, i.e:

> MessageBox(NULL, _T( "1" ), _T( "bonjour(s)" ), MB_OK);

ZOPPO
0
 
LVL 1

Author Comment

by:sa3q
ID: 33453273
i tried  to  compile it in  debug mode  and run  it    and  the debuger show to  me where the error

the file that have the problem is not of min  
it in  winmain.cpp

in this line  

if (!pThread->InitInstance())
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33453282
And what do you find is the value for pThread? Is it 0?
0
 
LVL 1

Author Comment

by:sa3q
ID: 33453296
this print scr
rror.png
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33453306
It shows pThread is NULL (i.e., 0x00000000). Somehow, when you made changes, it appears that pThread did not get initialized properly. When you initialize pThread, are you checking for errors? Set a breakpoint on the line where pThread is set, and see if you even get there.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33453319
>>after i changed the charter set  to  Use Unicode Character Set

See the comment by ZOPPO - you are casting an ANSI string to a wide char string - WRONG !!!!
0
 
LVL 1

Author Comment

by:sa3q
ID: 33453379
this in the deceleration  
rror.png
0
 
LVL 1

Author Comment

by:sa3q
ID: 33453415
i commented all  the code in  the main  

and try the debug

the same error  

because pThread  in 0000000000
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 32

Expert Comment

by:phoffric
ID: 33453433
Did you try the advice given by zoppo in http:#33453241 ?

Here's one result of researching your problem:
"The crux of the problem was that someone had accidently commented the global CWinApp constructor."
    http://blogs.msdn.com/b/dsvc/archive/2009/06/21/why-afxgetthread-returns-null-in-afxwinmain.aspx

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 33453518
Hm - IMO pThread can only be NULL if initialization fails for any reason. Do you see any messages in the output window like i.e. 'Warning: Error during thread initialization!' or something else which could give any indication about the problem?

And, another question: Do you have more strings in your code which aren't declared using '_T( ... )' or casted as you did in the 'MessageBox'-call?

ZOPPO

0
 
LVL 1

Author Comment

by:sa3q
ID: 33453537
Zoppo:
phoffric:
the main is empty    and  the error  appear  it  appears after  i change  character  set  to  unicode

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 33453566
> the main is empty
What do you mean?

With the first question in my last post I meant this: If you start your application directly within the Debugger (Menu 'Debug->Start Debugging') do you see any messages in the 'Output' window after the crash occured?

With the second question (about the '_T') I tried to find if at any place a hardcoded string is declared in a way it cannot be used as UNICODE string - if so this could explain why the crash 'appears after  i change  character  set  to  unicode ' ...
0
 
LVL 1

Author Comment

by:sa3q
ID: 33453657
i mean in empty  that the code only have  

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
return 0;
}
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33453752
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)

That declares the lpCmdLine to be ANSI, not UNICODE

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nCmdShow)



But why do you have this declaration?  What type of application are you trying to develop ?
0
 
LVL 1

Author Comment

by:sa3q
ID: 33453823

AndyAinscow:


i used
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nCmdShow)

but their  was problem
 error C2731: 'WinMain' : function cannot be overloaded
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
ID: 33453858
You cannot use 'WinMain' with UNICODE.

Take a look here: http://msdn.microsoft.com/en-us/library/ms633559%28VS.85%29.aspx

> Note that lpCmdLine uses the LPSTR data type instead of the LPTSTR data type. This means that WinMain
> cannot be used by Unicode programs [...] Some programming frameworks might provide an alternative entry
> point that provides a Unicode command line. For example, the Microsoft Visual Studio C++ complier uses the
> name wWinMain for the Unicode entry point.

So, if you need to write your own 'WinMain' you have to use 'wWinMain' instead ...

ZOPPO
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33453979
What are you trying to do?  What sort of application do you want to develop?

You say it was changed to UNICODE then problems started.  Why do you now try to modify the 'main' of the application?  Just making random changes won't cure the problems (unless you are very lucky).
0
 
LVL 1

Author Comment

by:sa3q
ID: 33454048
thank you  all


0
 
LVL 1

Author Closing Comment

by:sa3q
ID: 33454054
that's it  thank  you

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 33454076
Hm - IMO you should have given points to AndyAinscow too since he first pointed out that 'WinMain' is the problem in http:#33453752
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now