Because like I said commenting the call to getTypeId out fixes memory from leaking.
I used the windows task manager to see how the memory allocation rises too fast. when commented out the memory allocation doesnt increase much.
Compiler/version?
Everything is okay for me.
Also note that type_info object created by typeid is not deleted to allow different typeid expressions to refer to the same object (caching)
how the memory allocation rises too fast
Did you consider that std::string uses dynamic memory and Windows do not reclaim freed memory instantly unless most of physical memory is allocated.
Microsoft Visual Studio 2010
Version 10.0.40219.1 SP1Rel
Microsoft .NET Framework
Version 4.5.50938 SP1Rel
I guess the memory leak is connected to the string more than typeid.
I tested like this:
1 2 3 4 5 6 7
template <class T> void checkPointer(T * ptr)
{
std::string type = "test test test test test test test test test test test test test ";//getTypeId(ptr);
throwOnInvalidPtr(ptr,type);
}
and that causes big memory allocation. (memory allocation rises to hundreds of mb in seconds)
std::typeinfo::name() returns an implementation-defined NTBS.
The standard does not specify what the storage duration (life-time) of this NTBS ought to be. Since programmers may store the value of the returned pointer, and then try to access the NTBS later, every implementation plays safe by never releasing the memory for the NTBS. Current implementations return the same pointer for multiple calls to std::typeinfo::name() for the same type. So, if your program calls template<typename T> const char* getTypeId(T &cls) for a very large number of different types, the memory required for the MTBS may be high.
Old Microsoft implementations - VS 2010 or earlier - were notorious for creating a new NTBS, each time std::typeinfo::name() was called for the same type. In this case, you should update to a more current version.
Update VS itself. Service pack does not fix problem for some people:
It turned out that this bug was fixed in VS2010SP1. Though on my machine I had the old version of files -- despite the SP being installed, and even using 'reapply' did not cure the situation. It got fixed after I asked to remove the SP1 entirely, then applied it again from scratch.
Are you sure that it is a leak and not "memory pages which system just does not want to reclaim now because there is no need to do so?" or memory fragmentation issue. What happens if you leave program for several hours? Does it stabilizes or eats all memory and starts swapping?
I dont know but I noticed release build memory allocation stays still at 50K. I think the problem is somehow connected to debugging. when debugging the app the memory allocation goes to 2GB in minute and app crashes. release seems stable.