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

  • Home
  • SEARCH
  • 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 8263011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:57:09+00:00 2026-06-08T03:57:09+00:00

I have a simple class: class Histogram { int m_width; int m_height; int m_sampleSize;

  • 0

I have a simple class:

class Histogram {
    int m_width;
    int m_height;
    int m_sampleSize;
    int m_bufferWidth;
    int m_bufferHeight;

    uint8* m_buffer;
    int m_size;
public:
    Histogram() : m_buffer(0) { }
    Histogram(int width, int height, int sampleSize) {
        m_buffer = new unsigned char [width*height*sampleSize];
    }
    ~Histogram() {
        my_log("destructor: buffer: %p", m_buffer);
        if ( m_buffer ) { delete [] m_buffer; m_buffer = NULL; }
    }
    unsigned char* buffer() {
        return m_buffer;
    }
};

It is a member in other class:

class Other {
    Histogram m_histogram;

    void reset() {
        my_log("reset() called: buffer: %p", m_histogram.buffer());
        m_histogram = Histogram(512, 512, 2);
    }
}

Now, I first create “uninitialized” object using Histogram() constructor – which sets m_buffer to NULL;

Then, I call the reset method, which does m_histogram = Histogram( 512, 512, 3 ) – the new object has m_buffer initialized via new.

So expected sequence of log messages is:

  • “reset() called: buffer: 0x0”
  • “destructor: buffer: 0x0”

But instead, I get:

  • “reset() called: buffer: 0x0”
  • “destructor: buffer: 0x072a7de”

So some irrational action is being performed. Moreover, the 0x072a7de address is displayied when I also delete the second object (created with “larger” constructor, with three int parameters).

  • 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-06-08T03:57:12+00:00Added an answer on June 8, 2026 at 3:57 am

    First of all, since you are pointing to a dynamically allocated array, you need to use operator delete[]

    delete[] m_buffer;
    

    Second, and more importantly, since you have dynamically allocated memory, you should follow the rule of three and implement a copy constructor, and assignment operator, as well as fixing the destructor.

    What happens now is that your (compiler synthesized) assignment operator is making a “shallow” copy, i.e. it is copying the pointer. Then you will hve multiple destructors trying to delete it. You are invoking undefined behaviour.

    You could really save yourself a lot of trouble by using an std::vector<uint8> as a buffer.

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

Sidebar

Related Questions

I have simple problem: class Weapon{ public: int dmg; float speed; int rate; };
I have a simple class like this: class A { public: void f(const int&
I have this simple class: public class MappingCollection<T> : List<T> { private int _declaredTotal
I have simple class with width and height member fields which define number of
I have a simple class Student under namespace School. namespace XmlTestApp { public class
I have a simple class I'm serializing. [DataContract(Name = Test, Namespace = )] public
I have a simple class inheriting from WebControl as follow: public class Instrument :
I have an simple class that I get from a webservice. public class person
I have a simple class class Foo { public: float m; Foo(); } Foo::Foo(){
I have a simple class like this [ActiveRecord(Subscriptions)] public class Subscription : ActiveRecordBase<Subscription> {

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.