c++ - Understanding why there is an Segmentation Fault -


i have code , try figure out, why i'm getting segmentation fault here:

i add speedeffect effectstack, works quite well. if try remove 1 of effects (which on stack) have call effect.removeeffect(). causes segmentation fault.

if try call effect.removeeffect() teststack() function, works (and prints expected "speed effect removed" on console)

void test::teststack() {     story* st = new story; //<-- needed initialization of effect     veins::tracimobility* mob = new veins::tracimobility; //<-- needed initialization of effect     speedeffect = speedeffect(1.0, st, mob);      a.removeeffect();  //<-- 1 works quite     (&a)->removeeffect();  //<-- clearly, works      effectstack s;     s.addeffect(&a);  //<-- adds effect effect stack     assert(s.geteffects().size() == 1);      s.removeeffect(&a); //<-- try remove effect stack } 

the stack , effect implemented following:

class effect { public:     effect(story* story, veins::tracimobility* car) :         m_story(story), m_car(car) {}      virtual void removeeffect() = 0; private:     story* m_story; protected:     veins::tracimobility* m_car; };   class speedeffect : public effect { public:     speedeffect(double speed, story* story, veins::tracimobility* car):         effect(story, car), m_speed(speed){}      void removeeffect() {         std::cout << "speed effect removed" << std::endl;     }  private:     double m_speed; };   class effectstack { public:     void addeffect(effect* effect) {         if(std::count(m_effects.begin(), m_effects.end(), effect) == 0) {             m_effects.push_back(effect);         }     }      void removeeffect(effect* effect) {         if(effect == m_effects.back()) {             //effect pointing on same address doing before, causes seg fault             m_effects.back()->removeeffect(); //<--- seg fault here!!             effect->removeeffect(); //<-- if use this, seg fault             m_effects.pop_back();         }else {             removefrommiddle(effect);         }     }      const std::vector<effect*>& geteffects() {         return m_effects;     }  private:     std::vector<effect*> m_effects; }; 

i hope code enough, have removed functions not called testing scenario.

is there problem, because address of speedeffect becomes invalid in stack?

maybe can me this.


new thoughts question: no have tested bit more, makes me more confused:

void dofoo(speedeffect* ef) {     ef->removeeffect(); //<-- breaks segmentation fault }  void test::teststack() {     story* st = new story;     veins::tracimobility* mob = new veins::tracimobility;     speedeffect e = speedeffect(3.0, st, mob);      e.removeeffect(); //<-- works fine     (&e)->removeeffect(); //<-- works fine     dofoo(&a); //<-- jumps dofoo() function } 

this may not you, persisting address of stack-based objects not great idea. in code above it's potentially okay since know effectstack won't outlive effect.

does crash still occur if do:

speedeffect* = new speedeffect(1.0, st, mob); 

(and adjust rest of code accordingly?) leak memory of course, tell if problem speedeffect being destroyed. option give speedeffect destructor (and effect virtual destructor) , set breakpoint inside see when the compiler destroying 'a'.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -