H
hsmit.home
Hi everyone,
I'm having some difficulty with the following piece of code. I have
stripped it to it's bare minimum to demonstrate the problem at hand.
Compiler: MS Visual C++ 2005 Express Edition (similar problem arises
with 2008)
Runtime Library: All multi-threaded variants have been seen to fail
[DLL/Static] | [Debug|Release]
Purpose: define a user defined stream buffer that processes each
incoming character and translates it to an encoded value. Place the
encoded character into a local buffer for output. The most simple case
would be an encoder that translates each character to upper case. A
more complicated case would be an encoder that encodes plain-text to
base64 encoding (this is not a one-to-one character encoding, it's a 3
character to 4character encoding, this is why an internal buffer is
needed)
Problem: The code throws an "Unhandled exception at 0x00529bcc in
hsl_d.exe: 0xC0000005: Access violation reading location 0x00000000."
after the "encoderbuf::underflow c = 51" character '3' has been read.
This basically tells me that deep down in the internals of the IO
library something is being dereferenced that is not allocated.
Question(s):
1) Does this occur with other compilers? (requires testing)
2) Is this a problem with the IO library? (unlikely I think)
3) Am I doing something stupid? (more than likely) And if so what?
References:
C++ Standard Library - A Tutorial and Reference (13.13.3 User-Defined
Stream Buffers)
Code:
#include <iostream>
#include <sstream>
class encoderbuf : public std::streambuf {
char mCharBuf[128];
int mBufLen;
int mBufPos;
public:
//--------------------------------------------------------------
/** default constructor */
encoderbuf()
: std::streambuf()
, mBufLen(0)
, mBufPos(0)
{
}
//--------------------------------------------------------------
/** outgoing data */
virtual int_type underflow () {
int_type c = EOF;
if (mBufPos < mBufLen) {
c = mCharBuf[mBufPos++];
}
std::cout << "encoderbuf::underflow c = " << c << std::endl;
return c;
}
//--------------------------------------------------------------
/** incoming data */
virtual int_type overflow (int_type c) {
std::cout << "encoderbuf:
verflow c = " << c << std::endl;
//TODO: do encoding here
mCharBuf[mBufLen++] = c;
return c;
}
};
//--------------------------------------------------------------
int main (int argc, char ** argv) {
encoderbuf buf;
std::iostream iostr(&buf);
iostr << 12345 << std::endl;
std::stringstream sstr;
iostr >> sstr.rdbuf(); // EXCEPTION AT PROCESSING CHARACTER '3'
std::string str = sstr.str();
std::cout << "main str = " << str << std::endl;
}
Output:
encoderbuf:
verflow c = 49
encoderbuf:
verflow c = 50
encoderbuf:
verflow c = 51
encoderbuf:
verflow c = 52
encoderbuf:
verflow c = 53
encoderbuf:
verflow c = 10
encoderbuf::underflow c = 49
encoderbuf::underflow c = 50
encoderbuf::underflow c = 51
popup: Unhandled exception at 0x00529bcc in hsl_d.exe: 0xC0000005:
Access violation reading location 0x00000000.
I'm having some difficulty with the following piece of code. I have
stripped it to it's bare minimum to demonstrate the problem at hand.
Compiler: MS Visual C++ 2005 Express Edition (similar problem arises
with 2008)
Runtime Library: All multi-threaded variants have been seen to fail
[DLL/Static] | [Debug|Release]
Purpose: define a user defined stream buffer that processes each
incoming character and translates it to an encoded value. Place the
encoded character into a local buffer for output. The most simple case
would be an encoder that translates each character to upper case. A
more complicated case would be an encoder that encodes plain-text to
base64 encoding (this is not a one-to-one character encoding, it's a 3
character to 4character encoding, this is why an internal buffer is
needed)
Problem: The code throws an "Unhandled exception at 0x00529bcc in
hsl_d.exe: 0xC0000005: Access violation reading location 0x00000000."
after the "encoderbuf::underflow c = 51" character '3' has been read.
This basically tells me that deep down in the internals of the IO
library something is being dereferenced that is not allocated.
Question(s):
1) Does this occur with other compilers? (requires testing)
2) Is this a problem with the IO library? (unlikely I think)
3) Am I doing something stupid? (more than likely) And if so what?
References:
C++ Standard Library - A Tutorial and Reference (13.13.3 User-Defined
Stream Buffers)
Code:
#include <iostream>
#include <sstream>
class encoderbuf : public std::streambuf {
char mCharBuf[128];
int mBufLen;
int mBufPos;
public:
//--------------------------------------------------------------
/** default constructor */
encoderbuf()
: std::streambuf()
, mBufLen(0)
, mBufPos(0)
{
}
//--------------------------------------------------------------
/** outgoing data */
virtual int_type underflow () {
int_type c = EOF;
if (mBufPos < mBufLen) {
c = mCharBuf[mBufPos++];
}
std::cout << "encoderbuf::underflow c = " << c << std::endl;
return c;
}
//--------------------------------------------------------------
/** incoming data */
virtual int_type overflow (int_type c) {
std::cout << "encoderbuf:
//TODO: do encoding here
mCharBuf[mBufLen++] = c;
return c;
}
};
//--------------------------------------------------------------
int main (int argc, char ** argv) {
encoderbuf buf;
std::iostream iostr(&buf);
iostr << 12345 << std::endl;
std::stringstream sstr;
iostr >> sstr.rdbuf(); // EXCEPTION AT PROCESSING CHARACTER '3'
std::string str = sstr.str();
std::cout << "main str = " << str << std::endl;
}
Output:
encoderbuf:
encoderbuf:
encoderbuf:
encoderbuf:
encoderbuf:
encoderbuf:
encoderbuf::underflow c = 49
encoderbuf::underflow c = 50
encoderbuf::underflow c = 51
popup: Unhandled exception at 0x00529bcc in hsl_d.exe: 0xC0000005:
Access violation reading location 0x00000000.