From: Jason Nye Newsgroups: comp.os.msdos.djgpp Subject: Solution(?) to STRANGE PROBLEM Date: Sat, 18 Oct 1997 21:20:24 -0300 Organization: NBTel Internet Lines: 141 Message-ID: <34495247.8960CC82@nbnet.nb.ca> NNTP-Posting-Host: mctnts05c13.nbnet.nb.ca Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk WARNING!!! Long message ahead, I repeat, WARNING!!! Long message ahead! I have figured out what was going on with my SIGSEGV!!! It wasn't the protected mode startup code (poetry). I didn't think it was because all my other pmode programs still worked fine while this one didn't. I would appreciate it if some would take the time to read this -- especially DJ Delorie if he could... My Configuration: CPU: P166MMX, 40MB of RAM DPMI Server: CWSDPMI.EXE Compiler version: gcc 2.7.2.1 Summary: I created a class, say A, which had a helper class, say B. B was basically a singleton (except its members were not static), which means that there would only ever be one instance of this class. This class, B, would be hidden to the user of class A, therefore, I decided to put it in the cpp file. I also instantiated the class in the cpp file like so: #include // // B's interface is of no interest to the user of A class B { public: B(int size); ... }; // // B's functions... B::B(int size) { ... } ... etc // // Create a static instance of B. static B workerB(100); //////////////////////////////////// // // A's interface is defined in A.hpp A::A(char c) { // // Start using workerB as a helper here... } --------------------------------------------------------------------- Now, I would think that this would work absolutely fine, but what actually happens is once the execution has gone past the construction of workerB, all Hell breaks loose. This, apparently is a bug and a damn hard to find bug at that. I also tried declaring workerB without the static storage class and it crashed that way too. This is how I got around the bug: ----------------------------------------------------------------------------------------------------------- // // Header: #ifndef A_HPP #define A_HPP class B; // Create a forward declaration of class B (compiler only needs // to know that there exists a class B since we are only declaring // a pointer to a B -- works if we are declaring a reference also). class A { public: A::A(char c); private: B *workerB; static bool setup; }; #endif ----------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------- // // Cpp: #include bool A::setup = false; // // B's declaration as above... // A::A(char c) { if (!setup) { workerB = new B(100); setup = true; } } ---------------------------------------------------------------------- This works with no crashes. Why did the other way not work? I have no Idea. Maybe someone could help me out on why the static initialization did not work. Here are my compile options, maybe they are wrong (I am using a P166MMX with 40MB of RAM): // // I wanted to optimize my code for a Pentium (I saw these options in the Linux kernel makefile except // Linus used =2 instead of =4 would that cause a problem?). I didn't want any optimizations yet (-Ox) // and I didn't want to compile with debug code (-g), I just use a lot of print statements when I am // debugging a class. I find it easier (personal preference). CPU = -m486 -malign-loops=4 -malign-jumps=4 -malign-functions=4 OFLAGS = -I$(HPPDIR) -c -Wall -ffor-scope $(CPU) Any input would be greatly appreciated, thanks for taking time to read this :).