Skip to content

Commit f3da91d

Browse files
committed
More robust result checks.
All returns from OpenAL are now checked. A failure will cause the affected object to abort construction. This includes OALSimpleAudio. This means that you must check the result of [OALSimpleAudio sharedInstance] at least once to be sure it successfully constructed.
1 parent e36e955 commit f3da91d

File tree

8 files changed

+104
-15
lines changed

8 files changed

+104
-15
lines changed

ObjectAL/ObjectAL (iOS)/OALSimpleAudio.m

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,29 @@ - (id) initWithReservedSources:(int) reservedSources
116116
OAL_LOG_DEBUG(@"%@: Init with %d reserved sources, %d mono, %d stereo",
117117
self, reservedSources, monoSources, stereoSources);
118118
device = [[ALDevice alloc] initWithDeviceSpecifier:nil];
119+
if(device == nil)
120+
{
121+
OAL_LOG_ERROR(@"%@: Could not create OpenAL device", self);
122+
goto initFailed;
123+
}
119124
context = [[ALContext alloc] initOnDevice:device
120125
outputFrequency:44100
121126
refreshIntervals:10
122127
synchronousContext:FALSE
123128
monoSources:monoSources
124129
stereoSources:stereoSources];
130+
if(context == nil)
131+
{
132+
OAL_LOG_ERROR(@"%@: Could not create OpenAL context", self);
133+
goto initFailed;
134+
}
125135
[self initCommon:reservedSources];
126136
}
127137
return self;
138+
139+
initFailed:
140+
as_release(self);
141+
return nil;
128142
}
129143

130144
- (id) initWithSources:(int) reservedSources
@@ -133,17 +147,34 @@ - (id) initWithSources:(int) reservedSources
133147
{
134148
OAL_LOG_DEBUG(@"%@: Init with %d reserved sources", self, reservedSources);
135149
device = [[ALDevice alloc] initWithDeviceSpecifier:nil];
150+
if(device == nil)
151+
{
152+
OAL_LOG_ERROR(@"%@: Could not create OpenAL device", self);
153+
goto initFailed;
154+
}
136155
context = [[ALContext alloc] initOnDevice:device attributes:nil];
156+
if(context == nil)
157+
{
158+
OAL_LOG_ERROR(@"%@: Could not create OpenAL contextself", self);
159+
goto initFailed;
160+
}
137161
[self initCommon:reservedSources];
138162
}
139163
return self;
164+
165+
initFailed:
166+
as_release(self);
167+
return nil;
140168
}
141169

142170
- (void) dealloc
143171
{
144172
OAL_LOG_DEBUG(@"%@: Dealloc", self);
145173
#if NS_BLOCKS_AVAILABLE && OBJECTAL_CFG_USE_BLOCKS && !__has_feature(objc_arc)
146-
dispatch_release(oal_dispatch_queue);
174+
if(oal_dispatch_queue != nil)
175+
{
176+
dispatch_release(oal_dispatch_queue);
177+
}
147178
#endif
148179

149180
as_release(backgroundTrack);

ObjectAL/ObjectAL (iOS)/OpenAL/ALBuffer.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,28 @@ - (id) initWithName:(NSString*) nameIn
6565
bufferId = [ALWrapper genBuffer];
6666
if(nil == [OpenALManager sharedInstance].currentContext)
6767
{
68-
OAL_LOG_ERROR(@"Cannot allocate a buffer without a current context. Make sure [OpenALManager sharedInstance].currentContext is valid");
69-
as_release(self);
70-
return nil;
68+
OAL_LOG_ERROR(@"%@: Cannot allocate a buffer without a current context. Make sure [OpenALManager sharedInstance].currentContext is valid", self);
69+
goto initFailed;
7170
}
7271
device = as_retain([OpenALManager sharedInstance].currentContext.device);
7372
bufferData = data;
7473
format = formatIn;
7574
freeDataOnDestroy = YES;
7675
parentBuffer = nil;
7776

78-
[ALWrapper bufferDataStatic:bufferId format:format data:bufferData size:size frequency:frequency];
77+
if(![ALWrapper bufferDataStatic:bufferId format:format data:bufferData size:size frequency:frequency])
78+
{
79+
OAL_LOG_ERROR(@"%@: Failed to create an OpenAL buffer", self);
80+
goto initFailed;
81+
}
7982

8083
duration = (float)self.size / ((float)(self.frequency * self.channels * self.bits) / 8);
8184
}
8285
return self;
86+
87+
initFailed:
88+
as_release(self);
89+
return nil;
8390
}
8491

8592
- (void) dealloc

ObjectAL/ObjectAL (iOS)/OpenAL/ALCaptureDevice.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@ - (id) initWithDeviceSpecifier:(NSString*) deviceSpecifier
5959
frequency:frequency
6060
format:format
6161
bufferSize:bufferSize];
62+
if(device == nil)
63+
{
64+
OAL_LOG_ERROR(@"%@: Failed to initialize OpenAL capture device", self);
65+
goto initFailed;
66+
}
6267
}
6368
return self;
69+
70+
initFailed:
71+
as_release(self);
72+
return nil;
6473
}
6574

6675
- (void) dealloc

ObjectAL/ObjectAL (iOS)/OpenAL/ALChannelSource.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,24 @@ - (id) initWithSources:(int) reservedSources
102102
OAL_LOG_DEBUG(@"%@: Init with %d sources", self, reservedSources);
103103

