Re: CByteBuffer implementation passed between modules
Well duh, RTL stands for Run Time Library, doesn't it?
Nope. Stands for Right-To-Left :-)
CRT is usualy "C Runtime Library"
Ok, after the cheep joke, something serious.
I would recomend making sure the crash is actually caused by the exe/dll
scenario.
In many cases you get a crash at delete because there is a copy happening
somewhere.
When you have allocated resources like this, you allways have to define
a copy-constructor and the assignement operator.
In there you either have to do a ref-count (with a lot of care, considering
what happens when several objects modify the same buffer), or make a copy of
the buffer (safer and easyer).
Let's say you have your class:
CByteBuffer a, b;
a.Set( buf, 10 );
b = a;
Now, b will use the default assignment (copy member by member), meaning the
b.m_pBuf will point to the same buffer as a.m_pBuf
At destruction time, the second object being distroyed will try to delete a
pointer that was already deleted. Crash!
Other problems I can think of (not for this crash, but for others :-)
Set (LPVOID p, size_t size)
{
m_pBuf = new BYTE[size];
memcpy (m_pBuf, p, size);
};
Now consider this:
CByteBuffer a;
a.Set( buf, 10 );
a.Set( buf, 20 );
Memory leak!
Then you also need the size of the source buffer.
CByteBuffer a;
BYTE *buf = new BYTE[10];
a.Set( buf, 10000000 );
Set will try to access way beyond the end of buffer, with a (possible)
violation. Crash!
Mihai
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email