简体   繁体   English

打开文件c ++的分段错误

[英]Segmentation fault opening a file c++

I'm trying to set up a simple logging system.我正在尝试建立一个简单的日志系统。

Here is my Log.h file这是我的 Log.h 文件

#include <stdlib.h>
#include <string>
#include <fstream>

using namespace std;

class Log{

private:
    const static string ERROR;
    const static string WARNING; 
    const static string NOTICE;
    const static string DEBUG;
    const static string DEFAULT_FILENAME;

    static string filename;
    static ofstream* file;

public:
    Log();
    Log(string filename);
    ~Log();
    void init(string filename);

    static void log(string level, string msg);
    static void error(string msg);
    static void warning(string msg);
    static void notice(string msg);
    static void debug(string msg);
    static Log* getInstance();
};

And the actual code in Log.cpp以及 Log.cpp 中的实际代码

#include <stdlib.h>
#include <string>
#include "Log.h"
#include <fstream>
#include <iostream>

const string Log::ERROR = "ERROR";
const string Log::WARNING = "WARNING";
const string Log::NOTICE = "NOTICE";
const string Log::DEBUG = "DEBUG";
const string Log::DEFAULT_FILENAME = "log.txt";

string Log::filename;
ofstream* Log::file;

Log::Log(){
    this->init(DEFAULT_FILENAME);
}       

Log::Log(string filename){
    this->init(filename);
}       

Log::~Log(){ 
    this->file->close();
}       

void Log::init(string filename){
    Log::filename = filename; 
    cout << Log::file << " foo " << Log::filename.c_str() << endl;
    Log::file->open(filename.c_str(), ios::out | ios::app);
    cout << "bar" << endl;
    if(!Log::file->is_open()){
            throw 10;
    }
}

void Log::log(string level, string msg){
    if(Log::file == NULL)
            Log();
    cout << level << " : " << msg << endl;
    *Log::file << level << " : " << msg << endl;
}

void Log::error(string msg){
    log(ERROR, msg);
}

void Log::warning(string msg){
    log(WARNING, msg);
}

void Log::notice(string msg){
    log(NOTICE, msg);
}

void Log::debug(string msg){
    log(DEBUG, msg);
}

My main only contains :我的主要内容只包含:

Log::debug("Starting the server");

I compile with :我编译:

g++  -Wall -std=c++11  -c -o main.o main.cpp
g++  -Wall -std=c++11  -c -o Log.o Log.cpp
g++ -lfcgi++ -lfcgi main.o Log.o -o main

When I execute I get :当我执行我得到:

0 foo log.txt
make: *** [exec] Segmentation fault

The code segfault opening the file.打开文件的代码段错误。 Somehow, it isn't right issue since this code :不知何故,这不是正确的问题,因为此代码:

ofstream myfile;
myfile.open ("log.txt");
myfile << "Writing this to a file.\n";
myfile.close();

Works perfectly well.工作得很好。

Do you know why I got this segmentation fault error ?你知道为什么我会收到这个分段错误错误吗?

Thanks you !谢谢 !

I think the problem is you never create Log::file properly.我认为问题是你从来没有正确创建Log::file Anything that's a pointer must be initialized with new or some equivalent allocator.任何是指针的东西都必须用new或一些等效的分配器初始化。 You're calling a method on an uninitialized pointer and your code crashes there.你在一个未初始化的指针上调用一个方法,你的代码在那里崩溃。

The reason your smaller example works is because you're allocating on the stack, not a pointer to a heap object.您的较小示例有效的原因是因为您在堆栈上分配,而不是指向堆对象的指针。 That's the best way to tackle this in the end anyway.无论如何,这是最终解决此问题的最佳方法。 Using heap-allocated objects can get very messy in a hurry unless you're very careful to manage ownership.除非您非常小心地管理所有权,否则使用堆分配的对象会很快变得非常混乱。

This is a very odd way of using streams where you've got a global static instance yet you have a class as well.这是使用流的一种非常奇怪的方式,其中您有一个全局static实例,但也有一个类。 You should probably move the ofstream instance into the object.您可能应该将ofstream实例移动到对象中。

As a matter of style it's not necessary to put this-> in front of every method call or property reference, it's implied.就风格而言,没有必要将this->放在每个方法调用或属性引用的前面,这是隐含的。 That's only necessary in the case of a name conflict.只有在名称冲突的情况下才需要这样做。

Here's some ideas:这里有一些想法:

class Log {
private:
    string filename;
    ofstream file;
}

void Log::init(string filename_) {
    filename = filename_; 

    cout << file << " foo " << filename << ends;

    file.open(filename.c_str(), ios::out | ios::app);

    cout << "bar" << std::endl;

    if(!file.is_open()){
        throw 10;
    }
}

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

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