Solved

i have big error in my c++ program ?

Posted on 2010-08-17
24
405 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing the elements of a set declared inside a map in C++. 3 52
Arduino EDI - Programming language 3 103
CKEditor - will not function 7 72
eclipse formatting 6 106
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

860 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