crash when calling library
<p>Let's hope I can dumb this down without leaving out crucial details...</p>

<p>I've got a test program:</p>

<pre><code>#include &lt;lib.h&gt;

const char * INPUT  = "xyz";

int main()
{
    initializeLib();
    LibProcess * process = createLibProcess();
    fprintf( stderr, "Before run(%s)\n", INPUT );
    process-&gt;run(INPUT);
    fprintf( stderr, "After run(%s)\n", INPUT );
    return 0;
}
</code></pre>

<p>This test program I compile (gcc 4.1.2) and run as:</p>

<pre><code>g++ -g -o test test.c -L /path/to/lib -I /path/to/include -lnameoflib
export LD_LIBRARY_PATH=/path/to/lib
./test
</code></pre>

<p>The library is rather complex, and not-too-smart in some places, and most importantly not-written-by-me, so don't flame me for the architecture of the functions involved:</p>

<pre><code>class ProcessBase
{
    public:
        virtual int run( const char* buffer = NULL ) = 0;
}

class LibProcess : ProcessBase
{
    public:
        LibProcess()
        {
            fprintf( "Reached LibProcess().\n" );
        }

        int  run( const char* buffer = NULL )
        {
            fprintf( stderr, "Reached run().\n" );
        }
};            

void initializeLib()
{
    // Preparing some data
}

ProcessBase * createLibProcess()
{
    ProcessBase * process = new LibProcess();
    fprintf( stderr, "Created Process.\n" );
    return (ProcessBase *) process;
}
</code></pre>

<p>So far, so good. But the output of it really had me baffled:</p>

<pre><code>Reached LibProcess().
Created Process.
Before run(xyz)

SEGFAULT
</code></pre>

<p>I know the error is (most likely) somewhere completely different. But <em>how is this at all possible?</em></p>

<p>I would understand if the test died on calling the library the first time. I would understand if the test died when creating the process, or when it actually <em>does</em> something in run().</p>

<p><em>But how can it die <b>between the calling of the function and actually reaching that function?</b></em></p>

<p>I'm clueless, especially I don't know how to continue debugging this. Help?</p>

<p><b>Edit:</b> Yes, I checked process to have a non-NULL value after createLibProcess(). A subsequent call to two different member functions of process, left out of the example, also worked beautifully. But the second member function call trashed memory due to a buffer overflow, and zeroed process. </p>

<p>This makes the question invalid. <i>Of course</i> is calling NULL->run() a segfaulting offense.</p>

<p>Question can be closed as "no longer relevant". Thanks anyway!</p>
