我试图理解为什么一个对象没有在我的代码中解除分配以及如何解决这个问题。我为所有类和一个外部库IndoorsSDK-iOS(3.3.0)启用了ARC。
我在下面的代码中没有释放属性del2(不调用IndoorsTestDelegate的dealloc方法)。 del2属性在registerLocationListener:message中传递给Indoors实例,并在视图控制器中的dealloc期间从中删除。当视图控制器被推回到导航控制器堆栈中时,将调用View Controller的dealloc方法。也调用了IndoorsTestDelegate中的del1 dealloc方法,但是没有调用del2 dealloc方法。我的问题是为什么没有调用del2 dealloc方法,我该如何解决这个问题。我没有来自外部图书馆的资源。
这是View Controller代码:
#import <Foundation/Foundation.h>
#import <Indoors/Indoors.h>
#import <IndoorsSurface/ISIndoorsSurfaceViewController.h>
@interface IndoorsTestDelegate : NSObject <IndoorsServiceDelegate, ISIndoorsSurfaceViewControllerDelegate, IndoorsLocationListener>
@property (weak, nonatomic) UIViewController *controller;
@property (strong, nonatomic) NSString *name;
- (instancetype)initWithController:(UIViewController *) controller
andName:(NSString *)name;
@end
这是IndoorsTestDelegate.h:
#import "IndoorsTestDelegate.h"
@implementation IndoorsTestDelegate
- (instancetype)initWithController:(UIViewController *)controller
andName:(NSString *)name
{
self = [super init];
if (self) {
_controller = controller;
_name = name;
}
return self;
}
- (void)dealloc {
// This method is called for del1 property
// but for del2 not. Why?
NSLog(@"Dealloc %@", _name);
}
// The protocols methods are empty in testing delegate
#pragma mark IndoorsLocationListener
- (void)zonesEntered:(NSArray *)zones {}
- (void)positionUpdated:(IDSCoordinate *)userPosition {}
- (void)contextUpdated:(IDSContext *)context {}
- (void)changedFloor:(int)floorLevel withName:(NSString *)name {}
- (void)weakSignal {}
- (void)orientationUpdated:(float)orientation {}
#pragma mark ISIndoorsSurfaceViewControllerDelegate
- (void)indoorsSurfaceViewController:(ISIndoorsSurfaceViewController *)indoorsSurfaceViewController isLoadingBuildingWithBuildingId:(NSUInteger)buildingId progress:(NSUInteger)progress {}
- (void)indoorsSurfaceViewController:(ISIndoorsSurfaceViewController *)indoorsSurfaceViewController didFinishLoadingBuilding:(IDSBuilding *)building {}
- (void)indoorsSurfaceViewController:(ISIndoorsSurfaceViewController *)indoorsSurfaceViewController didFailLoadingBuildingWithBuildingId:(NSUInteger)buildingId error:(NSError *)error {}
#pragma mark - IndoorsSurfaceServiceDelegate
- (void)onError:(IndoorsError *)indoorsError {}
- (void)bluetoothStateDidChange:(IDSBluetoothState)bluetoothState {}
- (void)locationAuthorizationStatusDidChange:(IDSLocationAuthorizationStatus)status {}
- (void)connected {}
@end
和IndoorsTestDelegate.m:
package myfirstapp.myapps.me.camera;
import ...
public class MainActivity extends ActionBarActivity {
ImageButton camBtn;
ImageView imageView;
ScrollView scrollView;
private File imageFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OpenCam(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"image.jpg");
Uri tempURI=Uri.fromFile(imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0)//==0 the same where startActivityForResult(intent, 0) so we are in the same process
{
switch (resultCode){
case Activity.RESULT_OK:
if(imageFile.exists())
{
Toast.makeText(MainActivity.this, "Image was saved at "+imageFile.getAbsolutePath(), Toast.LENGTH_SHORT)
.show();
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
}
else
{
Toast.makeText(MainActivity.this, "Image wasn't saved", Toast.LENGTH_SHORT)
.show();
}
break;
case Activity.RESULT_CANCELED:
Toast.makeText(MainActivity.this, "Image capture was cancelled", Toast.LENGTH_SHORT)
.show();
break;
}
}
}
}