Search code examples
c++ofstreamqnx

Program crashes trying to create ofstream or fopen


I don't have enough reputation points to comment to ask if they solved the problem originally stated here. I have the same problem of the program crashing in construction of an ofstream. It is not multi-threaded access, but it can be called in quick succession. I believe it crashes on the 2nd time. I use scope to ensure the stream object is destroyed. Why would this happen?

I tried std::fopen too. It also results in a crash.

Here is the code using the ofstream:

static bool writeConfigFile (const char * filename, const Config & cfg)
{
    logsPrintLine(_SLOG_SETCODE(_SLOGC_CONFIG, 0), _SLOG_INFO, "Write config file (%s stream)", filename);
    ofstream os(filename); // FIXME: Crashes here creating ofstream 2nd time
    if (os.good())
    {
        // Uses stream insertion operator to output attributes to stream 
        // in human readable form (about 2kb)
        outputConfig(cfg, os);
        if (!os.good())
        {
            logsPrintLine(_SLOG_SETCODE(_SLOGC_CONFIG, 0), _SLOG_NOTICE, "Failed to write configuration file (%s)", filename);
            return false;
        }
        logsPrintLine(_SLOG_SETCODE(_SLOGC_CONFIG, 0), _SLOG_INFO, "Configuration written to file (%s)", filename);
        return true;
    }
    logsPrintLine(_SLOG_SETCODE(_SLOGC_CONFIG, 0), _SLOG_NOTICE, "Cannot write configuration file (%s)", filename);
    return false;
}

/**
 * Called when configuration settings have been read/received and validated
 * @return true if successfully set, and written to file
 */
bool Config::set (SysConfigSource source, const struct SCADA_dsconfig * p)
{
    Lock lock(mtxSet); // This is locking a mutex on construction of the lock. Release it on destruction.
    // Setup the non-current one to switch to
    Config * pCfg = pConfig.other();
    unsigned i, f, n = 0;

    // set attributes in pCfg based on the config received
    // and some constants ...

    pCfg->setWritten(writeConfigFile("test.conf", *pCfg));

    if (!pCfg->isWritten())
    {
        // Don't set system config status here. Existing one still in use.
        logsPrintLine(_SLOG_SETCODE(_SLOGC_CONFIG, 0), _SLOG_NOTICE, "Config file not written. Retain prior config.");
        return false;
    }
    pConfig.swap(); // switch-in the new config
    setSystemConfigSource(source);
    toSyslog(pCfg);
    notifyConfigChange();
    return true;
}

Solution

  • It turned out to be a device driver bus master issue. Add "ahci nobmstr" when launching devb-ahci.

    Derived via http://www.qnx.com/developers/docs/qnxcar2/index.jsp?topic=%2Fcom.qnx.doc.neutrino.user_guide%2Ftopic%2Fhardware_Troubleshooting_devb-eide.html