On Fri, Mar 6, 2020 at 4:25 AM Nitish Saboo <nitish.sabo...@gmail.com> wrote:
>
> So what did I do :
>
> main.go
> ========
>
> var InitStruct *C.struct_Foo;
>
> func InitializeEngine(pattern string, path string) {
> pattern_db := C.CString(pattern)
> module_path := C.CString(path)
> if InitStruct != nil{
> C.free(unsafe.Pointer(InitStruct))
> }
> InitStruct := (*C.Foo)(C.calloc(1, C.sizeof_struct_Foo))
> InitStruct.cb = (C.key_value_cb)(C.callOnMeGo_cgo)
> InitStruct.data = C.int(5)
> fmt.Println(InitStruct)  <<<<<<<<<<<<<<<<<<<<'&{0x4b7cb0 5 [0 0 0 0]}' got 
> printed
> fmt.Printf("%p\n", InitStruct) 
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'0x1905630' got printed
> C.initialize_engine(pattern_db, module_path, InitStruct)
>
> }
>
> func ReloadPatternDB(patterndb string) {
>
> if InitStruct != nil{
> fmt.Println(InitStruct) <<<<<<<<<<<<<<<'&{0x4b7cb0 5 [0 0 0 0]}' got printed
> fmt.Printf("%p\n", InitStruct) <<<<<<<<<<<<<'0x1905630' got printed
> C.free(unsafe.Pointer(InitStruct))
> fmt.Printf("%p\n", InitStruct). <<<<<<<<<<<'0x1905630' got printed ...Older 
> memory address getting printed even after freeing the memory [a]
> fmt.Println(InitStruct). <<<<<<<<<<<<<<'&{0x863b470 5 [0 0 0 0]}' got 
> printed. Why is struct fields getting printed? [b]
> }
> path := C.CString(patterndb)
> InitStruct = (*C.Foo)(C.calloc(1, C.sizeof_struct_Foo))
> InitStruct.cb = (C.key_value_cb)(C.callOnMeGo_cgo)
> InitStruct.data = C.int(5)
> fmt.Println(InitStruct)    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
> '&{0x4b7cb0 5 [0 0 0 0]}' got printed
> C.reload_pattern_db(path, InitStruct) <<<<<<<<<. '0x9031d40' got printed
>
> }
>
>
> InitStruct is a global variable.I am calling C functions 'initialize_engine' 
> and 'reload_pattern_db' respectively.
>
> 1) Even after freeing the memory in 'reload_pattern_db', the older memory 
> address is getting printed.Why? [a]

Here you are using calls to C.calloc and C.free, so you are using C
pointers and C memory allocation.  That is fine.  But in C, calling
C.free doesn't somehow zero out the pointer.  It leaves the pointer
unchanged, and it becomes a dangling pointer that is unsafe to use.
Basically, if you use C calls, you get C behavior.  That is true even
when calling the C functions from Go.

> 2) Do I have to explicitly point 'InitStruct=nil' after freeing the memory.

Yes, that will remove the dangling pointer.

> 3) Why is 'fmt.Println(InitStruct)' printing the struct fields even after 
> freeing the memory ? [b]

Because that's how C works.  C is not a memory safe language, and it
doesn't become memory safe just because you make the calls from Go.

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/CAOyqgcU0AhwS_J7%2BQD8Bhh6GxF4MY%3DLzxvkxA_%2BMyg9Cm5AEWA%40mail.gmail.com.

Reply via email to