Search code examples
c++cstat

Programmatically deallocate memory in libstagrab


When and how do we free the memory when using libstatgrab. I didn't see any function call like sg_free that deallocates the memory object for storing the system statistics. For example:

sg_init(1);
sg_drop_privileges()
sg_load_stats *load_stat;
while(load_stat = sg_get_load_stats(NULL) != NULL) {
  cout << load_stat->min1 << load_stat->min5 << load_stat->min15);
}

The above while loop keeps calling the sg_get_load_stats function to get the cpu load. Does it use internal buffer or create a new one every time it is called? If the later is true, shouldn't we free the object? Thanks.


Solution

  • The Documentation appears to state that sg_get_load_stats() has local scope and is handled by the libstatgrab library.

    sg_get_load_stats_r(), which is presumably used to return the value of the stats out of the current scope, dynamically allocates and the resulting buffer needs to be cleaned up by the user.