I have functions to load and get Icon from files as follows:
std::map<wstring, HICON> m_map_icons;
void WindowSysTray::_loadIconFromFiles()
{
...
for (int i = 0; i < 3; ++i) {
wstring temp = path + trayList[i];
HICON hIcon = NULL;
Gdiplus::Bitmap bitmap(temp.c_str(), false);
bitmap.GetHICON(&hIcon);
pair<wstring, HICON> _pair = make_pair(trayList[i], hIcon);
m_map_icons.insert(_pair);
}
}
HICON WindowSysTray::getIconFromFile(const wchar_t* iconPath)
{
auto iter = m_map_icons.find(wstring(iconPath));
if (iter == m_map_icons.end()) {
return NULL;
}
else {
return iter->second; // ---Issue here---
}
}
...
hIcon = getIconFromFile(L"tray.png");
if (hIcon)
{
TrayIcon.SetIcon(hIcon);
}
...
When I invoke getIconFromFile
function, it sometimes crashes while returning value at the end of the function:
return iter->second;
I verified and make sure that I loaded Icon before by invoking _loadIconFromFiles
function.
Does anyone know what is the possible root cause of this issue?