-
June 12th, 2012, 12:59 PM
#1
Empty main invokes code of project classes
Hello
In a Visual Studio 2010 project I have the following code:
main.cpp
Code:
#include <string>
#include <vector>
#include <iostream>
using namespace std;
void main (int argc, char *argv[])
{
cout << "hello " <<endl;
}
I have:
- cleaned and rebuilt the project
- renamed and tested the main function to make sure that's the main function being run.
- Made sure there is only one project in the solution
Still a Singleton class called Settings (which normally should be instantiated by direct call to its getInstance method) is being instantiated. I receive the following message (which comes from getInstance of the Settings class).
Code:
Exception: Set app name before initial call to settings class
Press any key to continue . . .
1- How can I track whose calling the Class?
2- How is it possible that an empty main() function causes other codes to run? I even searched and made sure there is no static code (except static variable initializations) somewhere in other classes.
Thanks.
-
June 12th, 2012, 01:16 PM
#2
Re: Empty main invokes code of project classes
First, main returns int, not void.
Still a Singleton class called Settings (which normally should be instantiated by direct call to its getInstance method) is being instantiated. I receive the following message (which comes from getInstance of the Settings class).
Then you're running a different program than the one you claim is being built. It's that simple.
If a simple "Hello World" program did all of this "extra work", then Microsoft would have thousands of complaints, and you know that can't be the case. Why not go to the directory where this executable was created and run it directly, either from the command-line or Explorer. If the program runs correctly, then your project is not correct.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 12th, 2012 at 01:21 PM.
-
June 12th, 2012, 01:19 PM
#3
Re: Empty main invokes code of project classes
Paul,
Thank you for your time. I changed the int to void purposefully to see what happens.
When I change the main function name to main1, the program won't run. No, it is a 15000 lines of code program. I just removed everything from main to see why Settings class is being instantiated.
Regards,
Mac
-
June 12th, 2012, 01:25 PM
#4
Re: Empty main invokes code of project classes
Originally Posted by sarmadys
Paul,
Thank you for your time. I changed the int to void purposefully to see what happens.
When I change the main function name to main1, the program won't run. No, it is a 15000 lines of code program. I just removed everything from main to see why Settings class is being instantiated.
Regards,
Mac
So is it a three line main() program, or is it really a 15,000 line program?
Also, you haven't mentioned anything about debugging the code by using the debugger. Have you done that? What if you set a breakpoint in this singleton class and check the call stack when the breakpoint is reached? What function is calling this Singleton object?
Regards,
Paul McKenzie
-
June 12th, 2012, 01:35 PM
#5
Re: Empty main invokes code of project classes
Paul,
- When I exclude all classes and files from the project, main function runs fine.
- There is just this main function. I just tried to create errors in it so that I can see compiler will stop or not. And indeed it stops and cannot compile and run (I forgot to change the void to int).
- It is a simulation program with at least 50 classes. I used the debugger but it exits even before running that single line in Main. That's because the Settings class has a exit command (in case the appName static variable is not set).
- I'll set a breakpoint in Singleton's getInstance and see what is in call stack.
Thanks.
-
June 12th, 2012, 01:48 PM
#6
Re: Empty main invokes code of project classes
Thanks Paul.
I found the problem by using call stack. Unfortunately there was a static code which was being run.
One of the variable initializations was calling the getInstance of my Settings class (a Singleton).
Code:
const double LeastEffortMicroscopicModelPr::CORNER_MOVE_SIZE = sqrt(2 * Settings::getInstance().getCellSize() * Settings::getInstance().getCellSize());
Still I am shocked how strange and dangerous behaviors can come out of such static code. A simple variable initialization elevates into instantiating an object and running its code! Even worse the order of running static code (in different classes) is not known.
The reason i used const static was because hundreds of thousands of instances of this class are being produced and I wanted to cut the memory usage. But it seems I have no way to do that (since it comes from a settings file).
Thanks
Last edited by sarmadys; June 12th, 2012 at 02:23 PM.
-
June 12th, 2012, 03:11 PM
#7
Re: Empty main invokes code of project classes
It's in the definition of the language. All static objects will be initialized, before any code is executed. The order that they are initialized in is undefined.
Viggy
-
June 12th, 2012, 04:01 PM
#8
Re: Empty main invokes code of project classes
Originally Posted by sarmadys
[B]Still I am shocked how strange and dangerous behaviors can come out of such static code. A simple variable initialization elevates into instantiating an object and running its code!
Welcome to the world of C++. If you declare a const like that, then the expression on the right hand side of the = needs to be evaluated. If the expression involves calling a static method of a class, so be it.
Unlike 'C', C++ allows code to be run before main() is executed, and you ran right into such a concept.
Regards,
Paul McKenzie
-
June 12th, 2012, 08:41 PM
#9
Re: Empty main invokes code of project classes
Minor clarifications:
Originally Posted by MrViggy
All static objects will be initialized, before any code is executed.
All static objects of global scope will be initialized, before any code is executed. Local static objects are initialized on first use.
For some additional information, see "What's the "static initialization order fiasco"?" at http://www.parashift.com/c++-faq-lit...html#faq-10.14 and a few of the sections after that.
Originally Posted by MrViggy
The order that they are initialized in is undefined.
But the order can be affected (at least in MS Visual C++ compiler) using #pragma init_seg , see "HOW TO: Use #pragma init_seg to Control Static Construction" at http://support.microsoft.com/kb/104248
Mike
-
June 12th, 2012, 09:34 PM
#10
Re: Empty main invokes code of project classes
Originally Posted by MikeAThon
All static objects of global scope will be initialized, before any code is executed. Local static objects are initialized on first use.
I believe the initialisation that MrViggy is talking about is the early zero initialisation of objects with static storage duration. This can happen for local static objects too.
-
June 13th, 2012, 10:00 AM
#11
Re: Empty main invokes code of project classes
A simple variable initialization elevates into instantiating an object and running its code! Even worse the order of running static code (in different classes) is not known.
And who said it's simple? Simplicity most of the times is just an illusion.
Best regards,
Igor
-
June 13th, 2012, 03:30 PM
#12
Re: Empty main invokes code of project classes
Originally Posted by laserlight
I believe the initialisation that MrViggy is talking about is the early zero initialisation of objects with static storage duration. This can happen for local static objects too.
Yep. Sorry I wasn't clearer.
Viggy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|