Heap corruption when using wxArrayString on Win32

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
mr1x
Knows some wx things
Knows some wx things
Posts: 34
Joined: Mon Jan 05, 2009 3:24 am

Heap corruption when using wxArrayString on Win32

Post by mr1x » Sat Mar 14, 2009 5:55 pm

I am getting heap corruption when using wxArrayString on Windows (wxWidgets version is 2.8.9).

Code: Select all

   int num = 0;
   wxArrayString    testLines;
   while (1) {
      wxString newLine = wxString::FromAscii("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    
      testLines.Add(newLine);

      if (num == 10) {
         testLines.RemoveAt((size_t)0, (size_t)1);
         testLines.Shrink();
         num--;
      }
      num++;
   }
basically, heap is corrupted during first call to Shrink() (evidently), because the second time Shrink() runs - fails with heap corruption error.

(The code is a trivialization of string FIFO.)

Removing Shrink() call "solves" the problem, but then I am not sure if array does not keep growing and taking more memory?

Is this a bug?

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Sat Mar 14, 2009 11:10 pm

This piece of code runs fine on my computer (wxmac 2.8.9), so you might want to look into platform-specific issues...

mr1x
Knows some wx things
Knows some wx things
Posts: 34
Joined: Mon Jan 05, 2009 3:24 am

Post by mr1x » Sat Mar 14, 2009 11:16 pm

Auria wrote:This piece of code runs fine on my computer (wxmac 2.8.9), so you might want to look into platform-specific issues...
The platform is Windows XP SP3. Nothing special about it otherwise - vanilla installation, vanilla Visual Studio Express.

tan
Moderator
Moderator
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan » Sun Mar 15, 2009 8:40 am

Hi,
it looks like that is real bug!
Here sources of the wxArrayString::Shrink() (WXDIR/src/common/string.cpp, line 2322):

Code: Select all

// minimizes the memory usage by freeing unused memory
void wxArrayString::Shrink()
{
  // only do it if we have some memory to free
  if( m_nCount < m_nSize ) {
    // allocates exactly as much memory as we need
    wxChar **pNew = new wxChar *[m_nCount];

    // copy data to new location
    memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
    delete [] m_pItems;
    m_pItems = pNew;
  }
}
Actually, it must be:

Code: Select all

// minimizes the memory usage by freeing unused memory
void wxArrayString::Shrink()
{
  // only do it if we have some memory to free
  if( m_nCount < m_nSize ) {
    // allocates exactly as much memory as we need
    wxChar **pNew = new wxChar *[m_nCount];

    // copy data to new location
    memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
    delete [] m_pItems;
    m_pItems = pNew;
    m_nSize = m_nCount;  // <--- save new real size!
  }
}
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10

Post Reply