Hi All,

I have built an application on C++. It is a multi threaded application. My application spawns 8 threads when it come up. Each thread opens one file and maps it using mmap.

It is a client-server application. Each thread picks up a message from the queue and processes it and finally using mmap stores it in a file.

Writing to the file and moving the file to another folder is fine. After running for 4 hours, application crashes (Segmentation Fault). When I debugged the core I came to know that SIGSEGV has been caught.

Here are some of the highlights of my codes snippets...

#define [B]NORMAL_CDRFILESIZE[/B] 1000000

if((m_hFile = fopen( m_currentFileName, "a+" ))==NULL)
    {
        cerr << "File could not be opened" << endl;
        return false;
    }

int fd = fileno(m_hFile);

if(ftruncate(fd,NORMAL_CDRFILESIZE) != 0)
    {
        perror("trucate error");
    }

mvmem =(char*) mmap(0, NORMAL_CDRFILESIZE, PROT_READ | PROT_WRITE,  MAP_SHARED, fd, 0); // mvmem is declared as char * mvmem

fmem = mvmem;

memcpy(fmem,pMsg,strlen(pMsg));  //pMsg is the buffer containing data to be written to the file

The above code snippet has been called by each thread in the application. I am using mutex lock and unlock when each thread executes the above code.

When I tried to debug the core file using dbx, it is at memcpy, the memory getting corrupted.

I have given a highlight of the code but actual code contains lots other computations.

Please let me know the cause for Segmentation Fault.

Thanks you very much in advance. Any clue will be a great help.

does your program ever call munmap() to release the memory back to the os? If not then maybe your program is just simply running out of memory.

Also about ftruncate() -- maybe you already know this.

If it <the file length> was previously shorter than length, it is unspecified whether the file is changed or its size increased

does your program ever call munmap() to release the memory back to the os? If not then maybe your program is just simply running out of memory.

Also about ftruncate() -- maybe you already know this.

Thank you very much for your kind reply. Sorry for late reply to your response to the query. I was not accessible to internet. The problem still persists. Looks like some other piece of code causing this problem.

Yes, I am calling munmap() and it is released properly. Suspecting strlen function causing the problem.

We are forming a string and it is not being null terminated. We are just concatenating "\n" to it and not "\0" and this string has been passed to strlen() function. Is this might be the cause for application crash?

you're right. The problem is probably strlen().

The length of a C string is determined by the terminating null-character: A C string is as long as the amount of characters between the beginning of the string and the terminating null character.

Not having a \0 to terminate your string will result in invalid output from strlen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.