C++:ofStream.Close() 上的 SIGABRT

发布于 2024-10-20 13:51:02 字数 3347 浏览 2 评论 0原文

我在 ofstream 文件 close() 上收到 SIGABORT。顺便说一句,这是自上一个工作版本以来我没有更改过的函数的一部分。以下是 Valgrind 的输出。我不知道问题出在哪里。谁能帮我解决这个问题。

==11082== Invalid write of size 4
==11082==    at 0x40EA72A: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string() (basic_string.h:255)
==11082==    by 0x8050408: SFile::SFile() (SFile.cc:11)
==11082==    by 0x8056A5E: DFile::Create(char*, fType, void*) (DFile.cc:55)
==11082==    by 0x8059D02: test1() (test.cc:48)
==11082==    by 0x805A628: main (test.cc:163)
==11082==  Address 0x4331f64 is 0 bytes after a block of size 52 alloc'd
==11082==    at 0x4026351: operator new(unsigned int) (vg_replace_malloc.c:255)
==11082==    by 0x8056A52: DFile::Create(char*, fType, void*) (DFile.cc:55)
==11082==    by 0x8059D02: test1() (test.cc:48)
==11082==    by 0x805A628: main (test.cc:163)
==11082== 
==11082== Syscall param write(buf) points to uninitialised byte(s)
==11082==    at 0x404A523: __write_nocancel (syscall-template.S:82)
==11082==    by 0x40B5515: std::basic_filebuf<char, std::char_traits<char> >::_M_convert_to_external(char*, int) (fstream.tcc:471)
==11082==    by 0x40B5661: std::basic_filebuf<char, std::char_traits<char> >::overflow(int) (fstream.tcc:426)
==11082==    by 0x40B5E57: std::basic_filebuf<char, std::char_traits<char> >::_M_terminate_output() (fstream.tcc:783)
==11082==    by 0x40B6227: std::basic_filebuf<char, std::char_traits<char> >::close() (fstream.tcc:154)
==11082==    by 0x40B8104: std::basic_ofstream<char, std::char_traits<char> >::close() (fstream:738)
==11082==    by 0x80508A2: SFile::Create(char*, fType, void*) (SFile.cc:45)
==11082==    by 0x8056AB5: DFile::Create(char*, fType, void*) (DFile.cc:61)
==11082==    by 0x8059D02: test1() (test.cc:48)
==11082==    by 0x805A628: main (test.cc:163)
==11082==  Address 0x4332278 is 16 bytes inside a block of size 8,192 alloc'd
==11082==    at 0x4025FE5: operator new[](unsigned int) (vg_replace_malloc.c:299)
==11082==    by 0x40B4BB2: std::basic_filebuf<char, std::char_traits<char> >::_M_allocate_internal_buffer() (fstream.tcc:54)
==11082==    by 0x40B63C1: std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) (fstream.tcc:102)
==11082==    by 0x40B7566: std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(char const*, std::_Ios_Openmode) (fstream:699)
==11082==    by 0x805080F: SFile::Create(char*, fType, void*) (SFile.cc:39)
==11082==    by 0x8056AB5: DFile::Create(char*, fType, void*) (DFile.cc:61)
==11082==    by 0x8059D02: test1() (test.cc:48)
==11082==    by 0x805A628: main (test.cc:163)
==11082== 

代码:

int SFile::Create (char *fPath, fType fileType, void *start)
{    
    _file.Open(0, fPath);
    _file.Close();

    int ftype = sorted;    
    sInfo *sInfo=(sInfo*)start; 
    ofstream metaF(fPath,ios::out|ios::binary|ios::ate);      
    metaF.write((char*)sInfo->oMaker,sizeof(oMaker));
    metaF.write((char*)&(sInfo->rLength),sizeof(int));
    metaF.close();  //here it throws SIGABORT when debugging
    return 1;
}

编辑:奇怪的是,当我添加以下内容时,SIGARBT 错误消失了: #include

为什么这没有给出编译错误?!!!

I am getting a SIGABORT on a ofstream file close(). Incidentally this is part of a function that I haven't changed since the last working version. The following is the Valgrind output. I did not get an idea regarding where the problem could be. Could anyone help me to figure this out.

