error in freeing alocate memory

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi
i have a class named IBuffer in my library named ICore.DLL.
i use this class inside another DLL named INet.DLL.
the purpose of IBuffer is to provide me a mechanism to read and write into a buffer. inside of INet.dll i have classes to send and recieve data to/from network and also serializing and deserializing messages that must be send on the network.
my problem with IBuffer is that whenever this class goes to be destroyed i try to free dynamically allocated memory by calling free api but i got a message indicate me that this buffer is not a valid heap pointer.<br/>
<br/>
IBuffer cunstructor :
<pre class="prettyprint" style=" IBuffer::IBuffer( size_t sz )
: currPosition(0), ptrSet(false) ,_buffer(NULL),_size(0),cleanedUp(false)
{
if (sz >0)
{
if((_buffer = (uint8_t*)calloc(sz,sizeof(uint8_t))) == NULL)
{
std::cout<<"Error in allocating memory for buffern";
}
ZeroMemory(_buffer,sz);
allocated = sz;
}
}[/code]
IBuffer Destructor :
<pre class="prettyprint" style=" IBuffer::~IBuffer(void)
{
/*_size = 0;
free(_buffer);

SetPosition(0);*/
if (cleanedUp)// if before destructor user cleared buffer this flag is true
{
assert(_buffer==NULL);
}
else
{
free(&_buffer);
}
std::cout<<"Ibuffer Freed n";
}[/code]
<br/>


use of this class inside INet.dll
<pre class="prettyprint" style=" SendMessage(Message msg)
{
.
.
.
.
SharedPtr<Utils::IBuffer> buff = SharedPtr<IBuffer>(new IBuffer(len));
buff->Write(data,len);

..
} // when leave this function destructor of IBuffer causes this error : Debug Assertion failed.[/code]
these two library linked correctly and have same compiler and build settings.

why destroying IBuffer causes invalid heap pointer here?
Code:
      msvcr100d.dll!__free_dbg_nolock()  + 0x141 bytes    <br/>
     msvcr100d.dll!__free_dbg()  + 0x50 bytes    <br/>
     msvcr100d.dll!_free()  + 0x10 bytes    <br/>
>    ICore.dll!Utils::IBuffer::~IBuffer()  Line 45 + 0xf bytes    C++<br/>
     IRNet.dll!Utils::IBuffer::`scalar deleting destructor()  + 0x2e bytes    C++<br/>
     IRNet.dll!Object::checked_delete<Utils::IBuffer>(Utils::IBuffer * x=0x00897ba0)  Line 36 + 0x2b bytes    C++<br/>
     IRNet.dll!Object::checked_deleter<Utils::IBuffer>::operator()(Utils::IBuffer * x=0x00897ba0)  Line 47 + 0x9 bytes    C++<br/>
     IRNet.dll!Object::sp_counted_base_impl<Utils::IBuffer *,Object::checked_deleter<Utils::IBuffer> >::dispose()  Line 198    C++<br/>
     IRNet.dll!Object::sp_counted_base::release()  Line 127 + 0xf bytes    C++<br/>
     IRNet.dll!Object::shared_count::~shared_count()  Line 252    C++<br/>
     IRNet.dll!Object::SharedPtr<Utils::IBuffer>::~SharedPtr<Utils::IBuffer>()  + 0x2e bytes    C++<br/>
     IRNet.dll!IRNetwork::IStun::SendUnReliably(IRNetwork::IStunMessage * message=0x00897068)  Line 247 + 0x1b bytes    C++<br/>
     IRNet.dll!IRNetwork::IStun::SendBind()  Line 61 + 0xc bytes    C++<br/>
     IRNet.dll!IRNetwork::IStun::Bind(unsigned int localfd=180)  Line 48    C++<br/>
     IRNet.dll!IRNetwork::INatContext::StunBind(unsigned int fd=180)  Line 52 + 0xc bytes    C++<br/>
     testDhcpv4.exe!TestStunAttribute::Start()  Line 48 + 0x24 bytes    C++<br/>
     testDhcpv4.exe!wmain(int argc=1, wchar_t * * argv=0x00894800)  Line 70    C++<br/>
     testDhcpv4.exe!__tmainCRTStartup()  Line 552 + 0x19 bytes    C<br/>
     testDhcpv4.exe!wmainCRTStartup()  Line 371    C<br/>
     kernel32.dll!@BaseThreadInitThunk@12()  + 0x12 bytes    <br/>
     ntdll.dll!___RtlUserThreadStart@8()  + 0x27 bytes    <br/>
     ntdll.dll!__RtlUserThreadStart@8()  + 0x1b bytes   
 
 [code] 
  
< 
 #WebCan Video Conference System Based ON SL# 
<br/>
<br/>

[url=http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/2f2354e3-aef3-4def-b30b-037ed6fe83ec]View the full article[/url]
 
Back
Top