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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:15:52+00:00 2026-05-18T10:15:52+00:00

We have code similar to the one below. We are trying to achieve RAII

  • 0

We have code similar to the one below. We are trying to achieve RAII using a class called MemRelease class. Now, FXOMemRelease is being used the way shown in sample.cc.

Is this OK to use the obeject of FXOMemRelase like that ? Most of the time I see that the destructor for MemRelease only gets called after MakeString() is completed. That is fine.
Will it be the case always ? We got a memory issue and the truss output last pointed to the print statement in the MemRelease class.

MemRelease.cpp

template<typename T>
MemRelease<T>::MemRelease(T* store, unsigned char array_yesno, short release_yesno)
        : ptr(store), array(array_yesno), release(release_yesno)
{
}


template<typename T>
T* MemRelease<T>::getStore()
{
        return (ptr);
}

template<typename T>
MemRelease<T>::~MemRelease()
{
        if (!release) return;
         if ( (array == 'Y') || (array == 'y') )
        {
                cout << "deleting array in MemRelease pid = " << getpid() << endl;
                delete [] ptr;
                ptr = NULL;
        }
        else
        {
                cout << "deleting memory in MemRelease pid = " << getpid() <<  endl;
                delete ptr;
                ptr = NULL;
        }
}

Sample.cc:

char* MakeString(char* str)
{
   cout << "Entered MakeString\n";
   char* newstr = new char[strlen(str) + 1];
   strcpy(newstr, str);
   return str;
}
int main()
{
   char *str = new char[10];
   strcpy(str, "jagan");
   char* newstr = MakeString(MemRelease<char>(str).getStore());

   getchar();
   delete newstr;
}
  • 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-18T10:15:53+00:00Added an answer on May 18, 2026 at 10:15 am

    Most of the time I see that the destructor for MemRelease only gets called after MakeString() is completed. That is fine. Will it be the case always ?

    Yes, always. The temporary object stays valid until the assignment to newstr completes.

    As afriza points out, we can’t verify which destruction code is running because you haven’t included the default values for the constructor’s array_yesno and release_yesno. BTW, there’s a type called bool which should be used for yes/no values.

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

Sidebar

Related Questions

We have similar code to the following in one of our projects. Can anyone
I have a piece of code VERY similar to this one http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailFetching I the
In a CUDA kernel, I have code similar to the following. I am trying
I have a backbone.js model similar to the one shown below. Filters = Backbone.Model.extend({
I have a favorite C# program similar to the one below that shows that
I am experiencing a memory leak with a code similar to the one below
I have code similar to this filtering entries in an Array of Objects: var
I have code similar to the following in many places: var dbParams = db.ReadParams(memberID,
I have code similar to the following with a URL like this... If I
I have code similar to: number_to_currency(line_item.price, :unit => £) littering my views in various

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.