Skip to content

Commit fdf790b

Browse files
author
abergeron
authored
Merge pull request #526 from notoraptor/fix-memory-leak-libgpuarray
Fix some memory leaks in libgpuarray
2 parents 39d49b2 + 06b2d4e commit fdf790b

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

pygpu/gpuarray.pyx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,17 +1431,20 @@ cdef GpuArray pygpu_reshape(GpuArray a, unsigned int nd, const size_t *newdims,
14311431
raise MemoryError, "could not allocate cdims"
14321432

14331433
cdef size_t d
1434-
for i in range(nd):
1435-
d = newdims[i]
1436-
if i == caxis:
1437-
d = a.size // tot
1434+
try:
1435+
for i in range(nd):
1436+
d = newdims[i]
1437+
if i == caxis:
1438+
d = a.size // tot
14381439

1439-
if d * tot != a.size:
1440-
raise GpuArrayException, "..."
1441-
cdims[i] = d
1440+
if d * tot != a.size:
1441+
raise GpuArrayException, "..."
1442+
cdims[i] = d
14421443

1443-
array_reshape(res, a, nd, cdims, ord, nocopy)
1444-
return res
1444+
array_reshape(res, a, nd, cdims, ord, nocopy)
1445+
return res
1446+
finally:
1447+
free(cdims)
14451448

14461449

14471450
cdef GpuArray pygpu_transpose(GpuArray a, const unsigned int *newaxes):

src/cache/disk.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,15 @@ static int key_path(disk_cache *c, const cache_key_t key, char *out) {
216216
unsigned char hash[64];
217217
int i;
218218

219-
if (c->kwrite(&kb, key)) return -1;
220-
if (Skein_512((unsigned char *)kb.s, kb.l, hash)) return -1;
219+
if (c->kwrite(&kb, key)) {
220+
strb_clear(&kb);
221+
return -1;
222+
}
223+
if (Skein_512((unsigned char *)kb.s, kb.l, hash)) {
224+
strb_clear(&kb);
225+
return -1;
226+
}
227+
strb_clear(&kb);
221228
if (snprintf(out, 10, "%02x%02x/%02x%02x",
222229
hash[0], hash[1], hash[2], hash[3]) != 9)
223230
return -1;

src/gpuarray_buffer_cuda.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,8 @@ static int compile(cuda_context *ctx, strb *src, strb* bin, strb *log) {
12231223

12241224
GA_CHECK(make_bin(ctx, &ptx, bin, log));
12251225

1226+
strb_clear(&ptx);
1227+
12261228
if (ctx->disk_cache) {
12271229
pk = calloc(sizeof(disk_key), 1);
12281230
if (pk == NULL) {
@@ -1234,23 +1236,23 @@ static int compile(cuda_context *ctx, strb *src, strb* bin, strb *log) {
12341236
memcpy(pk, &k, DISK_KEY_MM);
12351237
strb_appendb(&pk->src, src);
12361238
if (strb_error(&pk->src)) {
1237-
error_sys(ctx->err, "strb_appendb");
1239+
error_sys(ctx->err, "strb_appendb");
12381240
fprintf(stderr, "Error adding kernel to disk cache %s\n",
12391241
ctx->err->msg);
12401242
disk_free((cache_key_t)pk);
12411243
return GA_NO_ERROR;
12421244
}
12431245
cbin = strb_alloc(bin->l);
12441246
if (cbin == NULL) {
1245-
error_sys(ctx->err, "strb_alloc");
1247+
error_sys(ctx->err, "strb_alloc");
12461248
fprintf(stderr, "Error adding kernel to disk cache: %s\n",
12471249
ctx->err->msg);
12481250
disk_free((cache_key_t)pk);
12491251
return GA_NO_ERROR;
12501252
}
12511253
strb_appendb(cbin, bin);
12521254
if (strb_error(cbin)) {
1253-
error_sys(ctx->err, "strb_appendb");
1255+
error_sys(ctx->err, "strb_appendb");
12541256
fprintf(stderr, "Error adding kernel to disk cache %s\n",
12551257
ctx->err->msg);
12561258
disk_free((cache_key_t)pk);

src/gpuarray_elemwise.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -738,18 +738,24 @@ GpuElemwise *GpuElemwise_new(gpucontext *ctx,
738738

739739
void GpuElemwise_free(GpuElemwise *ge) {
740740
unsigned int i;
741-
for (i = 0; i < ge->nd; i++) {
742-
if (k_initialized(&ge->k_basic_32[i]))
743-
GpuKernel_clear(&ge->k_basic_32[i]);
744-
if (k_initialized(&ge->k_basic[i]))
745-
GpuKernel_clear(&ge->k_basic[i]);
746-
}
741+
if (ge->k_basic_32 != NULL)
742+
for (i = 0; i < ge->nd; i++) {
743+
if (k_initialized(&ge->k_basic_32[i]))
744+
GpuKernel_clear(&ge->k_basic_32[i]);
745+
}
746+
if (ge->k_basic != NULL)
747+
for (i = 0; i < ge->nd; i++) {
748+
if (k_initialized(&ge->k_basic[i]))
749+
GpuKernel_clear(&ge->k_basic[i]);
750+
}
747751
if (ge->strides != NULL)
748752
for (i = 0; i < ge->narray; i++) {
749753
free(ge->strides[i]);
750754
}
751755
if (k_initialized(&ge->k_contig))
752756
GpuKernel_clear(&ge->k_contig);
757+
free(ge->k_basic_32);
758+
free(ge->k_basic);
753759
free_args(ge->n, ge->args);
754760
free((void *)ge->preamble);
755761
free((void *)ge->expr);

0 commit comments

Comments
 (0)