【问题标题】:Creating Plugin for CLLocationManager为 CLLocationManager 创建插件
【发布时间】:2012-05-10 10:56:29
【问题描述】:

我正在为 unity3D 创建一个 iOS 插件。下面是代码。当使用 EXC_BAD_EXCESS 调用 [regionMonitor startMonitor] 函数时,它是如何爆炸的。根据互联网帖子,这似乎是一个内存管理错误。谁能看到这里有什么问题。谢谢。

“RegionMonitoringPlugin.h”

 #import <Foundation/Foundation.h>
 #import <CoreLocation/CoreLocation.h>


@interface RegionMonitoringPlugin : NSObject <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager; 
}

-(void)leavingHomeNotify;
-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)raduis;

@end

“RegionMonitoringPlugin.mm”

#import "RegionMonitoringPlugin.h"

@implementation RegionMonitoringPlugin

- (id) init
{
    if (self = [super init])
   {
      locationManager = [[[CLLocationManager alloc] init] autorelease];
      locationManager.delegate = self;
      [locationManager setDistanceFilter:kCLDistanceFilterNone];
      [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
   }
return self;
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    [self leavingHomeNotify];
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    [self leavingHomeNotify];
}

 - (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)regionwithError:(NSError *)error
{
    NSLog(@"Location error %@, %@", error, @"Fill in the reason here");
}

-(void)leavingHomeNotify
{
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody= @"Region Left";
[[UIApplication sharedApplication] presentLocalNotificationNow:note];
[note release];
 }

 -(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)radius
 {
  CLLocationCoordinate2D home;
  home.latitude = latitude;
  home.longitude = longitude;
  CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:radius identifier:@"home"];
  [locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
  [region release];    
 }

@end

extern "C" {

    static RegionMonitoringPlugin *regionMonitor;

    // Unity callable function to start region monitoring
    BOOL _startRegionMonitoring(float m_latitude,float m_longitude, float m_radius)
    {
        if (![CLLocationManager regionMonitoringAvailable] || ![CLLocationManager regionMonitoringEnabled] )
            return NO;
        if (regionMonitor == nil){
            regionMonitor = [[[RegionMonitoringPlugin alloc]init ] autorelease];
        }
        [regionMonitor startMonitor:m_latitude longitude:m_longitude radius:m_radius];
        return YES;

    }
}

【问题讨论】:

  • 统一标签适用于 Microsoft Unity。请不要滥用它。

标签: ios plugins unity3d cllocationmanager


【解决方案1】:

如果我没看错,你不应该 autorelease locationManagerregionMonitor

添加一个 dealloc 方法,而不是 release locationManager。停止位置监控后,regionMonitor 应该被释放。

【讨论】:

  • 谢谢。那行得通! ...你能告诉我为什么自动释放不起作用吗?
  • 因为autoreleasing 一个对象会导致它在方法返回给调用者时被释放(在这种情况下因此被销毁)。所以你试图访问一个不再存在的对象。如果您不清楚这一点,您应该阅读 iOS 中的内存管理。
猜你喜欢
  • 1970-01-01
  • 2020-07-20
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
  • 2014-03-29
  • 1970-01-01
相关资源
最近更新 更多