CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2001
    Posts
    31

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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.

  3. #3
    Join Date
    Dec 2001
    Posts
    31

    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

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Empty main invokes code of project classes

    Quote Originally Posted by sarmadys View Post
    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

  5. #5
    Join Date
    Dec 2001
    Posts
    31

    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.

  6. #6
    Join Date
    Dec 2001
    Posts
    31

    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.

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    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

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Empty main invokes code of project classes

    Quote Originally Posted by sarmadys View Post
    [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

  9. #9
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Empty main invokes code of project classes

    Minor clarifications:
    Quote Originally Posted by MrViggy View Post
    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.

    Quote Originally Posted by MrViggy View Post
    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

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Empty main invokes code of project classes

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  12. #12
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Empty main invokes code of project classes

    Quote Originally Posted by laserlight View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured