Boost logo

Boost Users :

Subject: Re: [Boost-users] polymorphic archive use and memory leak
From: sam p (sameersp21_at_[hidden])
Date: 2008-12-03 00:12:53


Looks like something is wrong over here.
If I check for memory leak using valgrind on Linux (or Boundschecker on
Windows) I get memory leak with below given code.
Here is some info on the same,
******************************************
==6794== 80 bytes in 1 blocks are definitely lost in loss record 8 of 11
==6794== at 0x40219DC: operator new(unsigned) (in
/usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==6794== by 0x804C1AB:
boost::archive::detail::basic_oarchive::basic_oarchive() (in
/home/sameer/work/a.out)
==6794== by 0x804BF84:
boost::archive::detail::common_oarchive<boost::archive::text_oarchive>::common_oarchive()
(in /home/sameer/work/a.out)
==6794== by 0x804BFAC:
boost::archive::basic_text_oarchive<boost::archive::text_oarchive>::basic_text_oarchive()
(in /home/sameer/work/a.out)
==6794== by 0x804BFFC:
boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::text_oarchive_impl(std::ostream&,
unsigned) (in /home/sameer/work/a.out)
==6794== by 0x804C08E:
boost::archive::detail::polymorphic_oarchive_impl<boost::archive::text_oarchive_impl<boost::archive::text_oarchive>
>::polymorphic_oarchive_impl<char, std::char_traits<char>
>(std::basic_ostream<char, std::char_traits<char> >&, unsigned) (in
/home/sameer/work/a.out)
==6794== by 0x8049B5A: func(std::basic_stringstream<char,
std::char_traits<char>, std::allocator<char> >&,
boost::archive::polymorphic_oarchive*&) (in /home/sameer/work/a.out)
==6794== by 0x8049BE0: main (in /home/sameer/work/a.out)
******************************************

But if I modify below given code as given below then it doesn't show any
memory leak.
I am using boost 1.32.
***********************************
changed code
**********************************
9 boost::archive::polymorphic_oarchive *ptr = NULL;
10 func(ss, ptr);
11 /* delete ptr; //Will this delete object properly????? */
         boost::archive::polymorphic_text_oarchive *p =
dynamic_cast<boost::archive::polymorphic_text_oarchive*>(ptr);
         delete p; //Deleted after NULL check

**********************************

Thanks,
Sameer

On 12/2/08, Steven Watanabe <watanabesj_at_[hidden]> wrote:
>
> AMDG
>
> sam p wrote:
>
>> I am using polymorphic archive in my code and I am using it as follows,
>> 1 void func(std::stringstream &ss,
>> boost::archive::polymorphic_oarchive *&ptr)
>> 2 {
>> 3 ptr = new boost::archive::polymorphic_text_oarchive(ss);
>> 4 return;
>> 5 }
>> 6 int main()
>> 7 {
>> 8 std::stringstream ss;
>> 9 boost::archive::polymorphic_oarchive *ptr = NULL;
>> 10 func(ss, ptr);
>> 11 delete ptr; //Will this delete object properly?????
>> 12 }
>> Now the question is, whether 'delete' one line no. 11 will work properly
>> or not?
>> And is this a right way to use polymorphic archive?
>>
>
> Yes. polymorphic_oarchive has a virtual destructor.
>
> In Christ,
> Steven Watanabe
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



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