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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:29:20+00:00 2026-05-27T21:29:20+00:00

Based on the book , pp338 #include <iostream> #include <vector> #include <string> #include <ostream>

  • 0

Based on the book, pp338

#include <iostream>
#include <vector>
#include <string>
#include <ostream>
#include <algorithm>

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/cast.hpp>

using namespace std;

template <typename R, typename Arg> class invoker_base {
public:
  virtual R operator()(Arg arg)=0;
};

template <typename R, typename Arg> class function_ptr_invoker 
  : public invoker_base<R,Arg> {
  R (*func_)(Arg);
public:
  function_ptr_invoker(R (*func)(Arg)):func_(func) {}

  R operator()(Arg arg) {
    return (func_)(arg);
  }
};

template <typename R, typename Arg> class function1 {
  invoker_base<R,Arg>* invoker_;
public:
  function1(R (*func)(Arg)) : 
    invoker_(new function_ptr_invoker<R,Arg>(func)) {}

  R operator()(Arg arg) {
    return (*invoker_)(arg);
  }

  ~function1() {
    delete invoker_;
  }
};

bool some_function(const std::string& s) {
  std::cout << s << " This is really neat\n";
  return true;
}

int main() {
  function1<bool,const std::string&> f1(&some_function);
  f1(std::string("Hello"));
}

Question> the default destructor of invoker_base is NOT virtual. Does there exist memory leak in the implementation of function1? As the code indicates, the function1::~function1 deletes the allocated resource through a non-virtual base class pointer.

  • 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-27T21:29:20+00:00Added an answer on May 27, 2026 at 9:29 pm

    There is no memory-leak here (the object doesn’t own any resource that needs delete-ing).

    However, invoking delete on a non-base object through a pointer-to-base without a virtual destructor causes undefined behaviour.

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

Sidebar

Related Questions

How good is this book for learning algorithm creation, based on experiences ?
This is based on the algorithm given in the Cormen's book. What am I
Basically I want the books to be text book based, ie, author discusses the
I'm currently working through some exercises in a c++ book, which uses text based
Based on chapter 5 (Generalized Functors) from the book Modern C++ Design, I'm trying
I would like to use form-based authentication as outlined in the book JavaServer Faces
I am implementing role based access control using yii framework for the application mentioned
I am learning CoreData from a book based in iOS4, so, implementing the example
This is based on the example from the book: Pro WPF and Silverlight MVVM
Based on the Pro Git book by Scott Chacon on page 9, to install

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.