@@ -107,6 +107,7 @@ typedef struct __TypeContext {
107107 JSINT64 longValue ;
108108
109109 const char * cStr ;
110+ int freeCStr ;
110111 NpyArrContext * npyarr ;
111112 PdBlockContext * pdblock ;
112113 int transpose ;
@@ -162,6 +163,7 @@ static TypeContext *createTypeContext(void) {
162163 pc -> longValue = 0 ;
163164 pc -> doubleValue = 0.0 ;
164165 pc -> cStr = NULL ;
166+ pc -> freeCStr = 0 ;
165167 pc -> npyarr = NULL ;
166168 pc -> pdblock = NULL ;
167169 pc -> rowLabels = NULL ;
@@ -327,13 +329,15 @@ static const char *NpyDateTimeToIsoCallback(JSOBJ Py_UNUSED(unused),
327329 NPY_DATETIMEUNIT base = ((PyObjectEncoder * )tc -> encoder )-> datetimeUnit ;
328330 NPY_DATETIMEUNIT valueUnit = ((PyObjectEncoder * )tc -> encoder )-> valueUnit ;
329331 GET_TC (tc )-> cStr = int64ToIso (GET_TC (tc )-> longValue , valueUnit , base , len );
332+ GET_TC (tc )-> freeCStr = 1 ;
330333 return GET_TC (tc )-> cStr ;
331334}
332335
333336/* JSON callback. returns a char* and mutates the pointer to *len */
334337static const char * NpyTimeDeltaToIsoCallback (JSOBJ Py_UNUSED (unused ),
335338 JSONTypeContext * tc , size_t * len ) {
336339 GET_TC (tc )-> cStr = int64ToIsoDuration (GET_TC (tc )-> longValue , len );
340+ GET_TC (tc )-> freeCStr = 1 ;
337341 return GET_TC (tc )-> cStr ;
338342}
339343
@@ -347,7 +351,9 @@ static const char *PyDateTimeToIsoCallback(JSOBJ obj, JSONTypeContext *tc,
347351 }
348352
349353 NPY_DATETIMEUNIT base = ((PyObjectEncoder * )tc -> encoder )-> datetimeUnit ;
350- return PyDateTimeToIso (obj , base , len );
354+ GET_TC (tc )-> cStr = PyDateTimeToIso (obj , base , len );
355+ GET_TC (tc )-> freeCStr = 1 ;
356+ return GET_TC (tc )-> cStr ;
351357}
352358
353359static const char * PyTimeToJSON (JSOBJ _obj , JSONTypeContext * tc ,
@@ -1880,6 +1886,9 @@ static void Object_endTypeContext(JSOBJ Py_UNUSED(obj), JSONTypeContext *tc) {
18801886 GET_TC (tc )-> rowLabels = NULL ;
18811887 NpyArr_freeLabels (GET_TC (tc )-> columnLabels , GET_TC (tc )-> columnLabelsLen );
18821888 GET_TC (tc )-> columnLabels = NULL ;
1889+ if (GET_TC (tc )-> freeCStr ) {
1890+ PyObject_Free ((void * )GET_TC (tc )-> cStr );
1891+ }
18831892 GET_TC (tc )-> cStr = NULL ;
18841893 PyObject_Free (tc -> prv );
18851894 tc -> prv = NULL ;
0 commit comments