简体   繁体   中英

Resource leak or not (Mac OS X)?

I'm trying to enumerate local users on Mac os. It works correctly, but i think that there is some resource leak. I can't understand that. Profiling says that there are no memory leaks, but memory usage are constantly grows (Memory Report chart at XCode). In my case since 2.7M to 4.9M (5 * 1000 iterations). Can anybody say what is wrong with my code. Are there any leaks or the behaviour is normal?

This is a simple c++ command line tool project with Objective-c code with default build settings (XCode 5):

/////////////////////////////////////////////
// main.cpp

#include "test.h"

#include <iostream>
#include <thread>

int main(int argc, const char * argv[])
{
  //for (int i = 0; i < 1000; ++i)
  for (int i = 0; i < 5; ++i)
  {
    std::cout << "Iteration # " << i << std::endl;

    for (int j = 0; j < 1000; ++j)
    {
      Execute();
    }

    std::this_thread::sleep_for(std::chrono::seconds(1));
  }

  return 0;
}


/////////////////////////////////////////////
// test.mm

#import <Collaboration/Collaboration.h>
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SCDynamicStore.h>
#import <SystemConfiguration/SCDynamicStoreCopySpecific.h>

#include <iostream>

void Execute()
{
  CSIdentityAuthorityRef identityAuthority = CSGetLocalIdentityAuthority();
  if (!identityAuthority)
  {
    std::cout << "Failed to get identity authority." << std::endl;
    return;
  }

  CSIdentityQueryRef usersQuery(CSIdentityQueryCreate(nil,  kCSIdentityClassUser, identityAuthority));
  if (!usersQuery)
  {
    std::cout << "Failed to create query." << std::endl;
    return;
  }

  /////////////////////////////////////////////////
  // Without CSIdentityQueryExecute(usersQuery, 0, nil) - everething is ok.
  /////////////////////////////////////////////////
  if (!CSIdentityQueryExecute(usersQuery, 0, nil))
  {
    std::cout << "Failed to execute query." << std::endl;
    return;
  }

  CFRelease(usersQuery);
}


#ifndef __MY_TEST_H__
#define __MY_TEST_H__

void Execute();

#endif

尝试在每次return之前执行CFRelease ,因为某些迭代不会释放数据。

I just ran this program, and I'm not seeing any memory growth. I slightly simplified it to be a single-file C++ program (currently it's a mix of C++ and ObjC++).

You do have a memory mistake, but I would only expect it to cause a leak if you were getting errors. This block leaks the query:

if (!CSIdentityQueryExecute(usersQuery, 0, nil))
{
  std::cout << "Failed to execute query." << std::endl;
  return;
}

You should either not return here (you don't technically need to), or you should include a CFRelease(usersQuery) before the return. But again, if this were the problem, you'd see lots of "Failed to execute query" log messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM