Hello.

We have Client/server socket application (multiple clients and servers).
Servers are multiplatform can be compiled for Windows (MSVC) and for Linux
(GCC).
Recently we detected memory leak in one of kinde of our servers. At the start
it uses only 15m (that is normally). Then it slowly grows up to 2000m within 2
weeks.
        Only Linux+GCC version was affected by this problem. We have checked
our server by valgrind. It detected leak. The leak is possible in
std::vectot<int>::reserve()or in std::vectot<int>::clear()

I use GCC 3.4.5 builded and installed on Fedora Core 2
REproduceable: Always.

The code producing leak:

...

typedef int card_type;
typedef int Funds;                                                      ….

const size_t MAX_CARDS_ON_HAND = 7;

typedef std::vector<card_type> CardsSet;
...

//----------------------------------------------------------------------------
class CBaseSeat : public ISerializable
{
public:
    CBaseSeat();
    void standUpSeat();
...
protected:
    unsigned m_flags;
    Funds m_cash;
    Funds m_bet;
    CardsSet m_cardsSet;
};

//----------------------------------------------------------------------------
CBaseSeat::CBaseSeat() 
:   m_flags(0),   m_cash(0),   m_bet(0),  m_cardsSet()
{
    m_cardsSet.reserve( MAX_CARDS_ON_HAND );
    standUpSeat();
}

//----------------------------------------------------------------------------
void CBaseSeat::standUpSeat()
{
    m_flags = 0;
    m_cash = 0;
    m_bet = 0;
    m_cardsSet.clear();
}


When we comment this line
//    m_cardsSet.reserve( MAX_CARDS_ON_HAND );

The leak is Gone. I just can suppose that the problem in reserve() or in
clear() method whick called imediately after reserve();


Please see Valgrind report:  (after several minutes of work)

==22501== 124,376 bytes in 4,442 blocks are definitely lost in loss record 6 of
6
==22501==    at 0x43B8B10: operator new(unsigned) (vg_replace_malloc.c:164)
==22501==    by 0x806B2F9: __gnu_cxx::new_allocator<int>::allocate(unsigned,
void const*) (new_allocator.h:81)
==22501==    by 0x806B191: std::_Vector_base<int, std::allocator<int>
>::_M_allocate(unsigned) (stl_vector.h:113)
==22501==    by 0x807DD9C: int* std::vector<int, std::allocator<int>
>::_M_allocate_and_copy<int*>(unsigned, int*, int*) (stl_vector.h:715)
==22501==    by 0x807DC96: std::vector<int, std::allocator<int>
>::reserve(unsigned) (vector.tcc:78)
==22501==    by 0x80842E7: Poker::CBaseSeat::CBaseSeat() (seat.cpp:20)
==22501==    by 0x807123D: Poker::CClientSeat::CClientSeat(Poker::CClientSeat
const&) (seat.h:134)
==22501==    by 0x80711FC: void std::_Construct<Poker::CClientSeat,
Poker::CClientSeat>(Poker::CClientSeat*, Poker::CClientSeat const&)
(stl_construct.h:81)
==22501==    by 0x8072FB8: __gnu_cxx::__normal_iterator<Poker::CClientSeat*,
std::vector<Poker::CClientSeat, std::allocator<Poker::CClientSeat> > >
std::__uninitialized_fill_n_aux<__gnu_cxx::__normal_iterator<Poker::CClientSeat*,
std::vector<Poker::CClientSeat, std::allocator<Poker::CClientSeat> > >,
unsigned, Poker::CClientSeat>(__gnu_cxx::__normal_iterator<Poker::CClientSeat*,
std::vector<Poker::CClientSeat, std::allocator<Poker::CClientSeat> > >,
unsigned, Poker::CClientSeat const&, __false_type) (stl_uninitialized.h:194)
==22501==    by 0x8072C5B: __gnu_cxx::__normal_iterator<Poker::CClientSeat*,
std::vector<Poker::CClientSeat, std::allocator<Poker::CClientSeat> > >
std::uninitialized_fill_n<__gnu_cxx::__normal_iterator<Poker::CClientSeat*,
std::vector<Poker::CClientSeat, std::allocator<Poker::CClientSeat> > >,
unsigned, Poker::CClientSeat>(__gnu_cxx::__normal_iterator<Poker::CClientSeat*,
std::vector<Poker::CClientSeat, std::allocator<Poker::CClientSeat> > >,
unsigned, Poker::CClientSeat const&) (stl_uninitialized.h:219)
==22501==    by 0x8072682: std::vector<Poker::CClientSeat,
std::allocator<Poker::CClientSeat>
>::_M_fill_insert(__gnu_cxx::__normal_iterator<Poker::CClientSeat*,
std::vector<Poker::CClientSeat, std::allocator<Poker::CClientSeat> > >,
unsigned, Poker::CClientSeat const&) (vector.tcc:314)
==22501==    by 0x8071CF3: std::vector<Poker::CClientSeat,
std::allocator<Poker::CClientSeat>
>::insert(__gnu_cxx::__normal_iterator<Poker::CClientSeat*,
std::vector<Poker::CClientSeat, std::allocator<Poker::CClientSeat> > >,
unsigned, Poker::CClientSeat const&) (stl_vector.h:612)
==22501==    by 0x8070CAC: std::vector<Poker::CClientSeat,
std::allocator<Poker::CClientSeat> >::resize(unsigned, Poker::CClientSeat
const&) (stl_vector.h:398)
==22501==    by 0x806FE22: std::vector<Poker::CClientSeat,
std::allocator<Poker::CClientSeat> >::resize(unsigned) (stl_vector.h:412)
==22501==    by 0x806C0AB: Poker::CClientGameState::read(std::istream&)
(clientgamestate.cpp:144)
==22501==    by 0x80B4430:
Poker::CSelfHolder<Poker::CClientGameState>::read(std::istream&) (holder.h:147)
==22501==    by 0x808BE86: Poker::CMessage::read(std::istream&)
(message.cpp:71)
==22501==    by 0x80A8D54:
Poker::CMessageFactory::createFromStream(Poker::CSession*, std::istream&)
(messagesfactory.cpp:1206)
==22501==    by 0x80E49F1: Poker::CRawDistiler::read(Poker::ISocket*,
Poker::CServerSession*) (distiler.cpp:180)
==22501==    by 0x80E29E5: Poker::CConnection::recieveMessages()
(connection.cpp:94)
==22501==    by 0x80E29A3: Poker::CConnection::doWork() (connection.cpp:81)
==22501==    by 0x80E6E49: Poker::CLobbyClient::doWork() (lobbyclient.cpp:123)
==22501==    by 0x80F827D: Poker::CBot::doWork() (bot.cpp:174)
==22501==    by 0x80FE742: Poker::CBotServer::doWork() (botserver.cpp:32)
==22501==    by 0x81028DB: main (main.cpp:35)


Hope my information will be usefull


-- 
           Summary: Polible memory leak in std::vector<int>::reserve() or
                    std::vector<int>::clear()
           Product: gcc
           Version: 3.4.5
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ksharenkov at ya dot ru
 GCC build triplet: any compiler settings
  GCC host triplet: Intel Celeron 1.7G  768RAM  Linux ( Fedora Core 2 )
GCC target triplet: executable


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27530

Reply via email to