简体   繁体   English

Seg fault unique_ptr具有工厂设计尝试

[英]Seg fault unique_ptr with factory design attempt

I'm getting a segmentation fault when trying to use a unique_ptr to create instances of derived classes. 尝试使用unique_ptr创建派生类的实例时,我遇到了segmentation fault Before, I had coded every instantiation of the seven derived classes, one after the other and the code was working properly. 之前,我已经编写了七个派生类的每个实例,一个接一个地编码,代码工作正常。

Current code is the following: 目前的代码如下:

typedef std::unique_ptr<Comum> ComumPtr;

ComumPtr createInstance ( string dom, map<string, string> & config, map<string, string> & config_fields )
{
    ComumPtr ptr;       // initialized to nullptr.
    if ( dom == "voice" ) {
    ptr.reset ( new Voice (config, config_fields) );
    //    } else if ( dom == "account" ) {     // FOR OTHER DERIVED CLASSES
    //  ptr.reset ( new Account (config, config_fields) );
    }
    return ptr;
}

// At  main function:
 for (vector<string>::const_iterator cit = for_domain.begin(); cit != for_domain.end(); ++cit) { 
    const char * section (cit->c_str());
    string fsn = *cit + "_fields";
    const char * fields_section_name (fsn.c_str());
    const char * db_section ("Database");

    map <string, string> domain_config = cfg.getSectionConfig (config_file.c_str(), section);
    map <string, string> domain_config_fields  = cfg.getSectionConfig (config_file.c_str(), fields_section_name);
    map <string, string> database_config = cfg.getSectionConfig (config_file.c_str(), db_section);

    std::unique_ptr<Comum> domain = createInstance(*cit, domain_config, domain_config_fields);

    domain->readDatabaseFields (database_config);   // <- segmentation fault

Do you see any reason for this to seg fault? 你认为有什么理由可以解决这个错误吗?

function createInstance has the chance to return nullptr , you need to check the pointer is valid: function createInstance有机会返回nullptr ,需要检查指针是否有效:

if (domain.get())
{
  domain->readDatabaseFields (database_config);
}

This line is the error: 这一行是错误的:

ComumPtr ptr;       // initialized to nullptr.

While I understand that nullity is so easy , it's also the best way to shoot yourself in the foot (whether in C++ or Java), because now any single use of the result of this function need be checked. 虽然我理解无效是如此简单 ,但它也是用脚射击自己的最好方法(无论是用C ++还是Java),因为现在需要检查这个函数结果的任何单一用法。

Instead, you could: 相反,你可以:

  • use a Null object: the method readDatabaseFields will just do nothing 使用Null对象: readDatabaseFields方法将不执行任何操作
  • choose to throw an exception rather than returning a null pointer 选择抛出异常而不是返回空指针

None of the above alternative is inherently better than the other, it very much depends on the situation; 上述替代方案本身并不比另一种更好,它在很大程度上取决于具体情况; however both are better than returning a null unique_ptr . 但是两者都比返回null unique_ptr更好。

Supposing you'd elect the exception method: 假设您选择了异常方法:

ComumPtr createInstance ( string dom, map<string, string> & config, map<string, string> & config_fields )
{
    if ( dom == "voice" ) {
        return ComumPtr ( new Voice (config, config_fields) );
    } 
    if ( dom == "account" ) {
        return ComumPtr ( new Account (config, config_fields) );
    }

    throw std::runtime_error("Unknown config field");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM