Fix memory leaks and add checks for failing malloc() calls to testcapi module

CID 1058288
This commit is contained in:
Christian Heimes 2013-07-26 15:03:50 +02:00
parent ff369a5595
commit 7e138027ff
1 changed files with 9 additions and 0 deletions

View File

@ -2183,6 +2183,8 @@ profile_int(PyObject *self, PyObject* args)
/* Test 3: Allocate a few integers, then release /* Test 3: Allocate a few integers, then release
them all simultaneously. */ them all simultaneously. */
multiple = malloc(sizeof(PyObject*) * 1000); multiple = malloc(sizeof(PyObject*) * 1000);
if (multiple == NULL)
return PyErr_NoMemory();
gettimeofday(&start, NULL); gettimeofday(&start, NULL);
for(k=0; k < 20000; k++) { for(k=0; k < 20000; k++) {
for(i=0; i < 1000; i++) { for(i=0; i < 1000; i++) {
@ -2194,10 +2196,13 @@ profile_int(PyObject *self, PyObject* args)
} }
gettimeofday(&stop, NULL); gettimeofday(&stop, NULL);
print_delta(3, &start, &stop); print_delta(3, &start, &stop);
free(multiple);
/* Test 4: Allocate many integers, then release /* Test 4: Allocate many integers, then release
them all simultaneously. */ them all simultaneously. */
multiple = malloc(sizeof(PyObject*) * 1000000); multiple = malloc(sizeof(PyObject*) * 1000000);
if (multiple == NULL)
return PyErr_NoMemory();
gettimeofday(&start, NULL); gettimeofday(&start, NULL);
for(k=0; k < 20; k++) { for(k=0; k < 20; k++) {
for(i=0; i < 1000000; i++) { for(i=0; i < 1000000; i++) {
@ -2209,9 +2214,12 @@ profile_int(PyObject *self, PyObject* args)
} }
gettimeofday(&stop, NULL); gettimeofday(&stop, NULL);
print_delta(4, &start, &stop); print_delta(4, &start, &stop);
free(multiple);
/* Test 5: Allocate many integers < 32000 */ /* Test 5: Allocate many integers < 32000 */
multiple = malloc(sizeof(PyObject*) * 1000000); multiple = malloc(sizeof(PyObject*) * 1000000);
if (multiple == NULL)
return PyErr_NoMemory();
gettimeofday(&start, NULL); gettimeofday(&start, NULL);
for(k=0; k < 10; k++) { for(k=0; k < 10; k++) {
for(i=0; i < 1000000; i++) { for(i=0; i < 1000000; i++) {
@ -2223,6 +2231,7 @@ profile_int(PyObject *self, PyObject* args)
} }
gettimeofday(&stop, NULL); gettimeofday(&stop, NULL);
print_delta(5, &start, &stop); print_delta(5, &start, &stop);
free(multiple);
/* Test 6: Perform small int addition */ /* Test 6: Perform small int addition */
op1 = PyLong_FromLong(1); op1 = PyLong_FromLong(1);