Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mismatched free() / delete / delete [] (from modular/dirichlet.py) #535

Closed
sagetrac-mabshoff mannequin opened this issue Aug 30, 2007 · 2 comments
Closed

Mismatched free() / delete / delete [] (from modular/dirichlet.py) #535

sagetrac-mabshoff mannequin opened this issue Aug 30, 2007 · 2 comments

Comments

@sagetrac-mabshoff
Copy link
Mannequin

sagetrac-mabshoff mannequin commented Aug 30, 2007

From Sage 2.8.3rc3:

==25034== Mismatched free() / delete / delete []
==25034==    at 0x4A05130: operator delete(void*) (vg_replace_malloc.c:244)
==25034==    by 0x923CCB6: del_charstar (in /tmp/Work2/sage-2.8.3.rc3/local/lib/libcsage.so)
==25034==    by 0x17AC7793: __pyx_f_3ntl_string_delete (ntl.c:996)
==25034==    by 0x17AC7F88: __pyx_f_3ntl_9ntl_ZZ_pX___repr__ (ntl.c:6314)
==25034==    by 0x443C61: _PyObject_Str (object.c:406)
==25034==    by 0x443D0A: PyObject_Str (object.c:426)
==25034==    by 0x44EA8F: string_new (stringobject.c:3892)
==25034==    by 0x45A272: type_call (typeobject.c:422)
==25034==    by 0x4156A2: PyObject_Call (abstract.c:1860)
==25034==    by 0x480783: PyEval_EvalFrameEx (ceval.c:3775)
==25034==    by 0x485025: PyEval_EvalFrameEx (ceval.c:3650)
==25034==    by 0x4865EF: PyEval_EvalCodeEx (ceval.c:2831)

Cheers,

Michael

Component: memleak

Issue created by migration from https://trac.sagemath.org/ticket/535

@sagetrac-mabshoff sagetrac-mabshoff mannequin added this to the sage-2.9 milestone Aug 30, 2007
@sagetrac-mabshoff sagetrac-mabshoff mannequin added t: bug labels Aug 30, 2007
@sagetrac-mabshoff sagetrac-mabshoff mannequin self-assigned this Aug 30, 2007
@malb
Copy link
Member

malb commented Aug 30, 2007

comment:2

This is caused because the char arrays in ntl_wrap.cc are created using 'new' and free'd in Cython using 'free'. This has been fixed before (i.e. it is a regression) by replacing the 'new' calls with 'malloc's. malloc is the correct memory allocator here because the string is returned to C land.

@sagetrac-mabshoff sagetrac-mabshoff mannequin modified the milestones: sage-2.9, sage-2.8.4 Sep 1, 2007
@williamstein williamstein modified the milestones: sage-2.8.4, sage-2.9 Sep 7, 2007
@sagetrac-mabshoff sagetrac-mabshoff mannequin modified the milestones: sage-2.9, sage-2.8.4.2 Sep 9, 2007
@williamstein
Copy link
Contributor

comment:6

Unfortunately at least in the code I'm looking at right now the array is freed using delete. Could you
retest. Here's all the relevant code (from ntl_wrap.cc, ntl.pyx and misc.pxi). There's no malloc or free involved:

    def __repr__(self):
        _sig_on
        return string_delete(ZZ_pX_repr(self.x))

char* ZZ_pX_repr(struct ZZ_pX* x)
{
  ostringstream instore;
  instore << (*x);
  int n = strlen(instore.str().data());
  char* buf = new char[n+1];
  strcpy(buf, instore.str().data());
  return buf;
}

cdef object string_delete(char* s):
    """
    Takes a char* allocated using C++ new, and converts it to a Python
    string, then deletes the allocated memory.  Also unsets the signal
    handler, so you *must* call _sig_on right before calling this!
    """
    _sig_off
    t = str(s)
    del_charstar(s)
    return t

void del_charstar(char* a) {
  delete a;
}

@williamstein williamstein modified the milestones: sage-2.8.4.2, sage-2.8.5 Sep 13, 2007
@sagetrac-mabshoff sagetrac-mabshoff mannequin modified the milestones: sage-2.8.5, sage-2.8.4.2 Sep 13, 2007
@sagetrac-mabshoff sagetrac-mabshoff mannequin closed this as completed Sep 13, 2007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants