patternMinor
Is there a memory leak in here?
Viewed 0 times
therehereleakmemory
Problem
I'm newish to obj-c and I have some code that works, but I'm thinking I may be leaking memory as I have two
In both cases if I remove the
Am I doing this right or is there a better way?
```
//===================================================================================================
{
self = [super init];
if (self) {
indexDeviceSearch = 0;
// Grab a list of paired devices
btPairedDevices = [IOBluetoothDevice pairedDevices];
[btPairedDevices retain];
}
return self;
}
//===================================================================================================
{
if(IOBluetoothValidateHardwareWithDescription(nil, nil) != kIOReturnSuccess)
{
[NSApp terminate:self];
}
NSLog(@"Application up an running");
// Start our timer looking for paired phones
timerDeviceSearch = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerDeviceSearchFired)
userInfo:nil
repeats:YES];
}
//===================================================================================================
{
// Check out the device
if (indexDeviceSearch = [btPairedDevices count])
indexDeviceSearch = 0;
}
//===========================================================================================================================
-(BOOL)addNewDeviceIfAcceptable:(IOBluetoothDevice*)device
{
NSEnumerator *enumerator;
IOBluetoothDevice *tmpDevice;
const BluetoothDeviceAddress *newDeviceAddress = [device getAddre
retain statements and no release statements.In both cases if I remove the
retain I get a bad access error on [btPairedDevices count]Am I doing this right or is there a better way?
```
//===================================================================================================
- (id)init
{
self = [super init];
if (self) {
indexDeviceSearch = 0;
// Grab a list of paired devices
btPairedDevices = [IOBluetoothDevice pairedDevices];
[btPairedDevices retain];
}
return self;
}
//===================================================================================================
- (void) applicationDidFinishLaunching:(NSNotification *)notification
{
if(IOBluetoothValidateHardwareWithDescription(nil, nil) != kIOReturnSuccess)
{
[NSApp terminate:self];
}
NSLog(@"Application up an running");
// Start our timer looking for paired phones
timerDeviceSearch = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerDeviceSearchFired)
userInfo:nil
repeats:YES];
}
//===================================================================================================
- (void) timerDeviceSearchFired
{
// Check out the device
if (indexDeviceSearch = [btPairedDevices count])
indexDeviceSearch = 0;
}
//===========================================================================================================================
-(BOOL)addNewDeviceIfAcceptable:(IOBluetoothDevice*)device
{
NSEnumerator *enumerator;
IOBluetoothDevice *tmpDevice;
const BluetoothDeviceAddress *newDeviceAddress = [device getAddre
Solution
You should definitely be releasing both
You shouldn't need the
So to sum up make sure you're releasing both in your
btMonitoredDevices and btPairedDevices in the dealloc. But it appears that this is your appDelegate so it matters little outside of correctness. I'd do it anyhow.You shouldn't need the
retain on btMonitoredDevices since you're calling alloc on it. So you already have a retain count of one on it. By calling retain on it you actually have a retain count of two. So to sum up make sure you're releasing both in your
dealloc and get rid of the [btMonitedDecices retain]; and you should be good.Context
StackExchange Code Review Q#7952, answer score: 4
Revisions (0)
No revisions yet.