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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:00:45+00:00 2026-06-07T21:00:45+00:00

A question: Should I delete pointers which are fetched in functions (not created, just

  • 0

A question:

Should I delete pointers which are fetched in functions (not created, just fetched)? Example:

#include <SomeObject>

#define SAFE_DELETE(p) { if (p) { delete (p); (p) = NULL; } }

class DraftObject
{
public:

    DraftObject() : _x(0) {}
    ~DraftObject(){}

    int CalculateSomething()
    {
        AnotherObject* aObj = SomeObject::getInstance()->getAObjPointer();

        /* Do some calculations and etc... */
        _x += aObj->GetSomeIntValue();

        SAFE_DELETE(aObj) // <-- Would you recomend this here?

        return _x;
    }

protected:
    int _x;
};

The aObj would be re-used in other cases as well in the SomeObject instance. I could go on and always call SomeObject::getInstance()->getAObjPointer() for everything I need it for, but SomeObject::getInstance()->getAObjPointer()->GetSomeIntValue() is not as readable as aObj->GetSomeIntValue() in my personal opinion.
I know that I would not need to worry if I used something from boost (shared_ptr, weak_ptr or even auto_ptr), but I am more curious about the way this works. Will not deleting the pointer create a memory leak situation or will the deletion of the pointer delete it from the memory so that it will be gone in other scopes (the instance object as well as anywhere else it might be used) ?

Any thoughts?

Cheers.

  • 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-07T21:00:46+00:00Added an answer on June 7, 2026 at 9:00 pm

    It depends.

    If SomeObject::getInstance()->getAObjPointer(); returns a different object each call, probably yes. Otherwise, no. This should be documented.

    Also, your “safe delete”:

    #define SAFE_DELETE(p) { if (p) { delete (p); (p) = NULL; } }
    

    is utterly useless. And ugly. If I saw this in code, I’d go and make fun of the programmer who wrote it. If p is NULL, the delete is safe.

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

Sidebar

Related Questions

This may not be the kind of question one should ask on StackOverflow, but
This should be a pretty straightforward question. I have the following code, which forms
this is a question about which class does a free on shared pointers. So
Ignore this question, just found a mistake in my code, flag to delete it
I don't know if my question should be asked here or not. Please let
Question should say it all. Let's say there's a local file mydefaultvalues.txt, separated from
Question Should one discard a packet with an unknown IPv6 extension header? Details I
I have a very simple yes no question: should static methods have same result
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
I have a question that should be quick and easy for you guys to

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.