Сбой программы при попытке создать ofstream или fopen - PullRequest
0 голосов
/ 29 октября 2018

У меня недостаточно очков репутации, чтобы комментировать, чтобы спросить, решили ли они проблему, указанную изначально здесь .У меня та же проблема сбоя программы при построении офстрима.Это не многопоточный доступ, но он может быть вызван в быстрой последовательности.Я полагаю, что это терпит крах во второй раз.Я использую область, чтобы гарантировать, что объект потока уничтожен.Почему это случилось?

Я тоже пробовал std :: fopen.Это также приводит к сбою.

Вот код, использующий 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;
}

Ответы [ 2 ]

0 голосов
/ 06 ноября 2018

Оказалось, что это проблема мастера шины драйвера устройства.Добавьте "ahci nobmstr" при запуске devb-ahci.

Получено с помощью http://www.qnx.com/developers/docs/qnxcar2/index.jsp?topic=%2Fcom.qnx.doc.neutrino.user_guide%2Ftopic%2Fhardware_Troubleshooting_devb-eide.html

0 голосов
/ 29 октября 2018

Возможно, опубликуйте сегмент вашего исходного кода, чтобы понять, где он пошёл не так.

Вот очень простой фрагмент кода, как я бы использовал fstream .. надеюсь, вы найдете его полезным.

#include <iostream>
#include <fstream>
#include <string>

int main() {
    while (1) {
        std::string testString;
        std::ofstream outFile;
        outFile.open("Test", std::ios_base::app); // Appends to file, does not delete existing code

        std::cout << "Enter a string: ";
        std::cin >> testString;
        outFile << testString << std::endl;

        outFile.close();
    }
}
...