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

The Archive Base Latest Questions

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

A Scene struct has a pointer to (a linked list of) SceneObjects. Each SceneObject

  • 0

A Scene struct has a pointer to (a linked list of) SceneObjects.
Each SceneObject refers to a Mesh.
Some SceneObjects may however refer to the same Mesh (by sharing the same pointer – or handle, see later – to the Mesh). Meshes are pretty big and doing it this way has obvious advantages for rendering speed.

typedef struct {
    Mesh *mesh; 
        ...
    struct SceneObject *next;
} SceneObject;

typedef struct Scene {
    SceneObject *objects;
    ...
} Scene;

My question:
How do I free a Scene, while avoiding to free the same Mesh pointer multiple times?

I thought I could solve this by using handle to Mesh (Mesh** mesh_handle) instead of a pointer so I could set the referenced Mesh pointer to 0, and let successive frees on it just free 0, but I can’t make it work. I just can’t get my head around how to avoid multiple deallocations.

Am I forced to keep references for such a scenario? Or am I forced to put all the Mesh objects into a separate Mesh table and free it separately? Is there a way to tackle this without doing these things? By tagging the objects as instances of each other I can naturally adjust the free algorithm so it deals with the problem, but I was wondering if there is a more ‘pure’ solution for this problem.

  • 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-18T20:15:33+00:00Added an answer on May 18, 2026 at 8:15 pm

    One standard solution is to have reference counters, that is every object that can possibly be referred by many other objects must have a counter that remembers how many of them are pointing it. This is done with something like

    typedef struct T_Object
    {
        int refcount;
        ....
    } Object;
    
    Object *newObject(....)
    {
        Object *obj = my_malloc(sizeof(Object));
        obj->refcount = 1;
        ....
        return obj;
    }
    
    Object *ref(Object *p)
    {
        if (p) p->refcount++;
        return p;
    }
    
    void deref(Object *p)
    {
        if (p && p->refcount-- == 1)
            destroyObject(p);
    }
    

    Who first allocates the object will be the first owner (hence the counter is initialized to 1). When you need to store the pointer in other places every time you should store ref(p) instad, to be sure to increment the counter. When someone is not going to point to it anymore you should call deref(p). Once the last reference to the object is gone the counter will become zero and the deref call will actually destroy the object.

    It takes some discipline to get it working (you should always think when calling ref and deref) but it’s possible to write complex software that has zero leaks using this approach.

    A simpler solution that is sometimes applicable is having all your shared objects also stored in a separate list… you freely assign and change complex data structures pointing to these objects but you never free them during the normal use. Only when you need to throw everything away you deallocate those objects by using that separate list.
    Note that this approach is possible only if you’re not allocating many objects during the “normal use” because in that case delaying the destruction could be not viable.

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

Sidebar

Related Questions

A Scene struct has a pointer to (a linked list of) SceneObjects. Each SceneObject
In my scene I have a tree of meshes. Each mesh contains a transformation
Imagine the scene, you're updating some legacy Sybase code and come across a cursor.
I have an opengl scene rendering on an EAGLView layer and some other elements
I have a scene in andEngine. The scene has basically three animated objects. I
I have const struct aiScene *scene; declared in class. In the function where I
I declared a structure with the new game_struct. It has these contents : struct
My scene has a TextField object. I set up my TextField as DynamicText because
my scene has 2 layers, game,and buttons, with : +(CCScene *) scene { //
The scene : I'm writing an embeddable widget. It takes the form of a

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.