瀏覽代碼

Try to solve memory leak problems

Yann Weber 6 年之前
父節點
當前提交
50d59eddd0
共有 2 個文件被更改,包括 70 次插入27 次删除
  1. 66
    27
      libs/cturmit.c
  2. 4
    0
      libs/cturmit.h

+ 66
- 27
libs/cturmit.c 查看文件

@@ -25,16 +25,21 @@ CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds)
25 25
 	char err_msg[128];
26 26
 	ssize_t arg_stk_sz, arg_int_max;
27 27
 	char *arg_expr;
28
-	int i;
28
+	int i, ret;
29 29
 	CTurmit *turmit;
30
-	PyObject *stack_size, *int_max, *expr, *keys;
30
+	PyObject *stack_size, *int_max, *expr, *keys, *asc_str;
31 31
 	PyObject *errtype, *errvalue, *errbck;
32 32
 	
33 33
 	PyErr_Fetch(&errtype, &errvalue, &errbck);
34 34
 	turmit  = (CTurmit*)PyMapping_GetItemString(kwds, "turmit");
35 35
 	if(turmit)
36 36
 	{
37
-		return _CTurmit_copy_init(self, turmit);
37
+		Py_XDECREF(errtype);
38
+		Py_XDECREF(errvalue);
39
+		Py_XDECREF(errbck);
40
+		ret = _CTurmit_copy_init(self, turmit);
41
+		Py_DECREF(turmit);
42
+		return ret;
38 43
 	}
39 44
 	PyErr_Restore(errtype, errvalue, errbck);
40 45
 
@@ -49,8 +54,9 @@ CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds)
49 54
 			PyErr_Restore(errtype, errvalue, errbck);
50 55
 			snprintf(err_msg, 128, "1 positional argument expected \
51 56
 but %ld found", sz);
52
-			PyErr_SetString(PyExc_TypeError, err_msg);
53
-			return -1;
57
+			PyErr_SetString(PyExc_TypeError,
58
+				err_msg);
59
+			goto cturmit_init_error;
54 60
 		}
55 61
 		stack_size = PySequence_GetItem(args, 0);
56 62
 	}
@@ -64,17 +70,23 @@ but %ld found", sz);
64 70
 		if(!PyLong_CheckExact(stack_size))
