C++ VS (2019) std::string inside class created with new and memory Diagnostic Tool question

Code Wanderer 396 Reputation points
2021-02-08T19:32:55.707+00:00

EDIT:
I decide rewrite my question to more simple because nobody was understands what I wanted to "say" in previously updated question and I discovered that it is not problem with my usage of new and delete, but somewhere else.


If I use inside class test_mem code type_string = typeid(*this).name(); and use Diagnostic Tool while debug, and take memory snapshots under breakpoints (Memory Usage is turned on), I got this Allocations (Diff): when objects are created +4 and when destroyed -3.

When I remove inside constructor type_string = typeid(*this).name();, Allocations are same (+x -x).

Then question is, can I ignore this Allocation +4 -3 (Diagnostic Tool)`? And why those Allocations are different, it does mean string doesn't delete its chars?

Here is source code, you can try it.

#include <iostream>
#include <string>
#include <typeinfo>

class test_mem
{
 std::string type_string;
public:
 test_mem()
 {
 type_string = typeid(*this).name();
 }

 std::string get_type() { return type_string; }
};

// -----------------------------------------------

void memory_test()
{
 test_mem tm1; // use breakpoint here and take snapshot
 test_mem tm2;
 test_mem tm3;
 test_mem tm4;
} // use breakpoint here and take snapshot (Allocation is +XX)

// -----------------------------------------------

int main()
{
 memory_test();
 return 0; // use breakpoint here and take snapshot (Allocation is -XX)
}
Developer technologies C++
Developer technologies Visual Studio Other
{count} votes

3 answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 34,436 Reputation points Microsoft External Staff
    2021-02-09T04:11:13.03+00:00

    Hello @Code Wanderer ,

    Welcome to Microsoft Q&A forum.

    If you try to test with this code cout << "" will you see an increase in the Allocations? The output content should occupy the memory.

    Besides, I think you can also refer to this thread: Does a std:string always require heap memory? Also, since the codes you provided are not full code, there may be some missing of delete code.

    Best Regards,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. Guido Franzke 2,191 Reputation points
    2021-02-09T11:42:38.347+00:00

    Hello,
    1. do not define the string in the class declarations, define the values in the (standard) constructor:

    class test_c
    {
    std::string name;
    public:
    test_c() { name = "none"; }
    //...

    class obj
    {
    protected:
    std::string type_string;
    public:
    obj() { type_string = ""; }

    2. the classes "obj_meta" and "ob" look ok: there is one new in the constructor and one corresponding delete in the destructor.

    3. in memory_test, you call new without the corresponding delete at the end of the function:

    void memory_test()
    {
    test_c* Tc1 = new test_c("object 1", 1);
    ob o1(Tc1);
    // ...
    delete Tc1;
    }

    Regards, Guido


  3. Viorel 122.5K Reputation points
    2021-02-10T11:07:07.91+00:00

    It seems that the implementation of typeid allocates certain data and keeps (caches) them for future reusage. See, for example, some details inside the std_type_name.cpp file from a subfolder of "VC\Tools\MSVC", which is usually present in Visual Studio.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.