Browse Source

Try to solve memory leak problems

Yann Weber 6 years ago
parent
commit
50d59eddd0
2 changed files with 70 additions and 27 deletions
  1. 66
    27
      libs/cturmit.c
  2. 4
    0
      libs/cturmit.h

+ 66
- 27
libs/cturmit.c View File

25
 	char err_msg[128];
25
 	char err_msg[128];
26
 	ssize_t arg_stk_sz, arg_int_max;
26
 	ssize_t arg_stk_sz, arg_int_max;
27
 	char *arg_expr;
27
 	char *arg_expr;
28
-	int i;
28
+	int i, ret;
29
 	CTurmit *turmit;
29
 	CTurmit *turmit;
30
-	PyObject *stack_size, *int_max, *expr, *keys;
30
+	PyObject *stack_size, *int_max, *expr, *keys, *asc_str;
31
 	PyObject *errtype, *errvalue, *errbck;
31
 	PyObject *errtype, *errvalue, *errbck;
32
 	
32
 	
33
 	PyErr_Fetch(&errtype, &errvalue, &errbck);
33
 	PyErr_Fetch(&errtype, &errvalue, &errbck);
34
 	turmit  = (CTurmit*)PyMapping_GetItemString(kwds, "turmit");
34
 	turmit  = (CTurmit*)PyMapping_GetItemString(kwds, "turmit");
35
 	if(turmit)
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
 	PyErr_Restore(errtype, errvalue, errbck);
44
 	PyErr_Restore(errtype, errvalue, errbck);
40
 
45
 
49
 			PyErr_Restore(errtype, errvalue, errbck);
54
 			PyErr_Restore(errtype, errvalue, errbck);
50
 			snprintf(err_msg, 128, "1 positional argument expected \
55
 			snprintf(err_msg, 128, "1 positional argument expected \
51
 but %ld found", sz);
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
 		stack_size = PySequence_GetItem(args, 0);
61
 		stack_size = PySequence_GetItem(args, 0);
56
 	}
62
 	}
64
 		if(!PyLong_CheckExact(stack_size))
70
 		if(!PyLong_CheckExact(stack_size))
65
 		{
71
 		{
66
 			PyErr_Restore(errtype, errvalue, errbck);
72
 			PyErr_Restore(errtype, errvalue, errbck);
67
-			PyErr_SetString(PyExc_TypeError,
73
+			Py_DECREF(stack_size);
74
+			snprintf(err_msg, 128,
68
 				"stack_size expected to be an integer");
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
 		arg_stk_sz = PyLong_AsSsize_t(stack_size);
80
 		arg_stk_sz = PyLong_AsSsize_t(stack_size);
81
+		Py_DECREF(stack_size);
72
 		if(arg_stk_sz == (size_t)-1 && PyErr_Occurred())
82
 		if(arg_stk_sz == (size_t)-1 && PyErr_Occurred())
73
 		{
83
 		{
74
 			PyErr_Restore(errtype, errvalue, errbck);
84
 			PyErr_Restore(errtype, errvalue, errbck);
75
-			PyErr_SetString(PyExc_ValueError,
85
+			snprintf(err_msg, 128,
76
 				"size_t overflow with stack_size");
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
 	else
92
 	else
89
 		if(!PyLong_CheckExact(int_max))
101
 		if(!PyLong_CheckExact(int_max))
90
 		{
102
 		{
91
 			PyErr_Restore(errtype, errvalue, errbck);
103
 			PyErr_Restore(errtype, errvalue, errbck);
92
-			PyErr_SetString(PyExc_TypeError,
104
+			snprintf(err_msg, 128,
93
 				"int_max expected to be an integer");
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
 		arg_int_max = PyLong_AsSsize_t(int_max);
110
 		arg_int_max = PyLong_AsSsize_t(int_max);
111
+		Py_DECREF(int_max);
97
 		if(arg_int_max == (size_t)-1 && PyErr_Occurred())
112
 		if(arg_int_max == (size_t)-1 && PyErr_Occurred())
98
 		{
113
 		{
114
+			PyErr_Restore(errtype, errvalue, errbck);
99
 			PyErr_SetString(PyExc_TypeError,
115
 			PyErr_SetString(PyExc_TypeError,
100
 				"size_t overflow with stack_size");
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
 	else
122
 	else
112
 		if(!PyUnicode_Check(expr) && !(expr = PyObject_Str(expr)))
131
 		if(!PyUnicode_Check(expr) && !(expr = PyObject_Str(expr)))
113
 		{
132
 		{
114
 			PyErr_Restore(errtype, errvalue, errbck);
133
 			PyErr_Restore(errtype, errvalue, errbck);
115
-			PyErr_SetString(PyExc_TypeError,
134
+			snprintf(err_msg, 128,
116
 				"prog expected to be a string !");
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
 			PyErr_Restore(errtype, errvalue, errbck);
144
 			PyErr_Restore(errtype, errvalue, errbck);
123
-			PyErr_SetString(PyExc_TypeError,
145
+			snprintf(err_msg, 128,
124
 				"prog expected to be an ASCII string");
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
 		if(!arg_expr)
153
 		if(!arg_expr)
129
 		{
154
 		{
130
 			PyErr_Restore(errtype, errvalue, errbck);
155
 			PyErr_Restore(errtype, errvalue, errbck);
131
-			PyErr_SetString(PyExc_TypeError,
156
+			snprintf(err_msg, 128,
132
 				"Unable to convert prog to Cstring");
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
 	else
163
 	else
144
 		strncpy(err_msg, "Unrecognized arguments : '", 128);
171
 		strncpy(err_msg, "Unrecognized arguments : '", 128);
145
 		for(i=0; i<PyList_Size(keys); i++)
172
 		for(i=0; i<PyList_Size(keys); i++)
146
 		{
173
 		{
174
+			asc_str = PyUnicode_AsASCIIString(
175
+						PyList_GetItem(keys, i));
147
 			strncat(err_msg,
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
 			strncat(err_msg, "', '", 128);
178
 			strncat(err_msg, "', '", 128);
153
 		}
179
 		}
180
+		Py_DECREF(keys);
154
 		strncat(err_msg, "'", 128);
181
 		strncat(err_msg, "'", 128);
182
+		PyErr_Restore(errtype, errvalue, errbck);
155
 		PyErr_SetString(PyExc_RuntimeError,
183
 		PyErr_SetString(PyExc_RuntimeError,
156
 			err_msg);
184
 			err_msg);
157
-		return -1;
185
+		goto cturmit_init_error;
158
 	}
186
 	}
159
 
187
 
160
 	PyErr_Restore(errtype, errvalue, errbck);
188
 	PyErr_Restore(errtype, errvalue, errbck);
162
 		TURMIT_AUTOCOMP);
190
 		TURMIT_AUTOCOMP);
163
 	if(self->turmit->err)
191
 	if(self->turmit->err)
164
 	{
192
 	{
193
+		PyErr_Restore(errtype, errvalue, errbck);
165
 		snprintf(err_msg, 128,
194
 		snprintf(err_msg, 128,
166
 			"Unable to compile '%s' err : %s", arg_expr,
195
 			"Unable to compile '%s' err : %s", arg_expr,
167
 			self->turmit->err_str);
196
 			self->turmit->err_str);
168
 		PyErr_SetString(PyExc_ValueError,
197
 		PyErr_SetString(PyExc_ValueError,
169
 			err_msg);
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
 	return 0;
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
 static PyObject *
214
 static PyObject *
238
 				err_msg);
277
 				err_msg);
239
 			Py_RETURN_NONE;
278
 			Py_RETURN_NONE;
240
 		}
279
 		}
280
+                Py_DECREF(arg);
241
 	}
281
 	}
242
-
243
 	res = turmit_exec(self->turmit, exec_args);
282
 	res = turmit_exec(self->turmit, exec_args);
244
 	return PyLong_FromUnsignedLongLong(res);
283
 	return PyLong_FromUnsignedLongLong(res);
245
 }
284
 }

+ 4
- 0
libs/cturmit.h View File

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

Loading…
Cancel
Save