package main
/*
#include <stdlib.h>
#include <malloc.h>
void mal(char **p) {
int l = 1000000;
*p= (char *)malloc(l);
*(*p+l-2) = 'a';
*(*p+l-1) = 'b';
}
*/
import "C"
import (
"fmt"
"sync"
"time"
"unsafe"
)
func main() {
time.Sleep(time.Second * 10)
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
var p *C.char
C.mal(&p)
defer C.free(unsafe.Pointer(p))
l := 1000000
ss := C.GoStringN(p, C.int(l))
if string(ss[l-2:]) != "ab" {
fmt.Println("not ab")
}
wg.Done()
}()
}
wg.Wait()
fmt.Println("complete!")
time.Sleep(time.Second * 300)
}
C.GoStringN(p, C.int(l))会产生内存泄露,生成的string不会被GC,大家有遇到这个情况吗