Jump to content
  • Advertisement
Sign in to follow this  
SteveDeFacto

How to get all instances of a class that was created with the new operator?

This topic is 2755 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My library has a main instance class and a developer can create more than one copy of the instance class. I have a pointer to a third party class which one of the library's instances is using. I need to search all copies of the instance class in order to find which copy is referencing the third party class. To do this I need to get a list of all copies of the instance class. Something like this:

std::vector<myClass*> list = GetInstancesOfClass(myClass);

I'm not sure if this is possible but if it's not then there is no way to do what I need...

Share this post


Link to post
Share on other sites
Advertisement
The first thing that comes to my mind is to create a list that stores you current instances of your class (this could be a static member). Now in your class when the constructor is called it can add a reference/pointer to this list. Now all you need to do is iterate over that list. Im not sure how your program is structured, but if you delete instances of your class then the destructor will have to be responsible for removing the relevant entry in the list.

Uni

Share this post


Link to post
Share on other sites
something like this perhaps:


class MyClass {
private:
static std::list<MyClass*> instances;
public:
MyClass() {
......
classList.push_back(this);
}
~MyClass() {
......
instances.remove(this);
}
}

Share this post


Link to post
Share on other sites
OK, so creating a global list of instances is my only option but I don't know how to do this since there is no main source file in my project and placing the code in one of the header files results in already defined errors. For example:



1>OvglGraphics.obj : error LNK2005: "class std::vector<void *,class std::allocator<void *> > gInstanceList" (?gInstanceList@@3V?$vector@PAXV?$allocator@PAX@std@@@std@@A) already defined in OvglAudio.obj
1>OvglInstance.obj : error LNK2005: "class std::vector<void *,class std::allocator<void *> > gInstanceList" (?gInstanceList@@3V?$vector@PAXV?$allocator@PAX@std@@@std@@A) already defined in OvglAudio.obj
1>OvglMath.obj : error LNK2005: "class std::vector<void *,class std::allocator<void *> > gInstanceList" (?gInstanceList@@3V?$vector@PAXV?$allocator@PAX@std@@@std@@A) already defined in OvglAudio.obj
1>OvglMesh.obj : error LNK2005: "class std::vector<void *,class std::allocator<void *> > gInstanceList" (?gInstanceList@@3V?$vector@PAXV?$allocator@PAX@std@@@std@@A) already defined in OvglAudio.obj
1>OvglScene.obj : error LNK2005: "class std::vector<void *,class std::allocator<void *> > gInstanceList" (?gInstanceList@@3V?$vector@PAXV?$allocator@PAX@std@@@std@@A) already defined in OvglAudio.obj



Technically I should never need to search instances of my library like this but PhysX uses a callback for detecting contact between objects and I have no way of getting a pointer to my own class which holds the physx actor.

Share this post


Link to post
Share on other sites
The information you want still doesn't exist.

Simple example of typical physics library code:Vector3D * v = (Vector3D *)_aligned_malloc(n * sizeof(Vector3D), 16);

Same for std::allocators, especially for vectors:
- allocate(size_t n) // block allocation, n*sizeof(T), result is void *, it has no type
- construct(pointer) // same as new ((void*)p) T (val), applies constructor on memory chunk, no allocation is performed, may be done in-place


It's possible to write code in such a way to add type information, but existing code simply doesn't have it.

Share this post


Link to post
Share on other sites
Alright this is what I ended up doing:

class GlobalsClass
{
public:
std::vector<Instance*> InstanceList;
};

extern GlobalsClass Globals;



Then in OvglInstance.cpp I did this:

Ovgl::GlobalsClass Ovgl::Globals;

This allows me to have a global list of instances that I can search.

Share this post


Link to post
Share on other sites
You can do the same thing with the static container inside the class itself, rather than creating this "globals class" (which will almost certainly be used for other information - bad!). Don't forget to include instances created with a copy constructor, or disabled copying for that class.

Share this post


Link to post
Share on other sites

You can do the same thing with the static container inside the class itself, rather than creating this "globals class" (which will almost certainly be used for other information - bad!). Don't forget to include instances created with a copy constructor, or disabled copying for that class.



Can you show me what you mean with the static container inside the class?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!