Memory Allocation question?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Memory Allocation question?

      I have an issue that I am hoping someone can help me with. I want to override the new and delete operators so I can keep track of all memory allocations in a MemoryManager class. The issue I am having is as follows.

      All of my managers inherit from a Singleton class to guarantee that only one instance of each exists.

      Here is my definition of a class ResourceManager
      #pragma once
      #include "../SosaGlobal.h"

      class ResourceManager : public Singleton <ResourceManager>
      {
      public:
      ResourceManager(void);
      ~ResourceManager(void);
      };

      Next in my application class, I am trying to declare an instance of the ResourceManager as follows.

      In my header file app.h
      #include "Resource/ResourceManager.h"

      ResourceManager resourceManager;

      and in my app.cpp file

      resourceManager = new ResourceManager();

      I get the following error:
      error C2679: binary '=' : no operator found which takes a right-hand operand of type 'ResourceManager *' (or there is no acceptable conversion)

      If I remove this line from the header file
      ResourceManager resourceManager;

      and in the app.cpp I call

      ResourceManager resourceManager = new ResourceManager();

      Then I get a different error:
      error C2440: 'initializing' : cannot convert from 'ResourceManager *' to 'ResourceManager'

      FYI, if I do the following I can declare an instance but not using the new operator.
      for example:
      AudioManager audioManager;
      audioManager.Initialize();

      Can someone please let me know why I get use the new operator to declare an instance of my class?
    • RE: Memory Allocation question?

      Originally posted by sosa
      In my header file app.h
      #include "Resource/ResourceManager.h"

      ResourceManager resourceManager;

      and in my app.cpp file

      resourceManager = new ResourceManager();

      I get the following error:
      error C2679: binary '=' : no operator found which takes a right-hand operand of type 'ResourceManager *' (or there is no acceptable conversion)

      Originally posted by sosa
      If I remove this line from the header file
      ResourceManager resourceManager;

      and in the app.cpp I call

      ResourceManager resourceManager = new ResourceManager();

      Then I get a different error:
      error C2440: 'initializing' : cannot convert from 'ResourceManager *' to 'ResourceManager'


      The reason why you get those errors is because resourceManager needs to be declared as a pointer. Declare it as...

      ResourceManager *resourceManager = new ResourceManager();

      ...to start with, that should be sufficient to be able to use your own implementation of the memory allocation/deallocation. I'm not sure how and if you can do what you seem to want to be doing here though, since the "self" pointer in the singleton is static, and is going to point to the same memory in all of the classes that inherit the singleton class.

      Which is probably not what you want(?). I would rather separate the different resource systems, and treat them all as separate black boxed modules(i.e. separate singleton for each sub-system in this case), and keep the memory handling to the actual game data and assets with your own allocator.
      "There are of course many problems connected with life, of which some of the most popular are: Why are people born? Why do they die? Why do they want to spend so much of the intervening time wearing digital watches?"
      -Douglas Adams