C++ 尝试打开流时,记录器接收到分段故障

C++ 尝试打开流时,记录器接收到分段故障,c++,segmentation-fault,ofstream,C++,Segmentation Fault,Ofstream,我在尝试打开Logger类的文件时抛出了一些SIGSEGV,这让我有点麻烦。我已经使用这个类有一段时间了,但这是我第一次得到这个。从一句简单的话来说,就像以前所说的: LOG(logDEBUG) << "Node " << nodeId << " in link layer state waitAck."; 得到这个gdb: Program received signal SIGSEGV, Segmentation fault. 0x00007ffff72

我在尝试打开Logger类的文件时抛出了一些SIGSEGV,这让我有点麻烦。我已经使用这个类有一段时间了,但这是我第一次得到这个。从一句简单的话来说,就像以前所说的:

 LOG(logDEBUG) << "Node " << nodeId << " in link layer state waitAck.";
得到这个gdb:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7210289 in malloc_consolidate () from /usr/lib/libc.so.6
(gdb) backtrace
#0  0x00007ffff7210289 in malloc_consolidate () from /usr/lib/libc.so.6
#1  0x00007ffff7211d2a in _int_malloc () from /usr/lib/libc.so.6
#2  0x00007ffff7213d44 in malloc () from /usr/lib/libc.so.6
#3  0x00007ffff7ae1a48 in operator new (sz=8192)
    at /build/gcc/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50
#4  0x00007ffff7ae1af5 in operator new[] (sz=<optimized out>)
    at /build/gcc/src/gcc/libstdc++-v3/libsupc++/new_opv.cc:32
#5  0x00007ffff7b41698 in std::basic_filebuf<char, std::char_traits<char> >::_M_allocate_internal_buffer
    (this=0x7fffffffdc68)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/fstream.tcc:55
#6  0x00007ffff7b45932 in std::basic_filebuf<char, std::char_traits<char> >::open (this=0x7fffffffdc68,
    __s=0x690550 <Log::fileName[abi:cxx11]+16> "simulation.log", __mode=17)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/fstream.tcc:187
#7  0x00007ffff7b45a53 in std::basic_filebuf<char, std::char_traits<char> >::open (
    __mode=<optimized out>, __s=..., this=0x7fffffffdc68)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/fstream:308
#8  std::basic_ofstream<char, std::char_traits<char> >::open (this=0x7fffffffdc60, __s=...,
    __mode=<optimized out>)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/fstream:823
#9  0x0000000000421c33 in Log::Log (this=0x7fffffffdc60) at src/log.cpp:21
#10 0x0000000000446198 in LinkCsma::iterate2Receive (this=0x6a9440, t=@0x7fffffffe1fc: 9387)
    at src/link_csma.cpp:329
#11 0x00000000004232a4 in Node::iterate2Receive (this=0x6a52f0, t=@0x7fffffffe1fc: 9387)
    at src/node.cpp:550
#12 0x000000000042f2bc in DirectionalNodes::iterate (this=0x6a6eb0, t=@0x7fffffffe1fc: 9387)
    at src/directional_nodes.cpp:250
#13 0x0000000000460ce5 in Simulation::run (this=0x6a6f00) at src/simulation.cpp:184
#14 0x0000000000429e8c in main (argc=1, argv=0x7fffffffe878) at src/main.cpp:65
程序接收信号SIGSEGV,分段故障。
来自/usr/lib/libc.so.6的malloc_consolidate()中的0x00007ffff7210289
(gdb)回溯
#0 0x00007FF7210289,位于/usr/lib/libc.so.6中的malloc_consolidate()中
#1 0x00007FF7211D2A位于/usr/lib/libc.so.6的_int_malloc()中
#来自/usr/lib/libc.so.6的malloc()中的2 0x00007ffff7213d44
#新操作员中的3 0x00007FF7AE1A48(sz=8192)
at/build/gcc/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50
#运算符新[]中的4 0x00007FF7AE1AF5(sz=)
at/build/gcc/src/gcc/libstdc++-v3/libsupc++/new_opv.cc:32
#5 0x00007FF7B41698标准::基本文件BUF::\U M\U分配\U内部缓冲区
(此参数=0x7FFFFFDC68)
at/build/gcc/src/gcc build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/fstream.tcc:55
#std::basic_filebuf::open中的6 0x00007ffff7b45932(此=0x7fffffffdc68,
__s=0x690550“simulation.log”,__模式=17)
at/build/gcc/src/gcc build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/fstream.tcc:187
#标准中的7 0x00007FF7B45A53::基本文件BUF::打开(
__模式=,_s=…,此=0x7fffffffdc68)
at/build/gcc/src/gcc build/x86_64-pc-linux-gnu/libstdc++-v3/include/fstream:308
#8 std::basic_of stream::open(this=0x7fffffffdc60,__s=。。。,
__模式=)
at/build/gcc/src/gcc build/x86_64-pc-linux-gnu/libstdc++-v3/include/fstream:823
#在src/Log.cpp:21处的Log::Log(this=0x7fffffffdc60)中的9 0x0000000000421c33
#LinkCsma::iterate2Receive中的10 0x0000000000446198(this=0x6a9440,t=@0x7fffffffe1fc:9387)
在src/linkcsma.cpp:329
#11节点中的0x00000000004232a4::iterate2Receive(this=0x6a52f0,t=@0x7fffffffe1fc:9387)
在src/node.cpp:550处
#DirectionalNodes中的12 0x000000000042f2bc::iterate(this=0x6a6eb0,t=@0x7fffffffe1fc:9387)
在src/directional_节点处。cpp:250
#13 0x0000000000460ce5在模拟中::在src/Simulation.cpp:184处运行(此=0x6a6f00)
#src/main的main中有14个0x0000000000429e8c(argc=1,argv=0x7fffffffe878)。cpp:65
日志类中的代码是:

