I'm currently working on a game in c++. since there's no garbage collector one has always to carefully delete the objects and also make sure that such objects are not accessed anymore once they got deleted.
Now as a project grows some objects may get referenced from more and more places. For example my units in the game may get referenced from the renderer, from the scene hierarchy, from the selection mechanism, from the HUD and so on. now - if a object gets deleted one has to make sure that all other classes that reference this object will be notified about it.
Or let's say it the other way arround - if i create a new class that may reference one of my units, i'll also have to change the code of the unit (or of the unit manager or whatever module delets the unit if it gets destroyed) to make sure this new module knows when the particular unit it currently references gets deleted.
Now I thoght there could be a simple event driven general purpose aproach to solve this problem by creating a baseclass to which one another object can subscribe. Something like this:
class DeletableBase;//forward declaration
class ISubscriber{
public:
virtual someObjectGotDeleted(DeletableBase* deletedObject)=0;
};
class DeletableBase{
private:
vector<ISubscriber*> subscribers;
public:
virtual ~DeletableBase(){
for(int i=0; i<subscribers.size(); i++)
subscribers[i]->someObjectGotDeleted(this);
}
subscribeForDeleteEvent(ISubscriber* subscriber){
subscribers.push_back(subscriber);
}
};
with that - if i reference any object that inherits from this class from a new class i can simply add myself as a subscriber and if the object will be deleted from any other place I will get notifed about it.
is this a "clean" way of coding?
Good design relies on a combination of high-level systems thinking and low-level component knowledge. In modern software design, best practice revolves around creating modular components that you can call and deploy as needed. In doing this, you build software that is reusable, extensible, and easy to test.
Software design is the process of envisioning and defining software solutions to one or more sets of problems. One of the main components of software design is the software requirements analysis (SRA). SRA is a part of the software development process that lists specifications used in software engineering.
As software design is performed by creating modules, it makes the task easier to maintain. Tasks like finding bugs, debugging, restructuring, and changing the functionality of specific elements in the software application become quite easy due to software design.
The software design process can be divided into the following three levels of phases of design: Interface Design. Architectural Design. Detailed Design.
If this is purely about memory management (rather than state change), use a smart pointer instead. Start with shared_ptr
, then optimize using make_shared
/allocate_shared
or boost::intrusive_ptr
if it's too slow.
One thing you'll have to consider (especially if you're writing a game) is that when one of your subscribed objects gets deleted on the main thread, your game will most likely block until each of its subscribers is done doing whatever it's going to do upon deletion of the object. That may affect game performance if you're not careful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With