0

I am overloading "malloc" by pre-loading a library. In this custom "malloc", i am using environment variable to distinguish my program to use my custom "malloc" from the general "malloc".

The problem is that, after several "mallocs" the program gets stuck inside getenv() call. I am not able to figure out why the program is getting stuck inside it.

The code is the following:

void* PerfTrackMallocInterposition::Malloc(size_t size) {
    // Malloc with statistics
    pthread_mutex_lock(&fgPTMutex);


    char *checkCDBEnd=NULL;
    static const char* CDBEndEnv = "checkCDBEnd";
    checkCDBEnd = getenv(CDBEndEnv);   //program gets stuck here

    if(checkCDBEnd!=NULL)
    {
        if(checkCDBEnd[0]=='1')
        {
            if(size>1024)
            {
                void *result = Alloc(size);   //Call to custom malloc
                pthread_mutex_unlock(&fgPTMutex);
                return result;
            }
        }
    }


    void* result = (*fPMalloc)(size);    //call to normal malloc
    pthread_mutex_unlock(&fgPTMutex);


    return result;
}

I also get a bus error at same position while using this library with vim editor.

Please help me.

Thank You

pratick
  • 303
  • 1
  • 3
  • 9
  • Load your program into a debugger like dbx/gdb and `Ctrl-C` when it is stuck. And then check where the control is stuck using `where` or some similar command – Pavan Manjunath Apr 16 '12 at 10:57

1 Answers1

0

Are you sure the program gets stuck on the getenv() call? I would be more suspicious of the mutexes: pthread_mutex_lock(&fgPTMutex); will block if another thread holds the mutex

Attila
  • 28,265
  • 3
  • 46
  • 55
  • Initially I suspect that too but I tried to use printf everywhere and it prints the line before getenv() call and gets stuck after that. I tried the same test several times. And also this mutex is used only here and no where else. – pratick Apr 16 '12 at 10:50
  • I could find only one [reference](http://www.unix.com/programming/1816-getenv-3c.html) on getenv blocking, but this might be your issue (altough the mutexes supposed to prevent this -- maybe another part of the program uses getenvb/setenv as well?) – Attila Apr 16 '12 at 11:05
  • A little on the side: Is it possible that `Alloc(size);` throws an exception? If yes, you should put `pthread_mutex_unlock` in a `catch()` block – Attila Apr 16 '12 at 11:09
  • No Alloc(size) cannot throws an exception as all the possible exception is handled inside it. – pratick Apr 16 '12 at 12:07