Birisi iç kapsamı terk edildiğinde neden ana() 'de aşağıdaki çökmelerin nedenini açıklayabilir mi? Visual Studio 2013 kullanıyorum. GCC 4.8.1 ile her şey yolunda olsa da, yine de kodda bir şeylerin yanlış olduğundan şüpheleniyorum. Anlamadım.Shared_ptr ile gizemli çöküş
#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.
}
Ben VS2013 ile olsun hata iletisi:
onaylama işlemi hatası
_BLOCK_TYPE_IS_VALID (pHead-> nBlockUse)
Kod oldukça küçük, neden bir hata ayıklayıcısını kullanarak adım atmıyorsunuz? –
@CaptainObvlious gerçekten yardımcı olur muydu? –
@ n.m. Muhtemelen hayır, ama benim meyveli çakıltaşımın sabah kasetini alana kadar mazoşistim. –