2

I am playing around with some variant and any classes and I try to figure out if there is a faster alternative to boost::spirit::hold_any or if it's the fastest soloution around. Is there something faster than this?

And maybe a subquestion, Dr Memory says any_hold is causing memory leaks, is that true? (I heard some things about that but i thought maybe it were fixed or only a rumor, I'm using Boost 1.57.0)

How reliable is Dr Memory in this case?

I executed mainly code like:

int main()
{
   boost::spirit::hold_any a;
   for (unsigned int i = 0; 150000 > i; ++i)
   {
       a = 5;
       a = 5.0;
   }

   return 0;
}

The Logfile of Dr Memory:

Dr. Memory version 1.8.1 build 0 built on Feb 17 2015 19:08:31
Dr. Memory results for pid 6208: "boostTest.exe"
Application cmdline: ""C:\Users\tim.junge\Documents\Visual Studio 2010\Projects\C++\Backup\boostTest\Debug\boostTest.exe""
Recorded 108 suppression(s) from default C:\Users\tim.junge\Documents\Visual Studio 2010\Projects\C++\DrMemory-Windows-1.8.1-RC1\bin\suppress-default.txt

Error #1: LEAK 8 direct bytes 0x00a801d8-0x00a801e0 + 0 indirect bytes
# 0 replace_operator_new                                       [d:\drmemory_package\common\alloc_replace.c:2613]
# 1 boost::spirit::basic_hold_any<>::assign<>                  [c:\boost_1_57_0\boost\spirit\home\support\detail\hold_any.hpp:293]
# 2 boost::spirit::basic_hold_any<>::operator=<>               [c:\boost_1_57_0\boost\spirit\home\support\detail\hold_any.hpp:305]
# 3 main                                                       [c:\users\tim.junge\documents\visual studio 2010\projects\c++\backup\boosttest\boosttest\main.cpp:50]

Error #2: LEAK 8 direct bytes 0x00a87810-0x00a87818 + 0 indirect bytes
# 0 replace_operator_new                                       [d:\drmemory_package\common\alloc_replace.c:2613]
# 1 boost::spirit::basic_hold_any<>::assign<>                  [c:\boost_1_57_0\boost\spirit\home\support\detail\hold_any.hpp:293]
# 2 boost::spirit::basic_hold_any<>::operator=<>               [c:\boost_1_57_0\boost\spirit\home\support\detail\hold_any.hpp:305]
# 3 main                                                       [c:\users\tim.junge\documents\visual studio 2010\projects\c++\backup\boosttest\boosttest\main.cpp:212]

Reached maximum leak report limit (-report_leak_max). No further leaks will be reported.

===========================================================================
FINAL SUMMARY:

DUPLICATE ERROR COUNTS:
    Error #   2:   9992

SUPPRESSIONS USED:

ERRORS FOUND:
      0 unique,     0 total unaddressable access(es)
      0 unique,     0 total uninitialized access(es)
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total GDI usage error(s)
      0 unique,     0 total handle leak(s)
      0 unique,     0 total warning(s)
      2 unique,  9993 total,  79944 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
      1 potential leak(s) (suspected false positives)
         (details: C:\Users\tim.junge\Documents\Visual Studio 2010\Projects\C++\DrMemory-Windows-1.8.1-RC1\drmemory\logs\DrMemory-boostTest.exe.6208.000\potential_errors.txt)
      0 unique,     0 total,      0 byte(s) of still-reachable allocation(s)
         (re-run with "-show_reachable" for details)
  50204 leak(s) beyond -report_leak_max
Details: C:\Users\tim.junge\Documents\Visual Studio 2010\Projects\C++\DrMemory-Windows-1.8.1-RC1\drmemory\logs\DrMemory-boostTest.exe.6208.000\results.txt
Mango
  • 362
  • 1
  • 14

1 Answers1

0

I just learned about hold_any about 2 hours ago, but I will try my best :)

Short answer:

I ran through a few tests to learn, that when you are setting:

    a = 5;

You are actually setting the POINTER("object") value to 0x000005. Causing no cleanup and a memory leak(?)

LONG ANSWER + Other info: This somehow causes memory leaks

However, when setting a variable type, everything seemed fine:

   boost::spirit::hold_any a;
   for (unsigned int i = 0; 15000000 > i; ++i)
   {
       //a = 5; //no cleanup on scope loss
       a = size_t(5);  //seems fine
       a = float(5.0);  //seems fine
   }

I read somewhere else that an empty hold_any (with no initial datatype) causes a memory leak (the guy submitted a patch boost::spirit::hold_any memory corruption , not sure if it's been implemented). Which appears to be your case.

So, when assigning new data, make sure it has a type, or when initializing it set the type(?) I would recommend BOTH to be safe:

   boost::spirit::hold_any a(size_t(15));
   for (unsigned int i = 0; 15000000 > i; ++i)
   {
       a = 5; //no memory leak, but invalid for classes - like std::string
   }

Also, an alternative to get/modify the data (since I don't see much documentation):

    size_t* c = boost::spirit::any_cast<size_t>(&a);  //pointer will be 0 if its not a size_t
    size_t &c2 = *(boost::spirit::any_cast<size_t>(&a));  //runtime error if not a size_t
    size_t &c3 = boost::spirit::any_cast<size_t&>(a);  //runtime error if not a size_t

And for checking the data type

       if (int * i = boost::spirit::any_cast<int>(&a))        // an int?
       //if (int& i = boost::spirit::any_cast<int&>(a))       // runtime error
            const bool anInt = true;
       if (size_t* i = boost::spirit::any_cast<size_t>(&a))       // a size_t?
            const bool aSizeT = true;
Community
  • 1
  • 1
VeNoM
  • 1
  • 1