I'm trying to build a simple command processor in C++ and I'm stuck on a not so easy issue. I have a class (CommandProcessor) that takes a request of some form. This request is transformed into an object (Command) and put onto a queue or started immediately if there is no running command. The Command object is responsible for fullfilling the request. When it's completed, the CommandProcessor is notified so it can starts processing another command. This callback is where my problem lies : the CommandProcessor should delete the Command at this point, however, since the CommandProcessor is notified during the execution of a method in the Command object, the Command object ends up indirectly deleting itself.
I could possibly delay the callback at the end of the method to ensure that nothing gets executed after it, but I feel that this is a bit of a brittle design.
My other solution was to keep a reference to the completed command and dlete it when a new request enters the CommandProcessor, but there's 2 problems with that, the first being that I'm basically using memory that may never be deleted and the second is that the Command object contains some resources that needs to be released as soon as possible (e.g.file handler).
I'm probably not the first guy to stumble on this problem so I was wondering if anybody had a better idea.
Thanks!
Aucun commentaire:
Enregistrer un commentaire