ethanmoffat posted: (12th Jan 2017, 04:11 am)
Got some resource leaks in your code.
- In if(!bitmap) you need to call FreeLibrary before returning
- In if(!GetObject) you need to call DeleteObject and FreeLibrary before returning
Also, you should definitely only call LoadLibrary once outside of the loop.
Also, if you declare your for loop like so:
int resid;
for(; resid < maxid; ++resid)
You won't need to have two separate counter variables.
Yeah very slappy code. Thanks for finding those leaks. So back to the problem at hand what I'm thinking about doing is returning the last successfully loaded bitmap id? Here is an edited version.
unsigned int Video::CountResources(std::string resname,unsigned int maxid)
{
HMODULE module = LoadLibrary(resname.c_str());
if(!module)return 0;
BITMAP bm;
HBITMAP bitmap = 0;
unsigned int last_id = 0;
for(unsigned int resid = 0;resid < maxid;++resid)
{
if (bitmap) DeleteObject(bitmap);
bitmap = LoadBitmap( module, MAKEINTRESOURCE( 101+resid ) );
if (!bitmap) continue;
if (!GetObject( bitmap, sizeof( bm ), reinterpret_cast<LPSTR>( &bm ))) continue;
last_id = resid;
}
if (bitmap) DeleteObject(bitmap);
FreeLibrary(module);
return last_id;
}