2012-05-10 7 views
1

私はunity3D用のiOSプラグインを作成しています。以下はコードです。 EXC_BAD_EXCESSで[regionMonitor startMonitor]関数が呼び出されたとき、どのくらい爆撃されていますか?インターネットの投稿によると、メモリ管理エラーのようです。誰でも問題が何であるか見ることができます。ありがとう。CLLocationManagerのプラグインを作成する

"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; 

    } 
} 
+0

ユニティタグは、Microsoft Unity用です。それを悪用しないでください。 –

答えて

1

私はそれを正しく表示された場合は、autoreleaselocationManagerとどちらregionMonitorべきではありません。

deallocメソッドを追加すると、代わりにreleaselocationManagerとなります。 regionMonitorは、位置監視を停止すると解除される必要があります。

+0

ありがとうございます。それはうまくいった! ...なぜオートリリースが機能しないのか教えていただけますか? – ila

+0

オブジェクトが 'autoreleasing'すると、メソッドが呼び出し元に戻るとオブジェクトが解放され(したがって、この場合は破棄される)したがって、もう存在しないオブジェクトにアクセスしようとしています。これがわからない場合は、iOSのメモリ管理についてお読みください。 –

関連する問題