Kompiler Flag



  • Ich sitze an einem Programm welches eine Singleton Pattern benutzt als Fassadenklasse.

    init ist im man quasi so:

    TestInstance* instance;
     	instance->Instance(); // falls noch keine Instance -> starte (privaten) konstrukter und hole Instanze
        	instance->Run();
    

    Zusätzlich habe ich 2 Targets in meinem Makefile.
    ein debug und einmal build (for release)

    bei debug ist immer alles ok und Prog läuft ...
    bei build kommt immär

    Main.cpp: In function ‘int main(int, char**)’:
    Main.cpp:41: warning: ‘instance’ is used uninitialized in this function
    Main.cpp:47: warning: ‘instance’ is used uninitialized in this function
    

    mit dem Kompiler Flag -O
    -> Beim beenden des Progs bekomme ich IMMER ein segment. fault !!!!!
    Warum?? Ohne Flag -O renn und beendet sich mein Programm sauber!!!!!

    mfg
    Davidus



  • davidwal schrieb:

    TestInstance* instance;
     	instance->Instance(); // falls noch keine Instance -> starte (privaten) konstrukter und hole Instanze
        	instance->Run();
    

    Mit TestInstance* instance; legst du nur einen Pointer an. bevor du diesen Verwendest musst du dafür sorgen dass er auf etwas sinnvolles Zeigt. Beispielsweise

    TestInstance* instance = new TestInstance();

    EDIT://
    Vergiss das oben von mir ich hab nicht genau gelesen

    wie ist denn Instance() deklariert?

    Ich bin mir auch echt nicht sicher dass du statische Methoden über einen nicht initialisierten Zeiger aufrufen kannst



  • So ungefähr:

    #include <pthread.h>
    #include <errno.h>
    
    #ifndef ITASKINSTANCE_H_
    #define ITASKINSTANCE_H_
    
    class ITaskInstance {
    
    public:
    	static 	ITaskInstance* Instance(bool); 			//!< Itask Instance
    	void 	Run();									//!< start process
    
    private:
    	ITaskInstance();
    	static ITaskInstance* 	ITinstance;
    	ITaskInstance(const ITaskInstance&); // copy constructor
    	~ITaskInstance();
    
         class Attendant { //!< attendant for memory-clearing 
    		public: ~Attendant() {
    			if( ITaskInstance::Instance != 0 )
    				delete ITaskInstance::ITinstance;
    		}
    	};
    	friend class Attendant;
    
    };
    

    und

    ITaskInstance* ITaskInstance::ITinstance = 0; 	// initialize pointer
    
    ITaskInstance* ITaskInstance::Instance (bool sm) {
    
    	static Attendant ITattendant;
    	if (ITinstance == 0) {
    		ITinstance = new ITaskInstance(); // create sole instance
        }
    	return ITinstance; // address of sole instance
    }
    
    /*!
    \brief	private constructor - no derive possible
    */
    ITaskInstance::ITaskInstance() { 
    
    }
    
    ITaskInstance::~ITaskInstance() {
    
    }
    
    void ITaskInstance::Run() {
    	// run threads
    	... blabla
    }
    

Anmelden zum Antworten