Hi Ian, syslogparser.go =============
func (obj SyslogParser) LoadPatternDB(opts Syslog, workerId int) { patterndbpath := C.CString(opts.Patterndb) . <<<<<<<<<<<<<<< . STEP 1 defer C.free(unsafe.Pointer(patterndbpath)) . <<<<<<<<<<<<<<<< STEP 2 InitStruct := (*C.Accumulatedparams)(C.calloc(1, C.sizeof_struct_Accumulatedparams)) InitStruct.callback = (C.key_value_cb)(C.callback) InitStruct.data = C.int(workerId) C.load_pattern_db(patterndbpath, (*C.Accumulatedparams)(unsafe.Pointer(InitStruct)), C.int(workerId)) } Since I am running this program in multiple go routines, when we do above STEP 1 : 1)Will the C memory allocate different addresses for 'patterndbpath' in both the routines ...Am I right ? 2)You said 'It may be calling free twice on the same pointer', but I am assigning a different pointer in STEP 1 everytime for each routine...Am I right ? 3)Is it possible that one Go routine has already freed up the address that the other Go routine is trying to free and that is causing this 'fatal error: bin/main': double free or corruption (fasttop)' Is this because of the race condition between two routines that this error is seen ? 4)If you show us a small, complete, self-contained example, that does not call any other code, then we may be able to see the problem. >> As you have mentioned problem is happening when we call 'C.free'.Do you mean by not calling internal syslog-ng methods and just calling a normal C program with multiple go routines ? Please correct me here if I am wrong Thanks, Nitish On Fri, Jul 12, 2019 at 11:00 AM Ian Lance Taylor <i...@golang.org> wrote: > On Thu, Jul 11, 2019 at 8:45 PM Nitish Saboo <nitish.sabo...@gmail.com> > wrote: > > > > I have the following function 'LoadPatternDB' where I am allocating > memory for a C struct.My C struct contains the callback function. > > I have the function 'LoadPatternDB' that is running in multiple go > routines.I have written a C wrapper that internally uses syslog-ng methods. > > While running the method 'LoadPatternDB' in syslogparser.go with > multiple go routines, I get the following error. > > > > *** Error in `fatal error: bin/main': double free or corruption > (fasttop)unexpected signal during runtime execution: 0x00007fd2b8588060 *** > > > > My queries are: > > > > 1)When do we get this error ? > > That error is from the C memory allocator. It does not come from Go. > It means that you called a C function such as malloc, calloc, or free, > and the call detected memory corruption. It may be calling free twice > on the same pointer, or it may be using a dangling pointer, or it may > be some other memory corruption. We don't know. > > > 2)Is it because of not deallocating the struct memory, I am getting this > error ?But at the same time I am allocating separate memories for each of > the go routines. > > No. > > > 3)Is it because of the memory crunch on the system that can lead to this > error ? > > Unlikely. It is conceivable that malloc or calloc is returning NULL, > and you are not checking for NULL, and that is leading to memory > corruption. But that is unlikely to happen, and it is unlikely to > cause this kind of error. > > > 4)The issues is seen with only multiple go routines.Single iteration of > the code is working fine. > > That sounds like a good place to start looking. > > > 5)Can I not call Go code and C code using multiple threads ? > > You can call C code from multiple goroutines, but of course the C code > has to be prepared to handle it. It's no different from a C program > that calls the C function from multiple threads. > > If you show us a small, complete, self-contained example, that does > not call any other code, then we may be able to see the problem. > > Ian > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CALjMrq5Xu0easdHnxExK32WV9h4Z5gu57G_oZrO6FqhS851PaQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.