Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WINAPI automatically free memory attached to controls?

Tags:

c

winapi

Just a simple question about WIN32 api.
I have a function that connects to MySQL using the MySQL C API, and that retrieves a linked list of pointers to structs.

So in a dialog callback I fill a ListView control by these elements, and attach every element struct pointer to a row in ListView.

The question is: should I free the pointers after removing all items from ListView by LVM_DELETEALLITEMS for example when I click a refresh button?
Or do the WIN32 API frees them for me?

like image 361
c-dev Avatar asked Dec 23 '16 22:12

c-dev


1 Answers

When deleting items from a list view, you are responsible for cleaning up any resources that are referenced through the lParam member of the LVITEM structure. The Windows API will not do this for you.

In fact, the Windows API cannot do this for you. It has no way of knowing, whether the pointer points to memory allocated using new, malloc, IMalloc, or any other allocator. Or maybe the pointer points into a statically allocated array, and doesn't need to be freed altogether. Or it may not even be a pointer, but rather a hash value for sorting, or an index into some other structure.

Since only you know, how to interpret the lParam, all responsibility with respect to resource management is on you. Contrary to some opinions, C++ will not help you here.

like image 146
IInspectable Avatar answered Nov 11 '22 14:11

IInspectable