Solved

i have big error in my c++ program ?

Posted on 2010-08-17
24
404 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 31

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 31

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
 
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 31

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 31

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 31

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 31

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS2015 compilation and missing DLLs 9 147
Resharper CTRL+N: Differentiate between files with same name 2 67
C++ question 3 65
C++ mouse_event mouse look 7 82
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 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

14 Experts available now in Live!

Get 1:1 Help Now