I decide not to use switch-case in the message loop. instead, I wrote abstract message class(MessageHandler) so that each message inherits the class and implements what to do.
The problem is it's obscure to find where I should make each message objects and delete them. All the messages should maintain until the program ends.
But in my understanding, the only place I can delete this message objects is inside of the close message itself which is cannot be done.
So should I change the whole design of this or is there a way to do this?
here is the message class. (Sorry, I couldn't summarize the code enough to reduce them)
class MessageHandler
{
private:
UINT message;
public:
static vector<MessageHandler*> msgs;
public:
inline MessageHandler(UINT _message){ setMessage(_message); registerMsg(this); }
virtual ~MessageHandler() = default;
virtual void handleMessage() = 0;
inline void registerMsg(MessageHandler *handlerObj) { msgs.push_back(handlerObj); }
inline UINT getMessage(){ return message; }
inline void setMessage(UINT _message){ message = _message; }
};
class WMQUITmsg : public MessageHandler
{
public:
inline WMQUITmsg() : MessageHandler(WM_QUIT) {}
~WMQUITmsg() = default;
void handleMessage();
};
class WMCLOSEmsg : public MessageHandler
{
public:
inline WMCLOSEmsg() : MessageHandler(WM_CLOSE) {}
~WMCLOSEmsg() = default;
void handleMessage();
};
class WMDESTROYmsg : public MessageHandler
{
public:
inline WMDESTROYmsg() : MessageHandler(WM_DESTROY) {}
~WMDESTROYmsg() = default;
void handleMessage();
};
this is how I use them
class EventHandler
{
public:
bool runMessageLoop(UINT _message);
};
bool EventHandler::runMessageLoop(UINT _message)
{
bool foundMsg = false;
for(auto& it : MessageHandler::msgs)
{
if(it->getMessage() == _message)
{
foundMsg = true;
it->handleMessage();
}
}
return foundMsg;
}
WinMain will call this function.
int ApplicationMain (HINSTANCE hInst,
HINSTANCE hPrevInst,
char *cmdParam,
int cmdShow,
const char *className)
{
//싱글톤 객체 생성
ApplicationManager* AppManager = ApplicationManager::getAppManger();
WindowClass winClass(WindowProcedure, className, hInst);
if(winClass.Register())
{
WindowMaker win(className, hInst);
AppManager->setMessages();
AppManager->setRunLoop();
}
return 0;
}
setMessages dose this
WMQUITmsg *a = new WMQUITmsg();
WMCLOSEmsg *b = new WMCLOSEmsg();
WMDESTROYmsg *c = new WMDESTROYmsg();
I believe this will cause memory leak, because the message objects never can be deleted inside of setMessages to maintain until program is terminated.
Aucun commentaire:
Enregistrer un commentaire