65 71
 		{
66 72
 			PyErr_Restore(errtype, errvalue, errbck);
67
-			PyErr_SetString(PyExc_TypeError,
73
+			Py_DECREF(stack_size);
74
+			snprintf(err_msg, 128,
68 75
 				"stack_size expected to be an integer");
69
-			return -1;
76
+			PyErr_SetString(PyExc_TypeError,
77
+				err_msg);
78
+			goto cturmit_init_error;
70 79
 		}
71 80
 		arg_stk_sz = PyLong_AsSsize_t(stack_size);
81
+		Py_DECREF(stack_size);
72 82
 		if(arg_stk_sz == (size_t)-1 && PyErr_Occurred())
73 83
 		{
74 84
 			PyErr_Restore(errtype, errvalue, errbck);
75
-			PyErr_SetString(PyExc_ValueError,
85
+			snprintf(err_msg, 128,
76 86
 				"size_t overflow with stack_size");
77
-			return -1;
87
+			PyErr_SetString(PyExc_ValueError,
88
+				err_msg);
89
+			goto cturmit_init_error;
78 90
 		}
79 91
 	}
80 92
 	else
@@ -89,15 +101,22 @@ but %ld found", sz);
89 101
 		if(!PyLong_CheckExact(int_max))
90 102
 		{
91 103
 			PyErr_Restore(errtype, errvalue, errbck);
92
-			PyErr_SetString(PyExc_TypeError,
104
+			snprintf(err_msg, 128,
93 105
 				"int_max expected to be an integer");
94
-			return -1;
106
+			PyErr_SetString(PyExc_TypeError,
107
+				err_msg);
108
+			goto cturmit_init_error;
95 109
 		}
96 110
 		arg_int_max = PyLong_AsSsize_t(int_max);
111
+		Py_DECREF(int_max);
97 112
 		if(arg_int_max == (size_t)-1 && PyErr_Occurred())
98 113
 		{
114
+			PyErr_Restore(errtype, errvalue, errbck);
99 115
 			PyErr_SetString(PyExc_TypeError,
100 116
 				"size_t overflow with stack_size");
117
+			PyErr_SetString(PyExc_ValueError,
118
+				err_msg);
119
+			goto cturmit_init_error;
101 120
 		}
102 121
 	}
103 122
 	else
@@ -112,25 +131,33 @@ but %ld found", sz);
112 131
 		if(!PyUnicode_Check(expr) && !(expr = PyObject_Str(expr)))
113 132
 		{
114 133
 			PyErr_Restore(errtype, errvalue, errbck);
115
-			PyErr_SetString(PyExc_TypeError,
134
+			snprintf(err_msg, 128,
116 135
 				"prog expected to be a string !");
117
-			return -1;
136
+			PyErr_SetString(PyExc_TypeError,
137
+				err_msg);
138
+			goto cturmit_init_error;
118 139
 		}
119
-		expr = PyUnicode_AsASCIIString(expr);
120
-		if(!expr)
140
+		asc_str = PyUnicode_AsASCIIString(expr);
141
+		Py_DECREF(expr);
142
+		if(!asc_str)
121 143
 		{
122 144
 			PyErr_Restore(errtype, errvalue, errbck);
123
-			PyErr_SetString(PyExc_TypeError,
145
+			snprintf(err_msg, 128,
124 146
 				"prog expected to be an ASCII string");
125
-			return -1;
147
+			PyErr_SetString(PyExc_ValueError,
148
+				err_msg);
149
+			goto cturmit_init_error;
126 150
 		}
127
-		arg_expr = (char*)PyBytes_AsString(expr);
151
+		arg_expr = (char*)PyBytes_AsString(asc_str);
152
+		Py_DECREF(asc_str);
128 153
 		if(!arg_expr)
129 154
 		{
130 155
 			PyErr_Restore(errtype, errvalue, errbck);
131
-			PyErr_SetString(PyExc_TypeError,
156
+			snprintf(err_msg, 128,
132 157
 				"Unable to convert prog to Cstring");
133
-			return -1;
158
+			PyErr_SetString(PyExc_RuntimeError,
159
+				err_msg);
160
+			goto cturmit_init_error;
134 161
 		}
135 162
 	}
136 163
 	else
@@ -144,17 +171,18 @@ but %ld found", sz);
144 171
 		strncpy(err_msg, "Unrecognized arguments : '", 128);
145 172
 		for(i=0; i<PyList_Size(keys); i++)
146 173
 		{
174
+			asc_str = PyUnicode_AsASCIIString(
175
+						PyList_GetItem(keys, i));
147 176
 			strncat(err_msg,
148
-				(char*)PyBytes_AsString(
149
-					PyUnicode_AsASCIIString(
150
-						PyList_GetItem(keys, i))),
151
-				128);
177
+				(char*)PyBytes_AsString(asc_str),128);
152 178
 			strncat(err_msg, "', '", 128);
153 179
 		}
180
+		Py_DECREF(keys);
154 181
 		strncat(err_msg, "'", 128);
182
+		PyErr_Restore(errtype, errvalue, errbck);
155 183
 		PyErr_SetString(PyExc_RuntimeError,
156 184
 			err_msg);
157
-		return -1;
185
+		goto cturmit_init_error;
158 186
 	}
159 187
 
160 188
 	PyErr_Restore(errtype, errvalue, errbck);
@@ -162,14 +190,25 @@ but %ld found", sz);
162 190
 		TURMIT_AUTOCOMP);
163 191
 	if(self->turmit->err)
164 192
 	{
193
+		PyErr_Restore(errtype, errvalue, errbck);
165 194
 		snprintf(err_msg, 128,
166 195
 			"Unable to compile '%s' err : %s", arg_expr,
167 196
 			self->turmit->err_str);
168 197
 		PyErr_SetString(PyExc_ValueError,
169 198
 			err_msg);
170
-		return -1;
199
+		goto cturmit_init_error;
171 200
 	}
201
+	Py_XDECREF(errtype);
202
+	Py_XDECREF(errvalue);
203
+	Py_XDECREF(errbck);
172 204
 	return 0;
205
+
206
+	cturmit_init_error:
207
+		Py_XDECREF(errtype);
208
+		Py_XDECREF(errvalue);
209
+		Py_XDECREF(errbck);
210
+		return -1;
211
+
173 212
 }
174 213
 
175 214
 static PyObject *
@@ -238,8 +277,8 @@ PyObject* CTurmit_call(PyObject *func, PyObject *args, PyObject *kwds)
238 277
 				err_msg);
239 278
 			Py_RETURN_NONE;
240 279
 		}
280
+                Py_DECREF(arg);
241 281
 	}
242
-
243 282
 	res = turmit_exec(self->turmit, exec_args);
244 283
 	return PyLong_FromUnsignedLongLong(res);
245 284
 }

+ 4
- 0
libs/cturmit.h 查看文件

@@ -3,6 +3,10 @@
3 3
 
4 4
 #include "turmit.h"
5 5
 
6
+/**
7
+ * @todo search for decref and memory leaks :/
8
+ */
9
+
6 10
 typedef struct
7 11
 {
8 12
 	PyObject VAR_HEAD;

Loading…
取消
儲存