views:

95

answers:

3

What is going on?! I fixed some structs, extensive amount of search/replace in my code. Then i finish and everything compiles fine, but the program crashes immediately.

This is my main function:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
    exit(1);

In all logics it shouldnt crash, since im not executing any functions. only WinMain(), which does nothing!

What the hell? And why its not giving me the line numbers anymore? its giving the location in the external include files, not the line in MY code.

Also i noticed my EXE size is now 5 times smaller than it was before, which also doesnt make sense, looks like it stops compiling at some point, but still says its compiled!

Edit: this is what i get from the error:

File: ... \include\xstring
Line: 1084
Expression: Invalid null pointer

When i run it in non-debug mode i get First-chance exception at 0x00413a95 ....: 0xC0000005: Access violation reading location 0x00000000.

--

Edit2: reason found: I initialized a global struct in the following way: const StructName VarName = {1, 1, 1}; but the StructName Struct was changed.

+2  A: 

Constructors for global and static objects are invoked before a program starts. (I'm not sure how this interacts with WinMain(), though.)

Run your application under the debugger to see how it crashes.

From your added description it seems as if a std::string is initialized with a NULL pointer, which isn't allowed. Do you have a global/static string that's initialized with NULL/0? This typically happens when you change a variable's type from char* (or char[]) to std::string.

sbi
updated my first post
Newbie
I dont remember setting strings to null. But now when i tested, the problem indeed IS in the structs, i just cant find the problem. I removed the structs and anything that points to them, and program runs fine! But i just cant find the problem... I didnt change string to char or etc. i just rearranged my structs so its more editable (many structs shares the same variables)
Newbie
@Newbie: Without seeing "the structs", we can't understand your problem either. Try to boil your code down to a minimal, self-contained repro case. _Very_ likely you will find your problem doing so. If not, you have the perfect piece of code to come back here and ask about.
sbi
@Newbie: It sounds like somebody is using a string before it has been constructed. i.e. The constructor of one object is using a string before that string's constructor has been called.
Martin York
@sbi: yeah doing that atm. @Martin York: how the hell is that even possible? could you give example?
Newbie
I got it working now, but now it crashes in debug mode >_< lol.
Newbie
Newbie
hmm... that line only crashes if Application Verifier is enabled. So now everything is working and case closed.
Newbie
@Newbie: Case closed? IME, when a bug "disappears" by doing something seemingly unrelated, it's _very_ likely that it comes back at unexpected (and, usually, inconvenient) moments. One difference between Debug and Release builds with VC is that the Release RTL will zero dynamically allocated memory before it hands it to you. In Release builds the same allocation will give you garbage.
sbi
I dont know how to even start fixing it, ive read my code over and over and i cant find any memory leaks or whatsoever they called. RTL tell me nothing.
Newbie
@Newbie: You need to boil it down to two or three dozen lines which reproduce the behavior. Post them here, if this doesn't reveal the problem.
sbi
Ok, i did that, and guess what? While i was trying to figure it out by removing code etc. The problem solved on its own while i was writing new question at this site... I was trying to translate non-english error messages into english, so i searched some interwebs and found out i need to add english as a new language in my win-xp settings, since ive removed it before. So when i tried to compile it in debug mode again, no errors found o.O But now i removed the english again, and still no errors found.I also changed the project exe to not have ( )marks since i noticed commandline crashes with it
Newbie
But i added the ( ) marks back, and still no errors. Shortly: This makes absolutely no sense! I said it before, i say it again: its a ghost in the machine.
Newbie
MMH. it was LuaPriv checks that crashed AV: http://stackoverflow.com/questions/3890640/debugging-with-av-causes-crash-when-luapriv-checks-enabled
Newbie
A: 

Please post a complete, minimal example that exhibits the problem.

Alf P. Steinbach
My code isnt minimal. the problem is i dont know which part of the code does this error!
Newbie
Hallo Alf, welcome to SO. You would be a great addition to our stock of C++ wizards. For the future, please post such as this as a comment to the question, rather than as an answer. (I know you cannot post comments until you have earned 50 rep, but that's just 5 up-votes - one good answer - away and still doesn't make yours a real answer.) Things like this should be covered by the [FAQ](http://stackoverflow.com/faq) which you are encouraged to read. Now be a nice citizen and delete this useless answer. `:)`
sbi
A: 

As @sbi said, it's likely global/static objects. In my experience, this (often?) happens if a string object is in a global scope which is referenced by other global object/initialization code. Due to non-deterministic initialization order, the string can be used before it has been constructed. I'd look for those cases (well, and avoid global code if possible).

On a side note, this can happen with any objects, not just strings. It's usually not that the object wasn't initialized properly, but rather that something is using it before it's initialized (although @sbi's answer could also be the cause).

Nick
So this is why globals are bad. Nobody told me this before, they all just say "globals are bad", gg. Dunno how i can make programs without globals though.
Newbie
As a suggestion, look into various discussions of globals and singletons on SO, and various design suggestion questions/threads: there's a lot of good info and help. Good luck. :)
Nick
@Newbie: This is _not_ the main problem with globals. The main problem is that, since they aren't explicitly passed to functions that modify them, you never know which functions do this.
sbi