Boost logo

Boost Users :

From: Youhei Murakami (ym555_at_[hidden])
Date: 2007-12-03 12:30:52


Hello everyone. I had met a memory leak error runnnin program generated
from a source which is attached to this mail.
[flow]
--The "sampleclass"-class has a wstring object as a member variable and
a setter and a getter for this variable.
--A sampleclass instance generated by the default new.
--A pointer to sampleclass instance attached to intrusive_ptr<sampleclass>.
--Make wstring instance with string L"0123456789abcdef".
--The wstring instance set to a sampleclass instance using the
sampleclass's setter.
--Finish this program and a memory leak occurs.
[emvironment]
WindowsXP 64bit
VisualStudio2005 sp1
boost-1_34_1
Unicode Charset



#include "stdafx.h"

#include <iostream>
#include <boost/intrusive_ptr.hpp>
#include <boost/detail/atomic_count.hpp>
#include <boost/config.hpp>
using namespace std;
using namespace boost;

#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#define new ::new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

namespace test {
        class countable {
        public:
                countable() : refcount(0) { }
                void add_ref( void ) { ++refcount; }
                void release( void ) {
                        if( 0 == --refcount ) {
                                delete this;
                        }
                }
                int getcount() { return static_cast<long>(refcount); }
        private:
                boost::detail::atomic_count refcount;
        };
        class sampleclass : public countable {
                wstring message;
        public:
                sampleclass(){}
                sampleclass( const wstring &mes ) : message(mes) {}
                void setmessage( const wstring &mes ) {
                        message = mes;
                }
                wstring getmessage() { return message; }
        };
};

namespace boost {
        using namespace test;
        inline void intrusive_ptr_add_ref(countable *p) {
                p->add_ref();
        }
        inline void intrusive_ptr_release(countable *p) {
                p->release();
        }
}

int _tmain(int argc, _TCHAR* argv[])
{
        _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
        using namespace test; {
                intrusive_ptr<sampleclass> intrusive_( new sampleclass(wstring(L"initial")) );
                //---------- memory leak prone ------------------------//
                wstring mes( L"0123456789abcdef" );
                intrusive_->setmessage( mes );
                //---------- memory leak prone ------------------------//
                wcout << intrusive_->getmessage().c_str() << endl;
        }
        getchar();
        return EXIT_SUCCESS;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net