Reputation: 300
Following code crashes on(/after) the Food-destructor; as shown in the stack below;
6 operator delete() 0xb7e5f4bf
5 std::string::_Rep::_M_destroy() 0xb7ec648b
4 <symbol is not available> 0xb7ec64d0
3 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() 0xb7ec653e
2 Food::~Food() Main.cpp:126 0x0804c33c
1 main() Main.cpp:199 0x0804c288
The Food-ctor is never called but the destructor is? AFAICT things go haywire when resources of "string _text" are being freed, but I can't seem to understand why this is going wrong. Obviously I could change "string _text" to "string* _text", but I'd rather understand why this goes wrong.
class Food {
private:
string _text;
public:
Food(){
cout << "ctor Food" << endl;
}
Food(string name) {
cout << "ctor Food: name=" << name << endl;
}
virtual ~Food() {
cout << "dtor Food" << endl;
}
};
template<typename T>
class Action {
public:
static T eat(int i) {
cout << "Eat: " << i << endl;
}
};
int main() {
auto x = Action<Food>::eat(1);
}
Upvotes: 0
Views: 76
Reputation: 409206
What you are doing is undefined behavior. You define a function (eat
) as returning a type T
, but you are not actually returning anything. This leads to the assignment being undefined.
Upvotes: 4