==11082== Invalid write of size 4
==11082==    at 0x40EA72A: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string() (basic_string.h:255)
==11082==    by 0x8050408: SFile::SFile() (SFile.cc:11)
==11082==    by 0x8056A5E: DFile::Create(char*, fType, void*) (DFile.cc:55)
==11082==    by 0x8059D02: test1() (test.cc:48)
==11082==    by 0x805A628: main (test.cc:163)
==11082==  Address 0x4331f64 is 0 bytes after a block of size 52 alloc'd
==11082==    at 0x4026351: operator new(unsigned int) (vg_replace_malloc.c:255)
==11082==    by 0x8056A52: DFile::Create(char*, fType, void*) (DFile.cc:55)
==11082==    by 0x8059D02: test1() (test.cc:48)
==11082==    by 0x805A628: main (test.cc:163)
==11082== 
==11082== Syscall param write(buf) points to uninitialised byte(s)
==11082==    at 0x404A523: __write_nocancel (syscall-template.S:82)
==11082==    by 0x40B5515: std::basic_filebuf<char, std::char_traits<char> >::_M_convert_to_external(char*, int) (fstream.tcc:471)
==11082==    by 0x40B5661: std::basic_filebuf<char, std::char_traits<char> >::overflow(int) (fstream.tcc:426)
==11082==    by 0x40B5E57: std::basic_filebuf<char, std::char_traits<char> >::_M_terminate_output() (fstream.tcc:783)
==11082==    by 0x40B6227: std::basic_filebuf<char, std::char_traits<char> >::close() (fstream.tcc:154)
==11082==    by 0x40B8104: std::basic_ofstream<char, std::char_traits<char> >::close() (fstream:738)
==11082==    by 0x80508A2: SFile::Create(char*, fType, void*) (SFile.cc:45)
==11082==    by 0x8056AB5: DFile::Create(char*, fType, void*) (DFile.cc:61)
==11082==    by 0x8059D02: test1() (test.cc:48)
==11082==    by 0x805A628: main (test.cc:163)
==11082==  Address 0x4332278 is 16 bytes inside a block of size 8,192 alloc'd
==11082==    at 0x4025FE5: operator new[](unsigned int) (vg_replace_malloc.c:299)
==11082==    by 0x40B4BB2: std::basic_filebuf<char, std::char_traits<char> >::_M_allocate_internal_buffer() (fstream.tcc:54)
==11082==    by 0x40B63C1: std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) (fstream.tcc:102)
==11082==    by 0x40B7566: std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(char const*, std::_Ios_Openmode) (fstream:699)
==11082==    by 0x805080F: SFile::Create(char*, fType, void*) (SFile.cc:39)
==11082==    by 0x8056AB5: DFile::Create(char*, fType, void*) (DFile.cc:61)
==11082==    by 0x8059D02: test1() (test.cc:48)
==11082==    by 0x805A628: main (test.cc:163)
==11082== 

Code:

int SFile::Create (char *fPath, fType fileType, void *start)
{    
    _file.Open(0, fPath);
    _file.Close();

    int ftype = sorted;    
    sInfo *sInfo=(sInfo*)start; 
    ofstream metaF(fPath,ios::out|ios::binary|ios::ate);      
    metaF.write((char*)sInfo->oMaker,sizeof(oMaker));
    metaF.write((char*)&(sInfo->rLength),sizeof(int));
    metaF.close();  //here it throws SIGABORT when debugging
    return 1;
}

EDIT: Strangely the SIGARBT error went off when I added:
#include<string>

Why was this not giving a compilation error?!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2024-10-27 13:51:02

Ofstream 可能无法打开文件,您应该在执行任何操作之前检查您的流是否有效:

ofstream metaF(fPath,ios::out|ios::binary|ios::ate);
  if (metaF.good())
  {
    metaF.write((char*)sInfo->oMaker,sizeof(oMaker));
    metaF.write((char*)&(sInfo->rLength),sizeof(int));
    metaF.close();
  }
return 1;

Ofstream may fail to open a file, you should check if your stream is valid before any operation :

ofstream metaF(fPath,ios::out|ios::binary|ios::ate);
  if (metaF.good())
  {
    metaF.write((char*)sInfo->oMaker,sizeof(oMaker));
    metaF.write((char*)&(sInfo->rLength),sizeof(int));
    metaF.close();
  }
return 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文