c++ - CComPtr and reference count -
i using objects of type ccomptr. having memory leak problems. in particular, have following code:
ccomptr<id2d1bitmap> bitmap = create_bitmap(bitmapsize); auto n = count_ref((id2d1bitmap*)bitmap);
where:
template<class interface> ulong count_ref(interface* pinterface) noexcept { if (pinterface) { pinterface->addref(); return pinterface->release(); } return 0; }
and:
id2d1bitmap* create_bitmap(const d2d1_size_u& size) { id2d1bitmap* bitmap; createbitmap(&bitmap); return bitmap; }
i expecting value of n
equal 1 equal 2. why reference count of ccomptr
not 1?
am using ccomptr
object properly?
and when process terminates following memory leak:
an interface [072b1f50] created not released. use 'dps 072b1f20' view allocation stack. object type: id2d1bitmap device-dependent size: 1000 x 600 device-independent size: 1000.00 x 600.00 format: dxgi_format_b8g8r8a8_unorm alpha mode: d2d1_alpha_mode_premultiplied outstanding reference count: 1 d2d debug error - memory leaks detected.
with ccomptr
need use raw interface pointer types.
you can make way, example:
ccomptr<id2d1bitmap> create_bitmap(const d2d1_size_u& size) { ccomptr<id2d1bitmap> bitmap; createbitmap(&bitmap); // declared createbitmap(id2d1bitmap**); return bitmap; } ccomptr<id2d1bitmap> pbitmap = create_bitmap(...); ...
ccomptr class manage references accurately along way pass pointers: local variable, return value, new local value. optimizing compiler in release builds removes of excessive addref/releases too, don't need worry them.
Comments
Post a Comment