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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:25:12+00:00 2026-05-28T01:25:12+00:00

Is it safe and correct to use an std::tr1::shared_ptr as in the sample code

  • 0

Is it safe and correct to use an std::tr1::shared_ptr as in the sample code below, for the purpose of reference counting? (this is just a particular sample, the class can contain anything else (void*) instead of the FILE*)

class File
{
public:
    File(const char* path, const char* mode) :
        _refcount(new int(0))
    {
        this->_file = fopen(path, mode);
    }

    ~File()
    {
        if (this->_refcount.unique())
        {
            if (this->_file != NULL)
            {
                fclose(this->_file);
            }
        }
    }

    int write(void* buff, size_t size)
    {
        fwrite(buff, size, 1, this->_file);
    }

private:
    FILE* _file;
    std::tr1::shared_ptr<int> _refcount;
};
  • 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-28T01:25:13+00:00Added an answer on May 28, 2026 at 1:25 am

    Consider instead using a shared_ptr<FILE> with a custom deleter:

    struct fclose_deleter
    {
        void operator()(FILE* f)
        {
            if (f)
            {
                std::fclose(f);
            }
        }
    };
    

    Then, your File class is much simpler (and correcter):

    class File
    {
    public:
        File(const char* path, const char* mode)
            : _file(std::fopen(path, mode), fclose_deleter())
        {
        }
    
        int write(void const* buff, size_t size)
        {
            // You'll want to verify that _file.get() is valid, or you'll want to
            // throw in the constructor if the call to 'std::fopen()' fails.
            std::fwrite(buff, size, 1, _file.get());
        }
    
    private:
        std::tr1::shared_ptr<FILE> _file;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this code correct or is there any possibility of some random threading deadlocks
Borrowing Howard Hinnant's example and modifying it to use copy-and-swap, is this op= thread-safe?
Is it safe (and correct) in a C++ header file to use the using
If I code this std::map<int, char> example = { (1, 'a'), (2, 'b'), (3,
How safe is it to use Silverlight in production for a graphic form? Is
Is it safe to use MS SQL's WITH (NOLOCK) option for select statements and
Is it good/safe/possible to use the tiny utfcpp library for converting everything I get
In ASP.NET or WCF is it safe to use ServiceSecurityContext.Current.WindowsIdentity.Name to get the current
We're trying to be type-safe in our views and use the new ExpressionInputExtenssion HtmlHelpers,
I want to use this function from mongoid: person.update_attributes(first_name: Jean, last_name: Zorg) But I

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.