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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:20:43+00:00 2026-06-13T11:20:43+00:00

I am not sure if the following code is safe from memory leak. #ifndef

  • 0

I am not sure if the following code is safe from memory leak.

#ifndef RENDERABLE_H
#define RENDERABLE_H

class QGLShaderProgram;
class GLWidget;
class BoundingBox;

class Renderable{

public:
    virtual void update(float duration) = 0;
    virtual void render(QGLShaderProgram& shader, float duration) = 0;
    virtual BoundingBox* getBBox() const = 0;
    virtual void translate(float xx, float yy, float zz) = 0;
    virtual void rotate(float degrees_x, float degrees_y, float degrees_z) = 0;
    virtual void scale(float xx, float yy, float zz) = 0;
};

#endif // RENDERABLE_H

The above “interface” is been implemented by object3d.cpp. We may then add many Object3D objects into a Scene object if they belong to the same scene. However, upon the end of the scene, I want to ensure there is no memory leak and so I would call delete on everything. However, in the scene object, I have the following variable:

QVector<Renderable*>* sceneObjects;
QVector<GLTexture2D*>* sceneTextures;
QMap<QString, Material*>* sceneMaterials;

As you can see,

delete sceneObjects;
delete sceneTextures;
delete sceneMaterials;

should delete the QVector and according to Qt, it should call the destructor of those objects in it. However, Qt documentation were not clear about object POINTERS. Would Qt delete object pointers with their proper destructor? Additionally, what happens to Renderable pointers? As you can see from the “interface” that it has no destructor.

Thanks for any input.
ChaoSXDemon

  • 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-13T11:20:45+00:00Added an answer on June 13, 2026 at 11:20 am

    First of all, your Renderable class must have a virtual destructor, because calling delete on a base pointer to a derived object is undefined behaviour without one.

    Second, no, you will need to loop through and call delete on each pointer (or, as Nikos noted, use qDeleteAll as long as every object in the container is allocated with plain new (and not new[] or malloc or anything else)) in each container to make sure their memory is reclaimed (how could Qt know if the pointers point to things allocated by new? They could point to things allocated by new[], things on the heap, the stack, or somewhere else).

    If you don’t want to do that, you can store unique_ptrs in the containers and then the destructors of the unique_ptrs will be called when you delete the containers, and those destructors will deallocate the memory they own. No need to manually deallocate them or use qDeleteAll.

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

Sidebar

Related Questions

The following javascript code seems to leak memory but I’m not sure why. I’ve
I'm not sure if the following code can cause redundant calculations, or is it
I have the following code. I am not sure why the myData array is
I have the following code which is in a transaction. I'm not sure where/when
I am not sure about the exact purpose of the following Rampion's code .
I have the following code; I want to make sure that submit does not
I have the following two chunks of code, I am not so sure what
I was wondering, whether the following code are safe. public class GUIBundle { //
Im not really sure why the following code gives me the following error in
I am not sure why Instruments is showing the following code as leaking 128

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.