Possible memory leak in ManagedDataStream

Tubulii

28-08-2012 12:12:27

I recently started using my own resource system and now I delegate all resources to ogre, but I notified that although I used (/had to use) shared pointers (DataStreamPtr) my filestreams were not closed.

I digged into mogre's source code and found out, that the shared ptrs are all destroyed correctly (after some bug fixes of my code ;) ) but the ManagedDataStream which wraps my filestream does not call "close" if it is destroyed.
~DataStream_Proxy()
{
_managed->_native = 0;
}

It just dropps the pointer to the native Datastream (which delegates the native calls to the managed stream). If you just want to get some data into ogre and you have still a reference to the managed stream its ok, but otherwise its a memory leak. I guess that this case is quite special but think it is not wrong to implement a work around/fix for this.
More or less i have extended the destructor:
~DataStream_Proxy()
{
if (autoCleanup)
{
//Close managed stream
this->close();
//set native stream field to null (in fact this instance is the native stream!)
_managed->_native = 0;
//Free the gc handle to the wrapper object
delete _managed;
}
else _managed->_native = 0;
}

Added a bool field "autoCleanup" to both ManagedDataStream and DataStreamProxy. In addition I extended the ManagedDataStream in a not API breaking way:
ManagedDataStream(System::IO::Stream^ stream) : DataStream( _createNative(stream) )
{
_createdByCLR = true;
}

ManagedDataStream(System::IO::Stream^ stream, bool AutoCleanup) : DataStream( _createNative(stream) )
{
_createdByCLR = true;
this->AutoCleanup = AutoCleanup;
}


Should I upload a patch? I.e. for the Autowrapper?