I'm currently working on creating a resource management system for use in my own projects.
My original idea was to prevent loading a resource multiple times into memory. For Example: Two objects both need to draw the same image;
I tried to set this functionality up by overriding new and delete.
When a resource is dynamically allocated, it checks to make sure that resource doesn't already exist. If it does, it returns the address of the already existing resource. Otherwise it returns allocated memory.
Obviously I have run into a lot of little tricks with Constructors and initializer lists overwriting already loaded resources, but it works mostly to my liking.
I overwrite the delete to only free the memory if the resource has no more objects referring to it. My current and only issue is that after calling delete and the 'images' destructor from an instance. Any other references to deleting that 'image' do not call the 'images' destructor.
I know there are a lot of wrong practices going on here, but I'm ok with that. Can anyone tell me if a destructor physically marks an object as not being 'that type of object' or something similar?
Image* x = new Image("File1");
Image* x2 = new Image("File1");
// At this Point, x == x2 due to overridden new operator; they reference same file.
delete x; // Calls ~Image() , doesn't free memory for the image
delete x2; // Does not Call ~Image(), does free memory for the image
// Reversing the order, yields the same result. x2 will call the destructor, x will not.