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

  • Home
  • SEARCH
  • 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 4040228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:43:33+00:00 2026-05-20T12:43:33+00:00

Dear all, this is going to be tough: I have created a game object

  • 0

Dear all, this is going to be tough: I have created a game object factory that generates objects of my wish. However, I get memory leaks which I can not fix.

Memory leaks are generated by return new Object(); in the bottom part of the code sample.

static BaseObject * CreateObjectFunc()
{
    return new Object();
}

How and where to delete the pointers? I wrote bool ReleaseClassType(). Despite the factory works well, ReleaseClassType() does not fix memory leaks.

bool ReleaseClassTypes()
{
    unsigned int nRecordCount = vFactories.size();
    for (unsigned int nLoop = 0; nLoop < nRecordCount; nLoop++ )
    {
        // if the object exists in the container and is valid, then render it
        if( vFactories[nLoop] != NULL) 
            delete vFactories[nLoop]();
    }
    return true;
}

Before taking a look at the code below, let me help you in that my CGameObjectFactory creates pointers to functions creating particular object type. The pointers are stored within vFactories vector container.

I have chosen this way because I parse an object map file. I have object type IDs (integer values) which I need to translate them into real objects. Because I have over 100 different object data types, I wished to avoid continuously traversing very long Switch() statement.

Therefore, to create an object, I call vFactories'[‘nEnumObjectTypeID’]'() via CGameObjectFactory::create() to call stored function that generates desired object.

The position of the appropriate function in the vFactories is identical to the nObjectTypeID, so I can use indexing to access the function.

So the question remains, how to proceed with garbage collection and avoid reported memory leaks?

#ifndef GAMEOBJECTFACTORY_H_UNIPIXELS
#define GAMEOBJECTFACTORY_H_UNIPIXELS

//#include "MemoryManager.h"
#include <vector>


template <typename BaseObject>
class CGameObjectFactory
{
public:
    // cleanup and release registered object data types
    bool ReleaseClassTypes()
    {
        unsigned int nRecordCount = vFactories.size();
        for (unsigned int nLoop = 0; nLoop < nRecordCount; nLoop++ )
        {
            // if the object exists in the container and is valid, then render it
            if( vFactories[nLoop] != NULL) 
                delete vFactories[nLoop]();
        }
        return true;
    }

    // register new object data type
    template <typename Object>
    bool RegisterClassType(unsigned int nObjectIDParam )
    {
        if(vFactories.size() < nObjectIDParam) vFactories.resize(nObjectIDParam);

        vFactories[nObjectIDParam] = &CreateObjectFunc<Object>;
        return true;
    }


    // create new object by calling the pointer to the appropriate type function
    BaseObject* create(unsigned int nObjectIDParam) const
    {
        return vFactories[nObjectIDParam]();
    }


    // resize the vector array containing pointers to function calls
    bool resize(unsigned int nSizeParam)
    {
        vFactories.resize(nSizeParam);
        return true;
    }

private:
    //DECLARE_HEAP;

    template <typename Object>
    static BaseObject * CreateObjectFunc()
    {
        return new Object();
    }


    typedef BaseObject*(*factory)();
    std::vector<factory> vFactories;
};


//DEFINE_HEAP_T(CGameObjectFactory, "Game Object Factory");

#endif // GAMEOBJECTFACTORY_H_UNIPIXELS
  • 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-20T12:43:33+00:00Added an answer on May 20, 2026 at 12:43 pm

    You can start by using std::shared_ptr or std::tr1::shared_ptr or boost::shared_ptr depending on your compiler.

    You would use it like this:

    typedef std::shared_ptr<BaseObject> BaseObjectPtr;
    static BaseObjectPtr CreateObjectFunc()
    {
        return BaseObjectPtr(new Object());
    }
    

    You won’t need to release the created resources. They will do automatic reference counting and deallocate themselves when there are no strong references pointing to it.

    So in your second code example:

    #ifndef GAMEOBJECTFACTORY_H_UNIPIXELS
    #define GAMEOBJECTFACTORY_H_UNIPIXELS
    
    //#include "MemoryManager.h"
    #include <vector>
    #include <memory>
    
    template <typename BaseObject>
    class CGameObjectFactory
    {
    public:
        typedef std::shared_ptr<BaseObject> BaseObjectPtr;
    
        // cleanup and release registered object data types
        bool ReleaseClassTypes()
        {
            unsigned int nRecordCount = vFactories.size();
            for (unsigned int nLoop = 0; nLoop < nRecordCount; nLoop++ )
            {
                // if the object exists in the container and is valid, then render it
                //if( vFactories[nLoop] != NULL) 
                //    delete vFactories[nLoop]();
                // The above code would create something then immediately delete it.
                // You could keep a container of pointers to the objects you created
                // and loop through that instead, or use shared_ptr.
                // If you want to unregister the creator functions just NULL the pointer.
                vFactories[nLoop] = NULL;
            }
            return true;
        }
    
        // register new object data type
        template <typename Object>
        bool RegisterClassType(unsigned int nObjectIDParam )
        {
            if(vFactories.size() < nObjectIDParam) vFactories.resize(nObjectIDParam);
    
            // Store a pointer to the creation function
            vFactories[nObjectIDParam] = &CreateObjectFunc<Object>;
            return true;
        }
    
    
        // create new object by calling the pointer to the appropriate type function
        BaseObjectPtr create(unsigned int nObjectIDParam) const
        {
            return vFactories[nObjectIDParam]();
        }
    
    
        // resize the vector array containing pointers to function calls
        bool resize(unsigned int nSizeParam)
        {
            vFactories.resize(nSizeParam);
            return true;
        }
    
    private:
        //DECLARE_HEAP;
    
        template <typename Object>
        static BaseObjectPtr CreateObjectFunc()
        {
            return BaseObjectPtr(new Object());
        }
    
    
        typedef BaseObjectPtr(*factory)();
        std::vector<factory> vFactories;
    };
    
    
    //DEFINE_HEAP_T(CGameObjectFactory, "Game Object Factory");
    
    #endif // GAMEOBJECTFACTORY_H_UNIPIXELS
    

    See if that helps.

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

Sidebar

Related Questions

Dear all,Now i have this question in my java program,I think it should be
dear all..i have this code: <script> var str=KD-R435MUN2D; var matches=str.match(/(EE|[EJU]).*(D)/i); if (matches) { var
dear all..i have this data inside DB: line model serial fa01 kd-g335ud 105x0001 fa01
Dear g++ hackers, I have the following question. When some data of an object
Dear all, I've been stuck with this problem now for a few days and
Possible Duplicate: LINQ Between Operator Dear All, Hi, I need to write this query
dear all..i have a problem. at firebug show: $(#menu ul.menu).lavaLamp is not a function
I'd like to have your opinion on a specific case, please. This is about
Dear All, I am trying to parse the following HTML fragment, and I would
Dear Stacktoverflow, can you show me an example of how to use a QScrollBar?

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.