Hello,
I have a derived class, that I'm trying to delete. This causes a segmentation error, though.
I'm fully aware of the virtual destructor thang, but I'm using it all the way around, and still this issue occurs.
I'll post my headers only, please tell if more is required.
DataStream.h
class DataStream
{
public:
DataStream(const ACCESS_MODE& access, const String& name = "") : m_Access(access), m_Name(name)
{
};
virtual ~DataStream(){}
virtual size_t read(void* buffer, const size_t& count) = 0;
virtual size_t write(const void* buffer, const size_t& count)
{
return 0;
}
virtual void close() = 0;
const String& getName()
{
return m_Name;
}
const ACCESS_MODE& getAccessMode()
{
return m_Access;
}
const size_t& getSize()
{
return m_Size;
}
bool isReadable()
{
return m_Access != ACCESS_NONE;
}
bool isWriteable()
{
return m_Access == ACCESS_WRITE;
}
virtual String readAsString()
{
char* buf = new char[m_Size+1];
read(buf, m_Size);
buf[m_Size] = '\0';
String ret;
ret.insert(0, buf, m_Size);
delete buf;
return ret;
}
protected:
size_t m_Size;
ACCESS_MODE m_Access;
private:
String m_Name;
};
FileSystemDataStream.h
#include "DataStream.h"
class FileSystemDataStream : public DataStream
{
public:
FileSystemDataStream(std::fstream* stream, bool autoFree = true, const String& name = "");
FileSystemDataStream(std::ifstream* stream, bool autoFree = true, const String& name = "");
virtual ~FileSystemDataStream(){} // tried having it calling close() too.
size_t read(void* buffer, const size_t& count);
size_t write(const void* buffer, const size_t& count);
void close();
private:
void _calculateSize();
bool m_AutoFree;
std::istream* m_IStream;
std::fstream* m_OStream;
std::ifstream* m_ReadOnlyStream;
};
The object is allocated by a factory:
DataStream* Factory::open(const String& filename) const
{
std::fstream* stream = new std::fstream;
stream->open((m_Path+filename).c_str(), std::ios::in | std::ios::binary);
if(stream->fail())
{
delete stream;
return NULL;
}
FileSystemDataStream* dstream = new FileSystemDataStream(stream, true, filename);
return dstream;
}
And then deleted right afterwards - result: segmentation fault.
I hope anyone can tell me why. I've been looking around to solve this issue for some weeks now, but I'm ending up with too many commented out delete calls ^^.
Thank you in advance!