Library call failed

Hopefully I can drop that without taking away the critical details ...

I have a test program:

#include <lib.h>

const char * INPUT  = "xyz";

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

      

This test program compiles (gcc 4.1.2) and runs as:

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

      

The library is quite complex and not too smart in some places and, most importantly, not written by me, so don't cry me for the architecture of the functions involved:

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;
}

      

So far so good. But the result of this really puzzled me:

Reached LibProcess().
Created Process.
Before run(xyz)

SEGFAULT

      

I know that the error is (most likely) somewhere else entirely. But how is this even possible?

I would understand if the test died when calling the library the first time. I would understand if the test died when the process was created or when it actually does something in run ().

But how can it die between calling a function and actually reaching that function?

I don't know, especially I don't know how to continue debugging. Help?

Edit: Yes, I checked the process to be non-NULL after createLibProcess (). Sequentially calling two different process member functions without an example also worked great. But the second call to the function causes memory loss due to a buffer overflow and a nullified process.

This invalidates the question. Calls NULL-> run () of course breaking segfaulting.

The question can be closed as "no longer relevant". Thanks anyway!

+2


source to share


2 answers


Is the process a definite value for a non-NULL valid pointer before it is dereferenced for a call?



+2


source


If a:

LibProcess * process = createLibProcess();

      



returns NULL or some invalid process, you will get exactly the behavior you see.

+3


source







All Articles