Streams causing heap crash using Python C-Extensions

Discussion in 'Python' started by Raj Batra, Sep 17, 2003.

  1. Raj Batra

    Raj Batra Guest

    Hi,

    I've created a dll that you can import into python. The function
    calls an ostringstream class. Calling this function repeatedly in python
    will cause a Microsoft Visual C++ Debug Library error:

    Debug Assertion Failed!
    Program: C:\Program Files\Python22\python.exe
    File: dbgheap.c
    Line: 346

    Expression: _CrtCheckMemory()

    Here are snippets of the code:

    // Method called by python via a PySwitchObject
    static PyObject * pyCircuit(PySwitchObject * self, PyObject * args)
    {
    int circuit;

    if (!PyArg_ParseTuple(args, "i", &circuit))
    {
    // Error handling...
    }

    self->_switch->circuit((U8) circuit);
    Py_INCREF(Py_None);
    return(Py_None);
    }

    // Called by pyCircuit method above. Multiple calls to this method
    // yield a heap assertion.
    void Switch::circuit(U8 c)
    {
    std::eek:stringstream close;

    close << "Some formating...";

    // Use the close method - doesn't really matter.
    }


    The error is always within the constructor or destructor of ostringstream and
    in the file called xmutex.cpp (implements mutex lock for iostreams) which
    calls the malloc/dealloc functions.

    I have tried other streams such as fstream, and get similar behavior.
    Switching to a char buffer[] and sprintf is my current work around the
    ostringstream issue.

    Any thoughts about what is going on?

    I'm using Python2.2.2, with Visual C++ 7 (.NET). The error occurs using
    both pythonwin and the standard command line python.

    Thanks,

    Raj
     
    Raj Batra, Sep 17, 2003
    #1
    1. Advertising

  2. Raj Batra

    Raj Batra Guest

    Just a follow up - It appears that the crash isn't related to
    just C++ i/o streaming. I tried using the fopen/fclose (e.g for logging)
    and again repeated calls from python to the code causes the same
    _CrtCheckMemory() error.

    E.g.

    void void Switch::circuit(U8 c)
    {
    if (!_filename) return;

    FILE *out = fopen(_filename, "a");

    if (!out) return;

    time_t t = time(0);
    fprintf(out,"%s\n %s%s\n", asctime(localtime(&t)), "Header", "Log stuff");

    fclose(out);

    }

    Another person solved the problem by using cout and then in python
    redirected stdout to a file. Then all calls to the cextension
    forced cout to write to a file. I prefer not to go down that road.

    Any thoughts appreciated as to what may be going on.

    Thanks,

    Raj


    (Raj Batra) wrote in message news:<>...
    > Hi,
    >
    > I've created a dll that you can import into python. The function
    > calls an ostringstream class. Calling this function repeatedly in python
    > will cause a Microsoft Visual C++ Debug Library error:
    >
    > Debug Assertion Failed!
    > Program: C:\Program Files\Python22\python.exe
    > File: dbgheap.c
    > Line: 346
    >
    > Expression: _CrtCheckMemory()
    >
    > Here are snippets of the code:
    >
    > // Method called by python via a PySwitchObject
    > static PyObject * pyCircuit(PySwitchObject * self, PyObject * args)
    > {
    > int circuit;
    >
    > if (!PyArg_ParseTuple(args, "i", &circuit))
    > {
    > // Error handling...
    > }
    >
    > self->_switch->circuit((U8) circuit);
    > Py_INCREF(Py_None);
    > return(Py_None);
    > }
    >
    > // Called by pyCircuit method above. Multiple calls to this method
    > // yield a heap assertion.
    > void Switch::circuit(U8 c)
    > {
    > std::eek:stringstream close;
    >
    > close << "Some formating...";
    >
    > // Use the close method - doesn't really matter.
    > }
    >
    >
    > The error is always within the constructor or destructor of ostringstream and
    > in the file called xmutex.cpp (implements mutex lock for iostreams) which
    > calls the malloc/dealloc functions.
    >
    > I have tried other streams such as fstream, and get similar behavior.
    > Switching to a char buffer[] and sprintf is my current work around the
    > ostringstream issue.
    >
    > Any thoughts about what is going on?
    >
    > I'm using Python2.2.2, with Visual C++ 7 (.NET). The error occurs using
    > both pythonwin and the standard command line python.
    >
    > Thanks,
    >
    > Raj
     
    Raj Batra, Sep 18, 2003
    #2
    1. Advertising

  3. (Raj Batra) writes:

    >> I'm using Python2.2.2, with Visual C++ 7 (.NET). The error occurs using
    >> both pythonwin and the standard command line python.


    I don't know whether you can use C++ stream I/O in a Python extension or
    not, but if you use MSVC7 to compile the extension, your Python must
    also be compiled with this one (generally).

    Thomas
     
    Thomas Heller, Sep 18, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad

    ASPX page causing IE to crash

    Brad, May 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,160
  2. laniik
    Replies:
    4
    Views:
    414
    laniik
    Apr 8, 2005
  3. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    741
    GArlington
    Mar 25, 2008
  4. viki
    Replies:
    6
    Views:
    568
    Erik Wikström
    Jun 28, 2008
  5. Raymond Schanks
    Replies:
    0
    Views:
    543
    Raymond Schanks
    Apr 11, 2010
Loading...

Share This Page