#ifndef LOG_H__
#define LOG_H__

#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
#include <ctime>

#ifdef DEBUG
    #define LOG(level) \
    Log().write(level)
#else
    #define LOG(level) \
    if(level >= logDEBUG) ; \
    else Log().write(level)
#endif

enum LogLevel {
    logERROR,
    logWARNING,
    logINFO,
    logDEBUG
};

class Log
{
    public:
        Log();
        ~Log();
        static void setFile(const std::string& s);
        std::ofstream& write(LogLevel level = logINFO);
    protected:
        std::ofstream fileStream;
    private:
        Log(const Log&);
        Log& operator =(const Log&);
        std::string level2String(LogLevel level);
        std::string timeString();

        static std::string fileName;
        static const std::string DEFAULT_LOG_FILE;
};

#endif //LOG_H__
\ifndef LOG\u H__
#定义日志__
#包括
#包括
#包括
#包括
#包括
#ifdef调试
#定义日志(级别)\
Log().write(级别)
#否则
#定义日志(级别)\
如果(级别>=logDEBUG)\
else Log().write(级别)
#恩迪夫
枚举日志级别{
日志错误,
日志警告,
logINFO,
日志调试
};
类日志
{
公众:
Log();
~Log();
静态void setFile(const std::string&s);
std::ofstream&write(LogLevel=logINFO);
受保护的:
流文件流的std::of;
私人:
日志(const Log&);
日志和运算符=(常量日志-);
std::stringlevel2string(日志级别);
std::string timeString();
静态std::字符串文件名;
静态常量std::字符串默认日志文件;
};
#endif//LOG\u H__
以及:

#include“./include/log.h”
使用名称空间std;
字符串日志::文件名;
const string Log::DEFAULT\u Log\u FILE=“logfile.Log”;
Log::Log()
{
fileStream.open(文件名,ios::out | ios::app);
}
日志::~Log()
{

fileStream似乎是我通过将输出从ofstream更改为FILE*,并在使用fprintf写入文件*之前使用ostringstream作为缓冲区来解决的。如所示。

文件名以前是通过调用setFile方法设置的。尽管在签入open调用时,gdb只显示“”.当你删除宏时会发生什么?@Beta:没有任何变化。我发现如果我只包含一行"我建议你准备一个。你可能会发现错误,否则你可以发布一个更简单的问题。仍然不能完全解决我原来的问题,但我无法在程序外使用该类时重现错误,所以我现在知道为什么会被否决。至少我学会了如何提出更好的问题。谢谢你的com(贝塔)
#ifndef LOG_H__
#define LOG_H__

#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
#include <ctime>

#ifdef DEBUG
    #define LOG(level) \
    Log().write(level)
#else
    #define LOG(level) \
    if(level >= logDEBUG) ; \
    else Log().write(level)
#endif

enum LogLevel {
    logERROR,
    logWARNING,
    logINFO,
    logDEBUG
};

class Log
{
    public:
        Log();
        ~Log();
        static void setFile(const std::string& s);
        std::ofstream& write(LogLevel level = logINFO);
    protected:
        std::ofstream fileStream;
    private:
        Log(const Log&);
        Log& operator =(const Log&);
        std::string level2String(LogLevel level);
        std::string timeString();

        static std::string fileName;
        static const std::string DEFAULT_LOG_FILE;
};

#endif //LOG_H__
#include "../include/log.h"

using namespace std;

string Log::fileName;
const string Log::DEFAULT_LOG_FILE = "logfile.log";

Log::Log()
{
    fileStream.open(fileName, ios::out | ios::app);
}

Log::~Log()
{
    fileStream << endl;
    flush(fileStream);
    fileStream.close();
}

std::string Log::level2String(LogLevel level)
{
    static const char* const buffer[] = { "ERROR", "WARNING", "INFO", "DEBUG" };
    return buffer[level];
}

void Log::setFile(const std::string& s)
{
    fileName = s;
}

std::string Log::timeString()
{
    time_t t = time(0);
    struct tm * now = localtime(&t);
    stringstream ss;

    ss << now->tm_hour << ":" << now->tm_min << " "
        << now->tm_mday << "/" << (now->tm_mon+1) << "/" <<     (now->tm_year+1900);

    return ss.str();
}

std::ofstream& Log::write(LogLevel level)
{
    fileStream << timeString();
    fileStream << " " << level2String(level) << ":\t";
    return fileStream;
}