I'm having some difficulty with the memory management with Cocoa. The best way I can explain the situation is by giving an analogy, because my real example would be harder to explain.
I have a Car Class. Inside this Car Class, there is an instance of an engine object from an Engine Class. The Engine Class is composed of objects, which make up the engine. From time to time, the car object is taken into the garage and a few parts for the engine need to be replaced, but not the whole engine. The way these parts are maintained in Objective-C lingo is that old parts are deallocated and replacement parts are allocated.
I know during the program execution that maintenance will occur several times. I have no idea which parts will be replaced each time maintenance is needed, so memory allocation and deallocation has to be flexible. I can't just remove a certain list of parts. And it makes no sense to replace the engine every time. I only want to replace the parts that need to be replaced, which means I need to deallocate them and create new object versions of them for my Engine instance.
The engine is in an NSAutoreleasePool instance. The only way I can replace bad parts is to release the pool to which all engine parts belong to. If I retain the parts that don't need to be replaced and then release the pool, it would work. But I would then have a new problem. Now the other parts are just floating around and I can't replace them by deallocating them, unless I create a new pool, then release that pool. But I'm not sure if this is the best way to handle it.
I would just send a free message to replacement parts and simply allocated new ones, but Cocoa forces some of my objects into the pool that is currently on the top of the stack, making a situation more complex than just simple freeing them with a message.
I need some input on the best way to deal with this.
I have a Car Class. Inside this Car Class, there is an instance of an engine object from an Engine Class. The Engine Class is composed of objects, which make up the engine. From time to time, the car object is taken into the garage and a few parts for the engine need to be replaced, but not the whole engine. The way these parts are maintained in Objective-C lingo is that old parts are deallocated and replacement parts are allocated.
I know during the program execution that maintenance will occur several times. I have no idea which parts will be replaced each time maintenance is needed, so memory allocation and deallocation has to be flexible. I can't just remove a certain list of parts. And it makes no sense to replace the engine every time. I only want to replace the parts that need to be replaced, which means I need to deallocate them and create new object versions of them for my Engine instance.
The engine is in an NSAutoreleasePool instance. The only way I can replace bad parts is to release the pool to which all engine parts belong to. If I retain the parts that don't need to be replaced and then release the pool, it would work. But I would then have a new problem. Now the other parts are just floating around and I can't replace them by deallocating them, unless I create a new pool, then release that pool. But I'm not sure if this is the best way to handle it.
I would just send a free message to replacement parts and simply allocated new ones, but Cocoa forces some of my objects into the pool that is currently on the top of the stack, making a situation more complex than just simple freeing them with a message.
I need some input on the best way to deal with this.