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 8497723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:12:27+00:00 2026-06-11T00:12:27+00:00

I ran across this issue in my application after checking it for memory leaks,

  • 0

I ran across this issue in my application after checking it for memory leaks, and discovered that some of my classes are not being destroyed at all.

The code below is split into 3 files, it is supposed to implement a pattern called pimpl. The expected scenario is to have both Cimpl constructor and destructor print their messages. However, that’s not what I get with g++. In my application, only constructor got called.

classes.h:

#include <memory>

class Cimpl;

class Cpimpl {
    std::auto_ptr<Cimpl> impl;
public:
    Cpimpl();
};

classes.cpp:

#include "classes.h"
#include <stdio.h>

class Cimpl {
public:
    Cimpl() {
        printf("Cimpl::Cimpl()\n");
    }
    ~Cimpl() {
        printf("Cimpl::~Cimpl()\n");
    }
};    

Cpimpl::Cpimpl() {
    this->impl.reset(new Cimpl);
}

main.cpp:

#include "classes.h"

int main() {
    Cpimpl c;
    return 0;
}

Here is what I was able to discover further:

g++ -Wall -c main.cpp
g++ -Wall -c classes.cpp
g++ -Wall main.o classes.o -o app_bug
g++ -Wall classes.o main.o -o app_ok

It looks like the destructor is being called in one of two possible cases, and it depends on the linking order. With app_ok I was able to get the correct scenario, while app_bug behaved exactly like my application.

Is there any bit of wisdom I am missing in this situation?
Thanks for any suggestion in advance!

  • 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-11T00:12:28+00:00Added an answer on June 11, 2026 at 12:12 am

    The goal of the pimpl idiom is to not have to expose a definition of the implementation class in the header file. But all the standard smart pointers require a definition of their template parameter to be visible at the point of declaration in order to work correctly.

    That means this is one of the rare occasions where you actually want to use new, delete, and a bare pointer. (If I’m wrong about this and there’s a standard smart pointer that can be used for pimpl, someone please let me know.)

    classes.h

    struct Cimpl;
    
    struct Cpimpl
    {
        Cpimpl();
        ~Cpimpl();
    
        // other public methods here
    
    private:
        Cimpl *ptr;
    
        // Cpimpl must be uncopyable or else make these copy the Cimpl
        Cpimpl(const Cpimpl&);
        Cpimpl& operator=(const Cpimpl&);
    };
    

    classes.cpp

    #include <stdio.h>
    
    struct Cimpl
    {
        Cimpl()
        {
            puts("Cimpl::Cimpl()");
        }
        ~Cimpl()
        {
            puts("Cimpl::~Cimpl()");
        }
    
        // etc
    };
    
    Cpimpl::Cpimpl() : ptr(new Cimpl) {}
    Cpimpl::~Cpimpl() { delete ptr; }
    
    // etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran across some routes that followed this format: resources :foobar, except: create I
I ran across this issue today and was able to determine that, when doing
I ran across an interesting issue in some of my humanize_bytes() code. This loop
I ran across this chunk of code (modified) in our application, and am confused
A situation I ran across this week: we have a jQuery Ajax call that
I recently ran across a routine that looks something like this: procedure TMyForm.DoSomething(list: TList<TMyObject>;
I recently ran across this problem while trying to implement a service that has
I just ran across this an Google App Engine article that uses that funny
I was debugging a co-workers program and ran across this issue in WPF. It
I ran across this problem where the UIAlertView does not trigger any handlers in

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.