Skip to content

Commit

Permalink
macOS Fix potential memory leaks in the Metal renderer caught by clan…
Browse files Browse the repository at this point in the history
…g's static analyzer.
  • Loading branch information
slime73 committed Oct 26, 2019
1 parent 1ce1364 commit a9b867a
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/render/metal/SDL_render_metal.m
Expand Up @@ -173,6 +173,7 @@ - (void)dealloc
[_mtltexture release];
[_mtltexture_uv release];
[_mtlsampler release];
[_lockedbuffer release];
[super dealloc];
}
#endif
Expand Down Expand Up @@ -831,6 +832,7 @@ - (void)dealloc
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
int buffersize = 0;
id<MTLBuffer> lockedbuffer = nil;

if (rect->w <= 0 || rect->h <= 0) {
return SDL_SetError("Invalid rectangle dimensions for LockTexture.");
Expand All @@ -844,13 +846,19 @@ - (void)dealloc
buffersize = (*pitch) * rect->h;
}

texturedata.lockedrect = *rect;
texturedata.lockedbuffer = [data.mtldevice newBufferWithLength:buffersize options:MTLResourceStorageModeShared];
if (texturedata.lockedbuffer == nil) {
lockedbuffer = [data.mtldevice newBufferWithLength:buffersize options:MTLResourceStorageModeShared];
if (lockedbuffer == nil) {
return SDL_OutOfMemory();
}

*pixels = [texturedata.lockedbuffer contents];
texturedata.lockedrect = *rect;
texturedata.lockedbuffer = lockedbuffer;
*pixels = [lockedbuffer contents];

/* METAL_TextureData.lockedbuffer retains. */
#if !__has_feature(objc_arc)
[lockedbuffer release];
#endif

return 0;
}}
Expand Down Expand Up @@ -934,11 +942,7 @@ - (void)dealloc
[data.mtlcmdbuffer commit];
data.mtlcmdbuffer = nil;

#if !__has_feature(objc_arc)
[texturedata.lockedbuffer release];
#endif

texturedata.lockedbuffer = nil;
texturedata.lockedbuffer = nil; /* Retained property, so it calls release. */
texturedata.hasdata = YES;
}}

Expand Down Expand Up @@ -1299,6 +1303,8 @@ - (void)dealloc
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
METAL_DrawStateCache statecache;
SDL_zero(statecache);

id<MTLBuffer> mtlbufvertex = nil;

statecache.pipeline = nil;
Expand Down Expand Up @@ -1586,6 +1592,9 @@ - (void)dealloc
view = SDL_Metal_CreateView(window);

if (view == NULL) {
#if !__has_feature(objc_arc)
[mtldevice release];
#endif
SDL_free(renderer);
return NULL;
}
Expand All @@ -1594,6 +1603,9 @@ - (void)dealloc
data = [[METAL_RenderData alloc] init];

if (data == nil) {
#if !__has_feature(objc_arc)
[mtldevice release];
#endif
SDL_Metal_DestroyView(view);
SDL_free(renderer);
return NULL;
Expand Down

0 comments on commit a9b867a

Please sign in to comment.