连接 std::basic_ofstream到 FIFO。 bad_cast 异常
在 Linux 2.6.32 上使用 gcc 4.4.3,将 std::basic_ofstream 连接到 FIFO 时出现 bad_cast 异常。
单步调试器,我可以看到错误是在标准库中的各个位置生成的,因为流或 filebuf 对象的 _M_codecvt 成员为 NULL。它发生的确切位置取决于操作的顺序,但每个操作的原因似乎都是相同的。
那么我在这里做了一些根本上愚蠢的事情吗? ofstream 和 ifstream 工作正常。是否有某种原因表明您不应该将除 char 之外的任何内容附加到 FIFO?
提前致谢。
编辑:添加源代码。
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sys/stat.h>
#include <pthread.h>
using namespace std;
//#define CHAR char
#define CHAR unsigned char
bool still_writing = true;
void* reader (void*)
{
basic_ifstream<CHAR> in ("FIFO", fstream::in);
basic_streambuf<CHAR> *stream_buffer = in.rdbuf();
bool no_reads = true;
CHAR buffer[10];
while (stream_buffer->in_avail() ||
still_writing ||
no_reads)
{
in.readsome(buffer, 10);
const int got = (int)in.gcount();
if (got > 0) {
std::cerr << "Got something!\n";
no_reads = false;
}
}
}
int main ()
{
mkfifo ("FIFO", S_IRUSR | S_IWUSR);
pthread_t reader_thread_id;
pthread_create (&reader_thread_id, NULL, reader, NULL);
basic_ofstream<CHAR> out ("FIFO");
out << (CHAR) 70;
still_writing = false;
out.flush();
pthread_join (reader_thread_id, NULL);
remove ("FIFO");
}
在此版本中,异常源自 stream_buffer->in_avail()
如果交换 #define
语句,则一切正常。
Using gcc 4.4.3 on Linux 2.6.32, I get bad_cast exceptions when connecting std::basic_ofstream to a FIFO.
Stepping though the debugger, I can see that the error is generated at various places in the standard library because the _M_codecvt member of the stream or filebuf object is NULL. Exactly where it happens depends on the order of operations, but it appears to be the same cause in each.
So am I doing something fundamentally stupid here? ofstream and ifstream work fine. Is there some reason that you shouldn't attach a stream of anything besides char to a FIFO?
Thanks in advance.
EDIT: Adding source code.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sys/stat.h>
#include <pthread.h>
using namespace std;
//#define CHAR char
#define CHAR unsigned char
bool still_writing = true;
void* reader (void*)
{
basic_ifstream<CHAR> in ("FIFO", fstream::in);
basic_streambuf<CHAR> *stream_buffer = in.rdbuf();
bool no_reads = true;
CHAR buffer[10];
while (stream_buffer->in_avail() ||
still_writing ||
no_reads)
{
in.readsome(buffer, 10);
const int got = (int)in.gcount();
if (got > 0) {
std::cerr << "Got something!\n";
no_reads = false;
}
}
}
int main ()
{
mkfifo ("FIFO", S_IRUSR | S_IWUSR);
pthread_t reader_thread_id;
pthread_create (&reader_thread_id, NULL, reader, NULL);
basic_ofstream<CHAR> out ("FIFO");
out << (CHAR) 70;
still_writing = false;
out.flush();
pthread_join (reader_thread_id, NULL);
remove ("FIFO");
}
In this version, the exception arises from stream_buffer->in_avail()
If you swap the #define
statements, everything is fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有类似的问题。我发现不能设置
skipws
标志。确保使用std::noskipws(your_stream_obj)
。我将其调试到 gcc 代码中并找出了发生这种情况的原因,但已经忘记了。希望有帮助...
I had a similar issue. What I found out is that setting a
skipws
-flag must not be set. To ensure that usestd::noskipws(your_stream_obj)
. I debugged it into the gcc-code and found out why that happened, but forgot it already.Hope that helps...