Pre-processor macros causing segfaults

This is a discussion on Pre-processor macros causing segfaults within the C++ Programming forums, part of the General Programming Boards category; Hello, so, I'm playing around with pre-processor macros, and I've come across behaviour I can't quite figure out. I've written ...

  1. #1
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39

    Pre-processor macros causing segfaults

    Hello,

    so, I'm playing around with pre-processor macros, and I've come across behaviour I can't quite figure out. I've written these macros to trace the execution path of my program, problem is, when I activate them, the program just seg-faults and I can't for the life of me figure out what is causing this, maybe anyone on here has a clue.

    Here are the two main files:

    Code:
    #ifndef DEBUG_HPP__
    #define DEBUG_HPP__
    
    #define DEBUG
    
    #ifdef DEBUG
    	#include <fstream>
    
    	#define LOCATION "(File: " << __FILE__ << ", Function: " << __FUNCTION__ << "(...), Line: " << __LINE__ << ")"
    	#define SPACE std::string(lvl*2, ' ')
    
    	#define BEGIN {															\
    					extern unsigned int lvl;								\
    					extern std::ofstream log;								\
    																			\
    					{														\
    						log << SPACE << "--> "								\
    							<< __FILE__ << "::"								\
    							<< __FUNCTION__ << "(...)"						\
    							<< std::endl;									\
    						lvl++;												\
    					}
    
    	#define PING															\
    					{														\
    						log << SPACE										\
    							<< "PING "										\
    							<< LOCATION										\
    							<< std::endl;									\
    					}
    
    	#define PRINT(x)														\
    					{														\
    						log << SPACE										\
    							<< x << " "										\
    							<< LOCATION										\
    							<< std::endl;									\
    					}
    
    	#define RETURN(x)														\
    					{														\
    						lvl--;												\
    						log << SPACE										\
    							<< "<-- "										\
    							<< __FILE__ << "::" << __FUNCTION__ << "(...)"	\
    							<< std::endl;									\
    					}														\
    																			\
    					return x;
    
    	#define END																\
    					{														\
    						lvl--;												\
    						log << SPACE										\
    							<< "<-- "										\
    							<< __FILE__ << "::" << __FUNCTION__ << "(...)"	\
    							<< std::endl; lvl--;							\
    					}														\
    				}
    
    #endif /*DEBUG*/
    
    #ifndef DEBUG
    	#define BEGIN {
    	#define PING
    	#define PRINT(x)
    	#define RETURN(x) return x;
    	#define END }
    #endif /*DEBUG*/
    
    #define DELETE(x) delete x; x = 0;
    
    #endif /*DEBUG_HPP__*/
    Ye, the indentation above sucks but hope you can understand it.

    Code:
    #include <fstream>
    #include "Debug.hpp"
    
    #ifdef DEBUG
    unsigned int lvl = 0;
    std::ofstream log("log.log");
    #endif /*DEBUG*/
    Here is a sample function which makes use of the macros.
    Code:
    void function(int arg)
    BEGIN
    
    PRINT(arg)
    END
    Thanks in advance, Jimmy.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seems to work for me.

    Code:
    --> main.cpp::function(...)
      7 (File: main.cpp, Function: function(...), Line: 6)
    <-- main.cpp::function(...)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Right, guess my example wasn't very error-prone, the problem might not lie in the macros them self I guess, but instead of something else interacting with the macros in some unexpected way. I need to do some more investigating here.

  4. #4
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Half-figured it out, seems that I had used the BEGIN and END macros on class constructors. And this somehow caused a few problems.
    Are the class constructors executed before the 'main' function starts executing?

    There was also a small error in the END macro causing the 'lvl' variable to fall out of sync.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The constructors of global objects are executed before main() starts, yes. And the objects in different translation units are initialized in unspecified order, so it could easily be that you have some log output before the log file is opened.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nempo View Post
    Half-figured it out, seems that I had used the BEGIN and END macros on class constructors. And this somehow caused a few problems.
    Are the class constructors executed before the 'main' function starts executing?

    There was also a small error in the END macro causing the 'lvl' variable to fall out of sync.
    Yes, class constructors for global/static objects are executed before YOUR CODE inside main. It may well be that the compiler adds a bit of compiler- generated code inside main() itself [gcc appears to call a function, and I think it's purpose is to call all global constructors].

    As CornedBee says, global constructors are executed in an undefined order [most likely "first found first", but not guaranteed by any means].

    You can probably solve this by using a singleton pattern for your logfile object.
    http://en.wikipedia.org/wiki/Singleton_pattern

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39

    Thumbs up

    Cheers for the help and clearing things up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. strlen causing my program to freeze
    By Beowolf in forum C Programming
    Replies: 2
    Last Post: 09-11-2007, 04:09 PM
  3. New compiler - Weird errors -,-.
    By Blackroot in forum C++ Programming
    Replies: 8
    Last Post: 08-27-2006, 07:23 AM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21