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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:30:33+00:00 2026-06-17T18:30:33+00:00

I want to make sure that I’m creating/destroying this object properly… This is the

  • 0

I want to make sure that I’m creating/destroying this object properly…

This is the definition of my Camera object which contains references to Vector objects:

#ifndef CAMERA_H
#define CAMERA_H

#include "vector.h"

class Camera {
 private:
  Vector* location;
  Vector* direction;
  float velocity;
 public:
  Camera();
  ~Camera();
};

#endif

which are created in the constructor:

#include "camera.h"

Camera::Camera() {
  location = new Vector(1.0, 1.0, 1.0);
  direction = new Vector(1.0, 1.0, 1.0);
  velocity = 0.0;
}

Camera::~Camera() {
  delete location;
  delete direction;
}

then, whenever I want a camera object I simply call Camera camera.

  • Am I correct in assuming that when the variable camera goes out of
    scope, the destructor will be called and I won’t suffer any memory
    leak?

  • If I want to remove the variable camera before the scope closes, is
    it correct to perform delete camera?

  • 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-17T18:30:35+00:00Added an answer on June 17, 2026 at 6:30 pm

    Am I correct in assuming that when the variable camera goes out of
    scope, the destructor will be called and I won’t suffer any memory
    leak?

    yes

    If I want to remove the variable camera before the scope closes, is it
    correct to perform delete camera?

    No, camera is not allocated by new operator, you can not delete it, just leave it until it goes out of scope. Unless call new/delete to force object duration.

    Potential memory leak:

    In below code, there is a chance to leak memory. if constructs location finishes but direction = new Vector(1.0, 1.0, 1.0); fails and exception is thrown, Camera destructor won’t be called thus location memory is leaked.

    Camera::Camera() {
      location = new Vector(1.0, 1.0, 1.0);
      direction = new Vector(1.0, 1.0, 1.0);
      velocity = 0.0;
    }
    

    A better solution:
    There is no need to introduce pointer for Vector members. Use automatic storage should be preferred.

    class Camera {
     private:
      Vector location;
      Vector direction;
      float velocity;
    
     public:
      Camera() 
      : location(1.0, 1.0, 1.0), 
        direction(1.0, 1.0, 1.0),
        velocity(0.0f)
      {
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to make sure that I'm properly deallocating memory in my program...
I am creating a quiz... and i want to make sure that people can
This is my ajax pager code below: Now I want to make sure that
I am making an application in which i want to make sure that only
I have an object named Charity and I want to make sure that each
I have a string, in which I want to make sure that every '_'
I am having trouble getting this to work I want to make sure that
I want to make sure that *this != &rhs in the assignment operator. But
This seems obvious but I want to make sure that I'm doing this right.
I'm troubleshooting a problem with creating Vista shortcuts. I want to make sure that

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.