简体   繁体   中英

Does this code leak memory (Objective-C global variables)?

Will these two functions leak memory if they get called many times? My knowledge of Objective-C is very rudimentary. They seem fine to me but I don't have a good feeling about that implementation? Should I remove that "retain"? Is that a proper way to store objects globally?

NSString* g_code = nil;

NSString* GetCode()
{
  if (!g_code)
  {
     std::string code = HelperFuncs::getCode();
     g_code = [[NSString stringWithUTF8String:code.c_str()] retain]; 
  }
  return g_code;
}

NSDictionary* g_options = nil;

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    g_options = [[NSDictionary alloc] initWithDictionary:userInfo];
}

我将在这里应用单例模式 ,如果可能的话:使用ARC,这会使生活变得更加轻松。

If you don't know much about Objective-C and working on a Non ARC Project, then its really hard to handle memory. One thing may help you, if you convert your project to ARC. Go to Xcode -> Edit -> Convert -> To Objective-C ARC... This way convert your project to ARC and let me know if this helps

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