Menu

#2318 Vector losing data if object is declared outside WinMain/Main (linux)

OTHER
closed
nobody
None
Bug
works-for-me
Unreproducible
False
2016-12-04
2016-11-12
No

The problem I (we) have is that vector is wiped out when the application enters main. You can see better with the links below

Simple (Recreated problem)
Google drive folder where i have simple version of the problem. There is windows exe compiled with x86_64-w64-mingw32-g++ and linux .out compiled with gcc(g++) on ubuntu 16.10, .out version does not have any issue. Anybody could open the link and see the output.
mingw -v -> 6.2.0 20160822.
g++ -v -> 6.2.0 20161005
Stack overflow (Real problem). Same configuration as above.

I also have compiled the "Simple" files on Windows 10 with mingw - 5.3.0 and it fails.

output in my terminal :

nikola@nikola:~/Desktop/asd$ ./a.out
push size : 1
constructor size : 1
main : 1
nikola@nikola:~/Desktop/asd$ wine a
push size : 1
constructor size : 1
main : 0

Thanks!

Related

Issues: #2320

Discussion

  • Keith Marshall

    Keith Marshall - 2016-11-24

    We do not support x86_64-w64-mingw32-g++; it is not a product of this project. However, if by "mingw-5.3.0 on Windows-10" you mean the version of GCC which we do provide, and you would like us to follow this up, then please attach a SSCCE directly to this ticket; linking to a potentially "here today, gone tomorrow" example on google drive, (and to a StackOverflow reference which has already disappeared), may be detrimental to the long term integrity of ticket information, and is therefore not acceptable.

     
  • Nikola Lozanovski

    yes, mingw-5.3.0 is the version of gcc on my windows 10. I deleted the stackoverflow post because it wasn't complete, sorry.

    I had problems adding attachment to this post so here is the code the will reproduce the problem, i got 405 Method Not Allowed The method GET is not allowed for this resource while posting with attachment.

    foo.h

    #include <vector>
    //#include <list>
    #include <iostream>
    class test
    {
     public:
      int tt;
      //static std::list<int> ttt;   //segmentation fault
       static std::vector<int> ttt;  //vector get empty
      test();
      test(int i);
      int push(int j);
    };
    

    foo.cpp

    #include "foo.h"
    std::vector<int>test::ttt;
    //std::list<int>test::ttt;
    
    int test::push(int j)
    {
      ttt.push_back(j);
      std::cout<<"push size : "<<ttt.size()<<std::endl;
      return j;
    }
    
    test::test()
    {
    
    }
    
    test::test(int i)
    {
      int q=push(i);
      std::cout<<"constructor size : "<<ttt.size()<<std::endl;
    }
    

    source.cpp

    #include "foo.h"
    //this bug occurs only when object is declared outside main
    test t(5);
    int main()
    {
      /*
        if the object is declared inside main it will work as expected
        test t(5);
      */
      std::cout<<"main : "<<test::ttt.size()<<std::endl;
    
      return 0;
    }
    

    Later i found that vector isn't the only faulty container but also list (maybe other containers) which provides segmentation fault.

    Program compiled with gcc on linux and visual studio compiler on windows does not produce this error

     

    Last edit: Nikola Lozanovski 2016-11-24
    • Keith Marshall

      Keith Marshall - 2016-11-24

      I deleted the stackoverflow post because it wasn't complete, sorry.

      No worries. IMO, StackOverflow is a waste of time anyway ... you're more likely to get uninformed nonsense from clueless reputation seekers, than any informed reply.

      I had problems adding attachment to this post ... 405 Method Not Allowed The method GET is not allowed for this resource

      How did you try to add it? I've never had any problem adding attachments, using the "Add attachments" link, next to the "Post" button and "Cancel" link, below the message editing pane. (I assume you were logged in to SourceForge).

       
  • Nikola Lozanovski

    Yes, i tried the way you describe, are .exe files filtered? I had few of them.

     
  • Nikola Lozanovski

    huh, adding rar file as attachment worked. Anyway there you will find cpp, header files and windows and linux binaries. You can compile the binaries yourself or use the existing ones.

     
  • Nikola Lozanovski

    I found out that if the cpp files are compiled into object files and then those object files compiled into executable there would be no problem, it will be working fine. Hmmm strange.

     
    • Keith Marshall

      Keith Marshall - 2016-11-25

      Strange indeed. FWIW, I was unable to extract anything useful from your RAR archive, but I copied and pasted your sources into foo.h, foo.cpp, and bar.cpp, then cross-compiled it, and ran it under wine-1.4.1 (old, I know, but WJFFM):

      $ mingw32-g++ -O3 -o foo.exe bar.cpp foo.cpp
      $ ./foo.exe;rstty
      push size : 1
      constructor size : 1
      main : 1
      

      Is this not the outcome you expect? I'm no C++ expert, but it looks right to me. (BTW, the rstty is there to circumvent a wine bug, which leaves my .inputrc configuration in some weird, and undesirable state; it's just a bash alias for less -fE /dev/null, defined in my .bashrc).

       
  • Nikola Lozanovski

    Sorry for late reply i had exams.
    I don't know what exactly is happening on my machines, on my windows machine sometimes works sometimes does not, main should be 1 like main : 1 to work. However i found solution for me and it is to compile to object file like object.o and then create executable file from these object files. We cannot reproduce the problem where it is not working for everybody.
    Thank you anyway !

     
  • Keith Marshall

    Keith Marshall - 2016-12-04
    • status: unread --> closed
    • Resolution: none --> works-for-me
    • Category: Unknown --> Unreproducible