104104
context = as_retain([OpenALManager sharedInstance].currentContext);
105+
if(context == nil)
106+
{
107+
OAL_LOG_ERROR(@"%@: Could not initialize channel: Context is nil", self);
108+
goto initFailed;
109+
}
105110

106111
sourcePool = [[ALSoundSourcePool alloc] init];
107112

108113
for(int i = 0; i < reservedSources; i++)
109114
{
110-
[self addSource:[ALSource source]];
115+
[self addSource:nil];
111116
}
112117
}
113118
return self;
119+
120+
initFailed:
121+
as_release(self);
122+
return nil;
114123
}
115124

116125
- (void) dealloc
@@ -517,6 +526,11 @@ - (void) addSource:(id<ALSoundSource>) source
517526
if(nil == source)
518527
{
519528
source = [ALSource source];
529+
if(source == nil)
530+
{
531+
OAL_LOG_ERROR(@"%@: Could not create an OpenAL source", self);
532+
return;
533+
}
520534
}
521535
if(defaultsInitialized)
522536
{

ObjectAL/ObjectAL (iOS)/OpenAL/ALContext.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ - (id) initOnDevice:(ALDevice *) deviceIn attributes:(NSArray*) attributesIn
150150

151151
if(nil == deviceIn)
152152
{
153-
OAL_LOG_ERROR(@"%@: Failed to init because device was nil. Returning nil", self);
154-
as_release(self);
155-
return nil;
153+
OAL_LOG_ERROR(@"%@: Could not init context: Device is nil", self);
154+
goto initFailed;
156155
}
157156

158157
suspendHandler = [[OALSuspendHandler alloc] initWithTarget:self selector:@selector(setSuspended:)];
@@ -176,6 +175,11 @@ - (id) initOnDevice:(ALDevice *) deviceIn attributes:(NSArray*) attributesIn
176175

177176
// Open the context with our list of attributes.
178177
context = [ALWrapper createContext:device.device attributes:attributesList];
178+
if(context == nil)
179+
{
180+
OAL_LOG_ERROR(@"%@: Failed to create OpenAL context", self);
181+
goto initFailed;
182+
}
179183

180184
listener = [[ALListener alloc] initWithContext:self];
181185

@@ -213,6 +217,10 @@ - (id) initOnDevice:(ALDevice *) deviceIn attributes:(NSArray*) attributesIn
213217
[device addSuspendListener:self];
214218
}
215219
return self;
220+
221+
initFailed:
222+
as_release(self);
223+
return nil;
216224
}
217225

218226
- (void) dealloc

ObjectAL/ObjectAL (iOS)/OpenAL/ALDevice.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ - (id) initWithDeviceSpecifier:(NSString*) deviceSpecifier
6060
device = [ALWrapper openDevice:deviceSpecifier];
6161
if(nil == device)
6262
{
63-
OAL_LOG_ERROR(@"%@: Failed to init device %@. Returning nil", self, deviceSpecifier);
64-
as_release(self);
65-
return nil;
63+
OAL_LOG_ERROR(@"%@: Failed to create OpenAL device %@", self, deviceSpecifier);
64+
goto initFailed;
6665
}
6766
}
6867
return self;
68+
69+
initFailed:
70+
as_release(self);
71+
return nil;
6972
}
7073

7174
- (void) dealloc

ObjectAL/ObjectAL (iOS)/OpenAL/ALListener.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,21 @@ - (id) initWithContext:(ALContext*) contextIn
5656
{
5757
if(nil != (self = [super init]))
5858
{
59+
if(contextIn == nil)
60+
{
61+
OAL_LOG_ERROR(@"%@: Could not init listener: Context is nil", self);
62+
goto initFailed;
63+
}
5964
suspendHandler = [[OALSuspendHandler alloc] initWithTarget:nil selector:nil];
6065

6166
self.context = contextIn;
6267
gain = 1.0f;
6368
}
6469
return self;
70+
71+
initFailed:
72+
as_release(self);
73+
return nil;
6574
}
6675

6776
- (void) dealloc

ObjectAL/ObjectAL (iOS)/OpenAL/ALSource.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ - (id) initOnContext:(ALContext*) contextIn
109109

110110
if(nil == contextIn)
111111
{
112-
OAL_LOG_ERROR(@"%@: Failed to init because context was nil. Returning nil", self);
113-
as_release(self);
114-
return nil;
112+
OAL_LOG_ERROR(@"%@: Failed to init source: Context is nil", self);
113+
goto initFailed;
115114
}
116115

117116
suspendHandler = [[OALSuspendHandler alloc] initWithTarget:self selector:@selector(setSuspended:)];
@@ -123,6 +122,11 @@ - (id) initOnContext:(ALContext*) contextIn
123122
ALContext* realContext = [OpenALManager sharedInstance].currentContext;
124123
[OpenALManager sharedInstance].currentContext = context;
125124
sourceId = [ALWrapper genSource];
125+
if(sourceId == (ALuint)AL_INVALID)
126+
{
127+
OAL_LOG_ERROR(@"%@: Failed to create OpenAL source", self);
128+
goto initFailed;
129+
}
126130
[OpenALManager sharedInstance].currentContext = realContext;
127131
}
128132
OAL_LOG_DEBUG(@"%@: Created source %08x", self, sourceId);
@@ -135,6 +139,10 @@ - (id) initOnContext:(ALContext*) contextIn
135139
[[self class] notifySourceAllocated:self];
136140
}
137141
return self;
142+
143+
initFailed:
144+
as_release(self);
145+
return nil;
138146
}
139147

140148
- (void) dealloc

0 commit comments

Comments
 (0)