Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:31:59+00:00 2026-05-26T22:31:59+00:00

Given a simple class in C++ with a private member variable name and a

  • 0

Given a simple class in C++ with a private member variable name and a basic constructor:

#include <QString>

class Testclass
{
  private:
    QString *name;

  public:
    Testclass(): name(new QString()) {}
};

Why does valgrind’s memcheck complains about 8 bytes in 1 block, which are definitely lost when using this constructor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:32:00+00:00Added an answer on May 26, 2026 at 10:32 pm
    ~Testclass(){delete name;}
    

    Will plug your leak. C++ doesn’t (and shouldn’t) do this for you.

    ETA: ildjarn correctly points out that you should also have a copy constructor and assignment operator.

    TestClass(const TestClass &cp): name(new QString(*(cp.name)) ) {}
    const TestClass& operator=(const Testclass&rhs)
    {
      (*name)=(*hrs.name);
      return *this;
    }
    

    Otherwise, the default copy constructor or assignment operator will cause the same memory to be deleted twice. Most classes that require a destructor should replace or disable the default copy constructor and assignment operator. This is called the “Rule of Three.”

    You might want to consider simply holding the QString by value, because it’s likely itself a lightweight container class, like std::string or std::vector. But if you’re a C++ beginner, doing it this way once is a valuable lesson.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a very simple object: class User { private Integer id; private String name;
Given a simple class: public class Person { public string FirstName; public string LastName;
Given a simple C# class definition like: [System.Windows.Markup.ContentProperty(PropertyOne)] public class SimpleBase { public string
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Given a simple class that overloads the '[ ]' operator: class A { public:
I wrote a simple Stopwatch class as follows. public class StopWatch { private long
Given 2 classes: ... class Grades{ public: Grades(int numExams) : _numExams(numExams){ _grdArr = new
This is a purely theoretical question. Given three simple classes: class Base { }
Given a simple parent/child class structure. I want to use linqkit to apply a
Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.