static std::string initialization

Posted on 2008-11-11
Last Modified: 2013-12-14
In C++ how do you do static std::string initialization in a class.
When I do it it leaks memory?
// test_static.cpp : Defines the entry point for the console application.



#include <stdlib.h>

#include <crtdbg.h>

#include "stdafx.h"

#include ".\test.h"

using namespace static_test;

int _tmain(int argc, _TCHAR* argv[])


  test my_test_class;

  _CrtMemState s1;

  _CrtMemCheckpoint( &s1 );


  _CrtMemDumpStatistics( &s1 );


	return 0;


#pragma once

#include <string>

namespace static_test


  class test



    static std::string string_20;

    // ctors and dtor

    test() {};

    virtual ~test() {};

    void doit();


} // end namespace

#include "StdAfx.h"

#include ".\test.h"

namespace static_test


  std::string test::string_20 = "12345678901234567890";

  void test::doit()


    std::string new_string = test::string_20;


} // end namespace

Open in new window

Question by:haggersr
    LVL 39

    Expert Comment

    >>>> When I do it it leaks memory?
    Not really. The leak detector is only running before the static objects were deleted. If you will avoid that you need to change to a static pointer which was initialized with new operator. Then you could provide a static class member function which deletes the pointer and sets it to NULL. You would call that function before exiting your program.

    Accepted Solution

    Please provide an example. I am confused because a std::string's memory is self-destructed, correct?
    So do the strings destruct after the leak detector and before main exits? Please clue me in about std::string initialization and destruction.

    Author Comment

    The real answer is that std::strings are iniyialized befoe main is called and destroyed after main exits. Therefore, the crtdbg leak check function will report the std::string as a leak. To correct for thid, instead of calling the leak check function before main exits, you can set an option too do leak checking when main is eneterd and it will only report true leaks from the applications normal block leakage.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now