Skip to content

Commit 7b2ac51

Browse files
Fixed memory management for Matrix Free. Initialize Matrix with UNDEFINED flag as PHP does not work with NULL.
1 parent 1154697 commit 7b2ac51

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

kernel/carray.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ void carray_init(int rows, int cols, MemoryPointer * ptr) {
3535
x.array2d = (float**)malloc(rows * sizeof(float*) + 64);
3636
for (i = 0; i < rows; ++i)
3737
x.array2d[i] = (float*)malloc(cols * sizeof(float));
38+
x.array1d = UNINITIALIZED;
39+
x.array0d = UNINITIALIZED;
3840
add_to_stack(ptr, x,(rows * cols * sizeof(float)) + 64);
3941
}
4042

@@ -49,6 +51,8 @@ void carray_init(int rows, int cols, MemoryPointer * ptr) {
4951
void carray_init1d(int width, MemoryPointer * ptr) {
5052
CArray x;
5153
int j, i;
54+
x.array0d = UNINITIALIZED;
55+
x.array2d = UNINITIALIZED;
5256
x.array1d = (float*)malloc(width * sizeof(float) + 64);
5357
add_to_stack(ptr, x,(width * sizeof(float)) + 64);
5458
}
@@ -63,6 +67,8 @@ void carray_init1d(int width, MemoryPointer * ptr) {
6367
void carray_init0d(MemoryPointer * ptr) {
6468
CArray x;
6569
int j, i;
70+
x.array1d = UNINITIALIZED;
71+
x.array2d = UNINITIALIZED;
6672
x.array0d = (float*)malloc(sizeof(float) + 64);
6773
add_to_stack(ptr, x,sizeof(float) + 64);
6874
}

phpsci.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,17 @@ static PHP_MSHUTDOWN_FUNCTION(phpsci)
296296
{
297297
int i;
298298
for(i = 0; i < PHPSCI_MAIN_MEM_STACK.size; i++) {
299-
if(PHPSCI_MAIN_MEM_STACK.buffer[i].array1d != NULL) {
299+
if(PHPSCI_MAIN_MEM_STACK.buffer[i].array0d != UNINITIALIZED) {
300+
free(PHPSCI_MAIN_MEM_STACK.buffer[i].array0d);
301+
}
302+
if(PHPSCI_MAIN_MEM_STACK.buffer[i].array1d != UNINITIALIZED) {
300303
free(PHPSCI_MAIN_MEM_STACK.buffer[i].array1d);
301304
}
302-
if(PHPSCI_MAIN_MEM_STACK.buffer[i].array2d != NULL) {
305+
if(PHPSCI_MAIN_MEM_STACK.buffer[i].array2d != UNINITIALIZED) {
303306
free(PHPSCI_MAIN_MEM_STACK.buffer[i].array2d[0]);
304307
free(PHPSCI_MAIN_MEM_STACK.buffer[i].array2d);
305308
}
306309
}
307-
free(PHPSCI_MAIN_MEM_STACK.buffer);
308310
UNREGISTER_INI_ENTRIES();
309311
return SUCCESS;
310312
}

0 commit comments

Comments
 (0)