Kann jemand bitte erklären, warum das folgende in main() abstürzt, wenn der innere Bereich verlassen wird? Ich benutze Visual Studio 2013. Obwohl alles mit GCC 4.8.1 in Ordnung ist, vermute ich, dass etwas im Code sowieso nicht stimmt. Ich verstehe es einfach nicht.Mysteriöser Absturz mit shared_ptr
#include <iostream>
#include <memory>
class Person; class PersonProxy;
class PersonInterface {
public:
virtual ~PersonInterface() = default;
virtual PersonProxy* getProxy() const = 0;
virtual void createProxy (Person*) = 0;
};
class Person : public PersonInterface {
private:
std::string name;
std::shared_ptr<PersonProxy> proxy;
public:
Person() = default;
explicit Person (const std::string& n) : name(n) {}
public:
virtual PersonProxy* getProxy() const override {return proxy.get();}
inline void createProxy (Person* p);
};
class PersonProxy : public PersonInterface {
private:
std::shared_ptr<Person> actual;
public:
explicit PersonProxy (Person* p) : actual (std::shared_ptr<Person>(p)) {}
explicit PersonProxy (std::shared_ptr<Person> p) : actual (p) {}
void rebind (std::shared_ptr<Person> p) {actual = p;}
virtual PersonProxy* getProxy() const override {return actual->getProxy();}
virtual void createProxy (Person* p) override {actual->createProxy(p);}
};
class Girl : public Person {
public:
Girl (const std::string& name) : Person (name) {createProxy (this);}
};
inline void Person::createProxy (Person* p) {
proxy = std::shared_ptr<PersonProxy>(new PersonProxy(p));
}
int main() {
{
Girl* a = new Girl("a");
// std::shared_ptr<Girl> a = std::make_shared<Girl>("a"); // Using this crashes with Visual Studio 2013 on the line 'a->getProxy()->rebind(b);'
std::shared_ptr<Girl> b = std::make_shared<Girl>("b");
a->getProxy()->rebind(b);
std::cout << "rebind succeeded." << std::endl;
}
std::cout << "Exited scope." << std::endl; // Exiting scope crashes with VS 2013.
}
Die Fehlermeldung, die ich mit VS2013 erhalten ist:
Assertionsfehler
_BLOCK_TYPE_IS_VALID (pHead-> nBlockUse)
Der Code ist ziemlich klein, warum nicht mit einem Debugger durchlaufen? –
@CaptainObvlious würde es wirklich helfen? –
@ n.m. Wahrscheinlich nicht, aber ich bin ein Masochist, bis ich meine Morgenschale mit Fruity Pebbles hatte. –