File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,10 +7,11 @@ package gojieba
77*/
88import "C"
99import (
10+ "fmt"
11+ "os"
1012 "runtime"
13+ "sync/atomic"
1114 "unsafe"
12- "os"
13- "fmt"
1415)
1516
1617type TokenizeMode int
@@ -28,11 +29,12 @@ type Word struct {
2829
2930type Jieba struct {
3031 jieba C.Jieba
32+ freed int32
3133}
3234
3335func NewJieba (paths ... string ) * Jieba {
3436 dictpaths := getDictPaths (paths ... )
35-
37+
3638 // check if the dictionary files exist
3739 for _ , path := range dictpaths {
3840 if _ , err := os .Stat (path ); os .IsNotExist (err ) {
@@ -54,14 +56,17 @@ func NewJieba(paths ...string) *Jieba {
5456 ipath ,
5557 spath ,
5658 ),
59+ 0 ,
5760 }
5861 // set finalizer to free the memory when the object is garbage collected
5962 runtime .SetFinalizer (jieba , (* Jieba ).Free )
6063 return jieba
6164}
6265
6366func (x * Jieba ) Free () {
64- C .FreeJieba (x .jieba )
67+ if atomic .CompareAndSwapInt32 (& x .freed , 0 , 1 ) { // only free once
68+ C .FreeJieba (x .jieba )
69+ }
6570}
6671
6772func (x * Jieba ) Cut (s string , hmm bool ) []string {
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package gojieba
33import (
44 "fmt"
55 "reflect"
6+ "runtime"
67 "strings"
78 "testing"
89)
@@ -267,3 +268,10 @@ func BenchmarkExtractor(b *testing.B) {
267268 x .ExtractWithWeight (s , 10 )
268269 }
269270}
271+
272+ func TestTypicalDoubleFree (t * testing.T ) {
273+ x := NewJieba ()
274+ defer x .Free ()
275+
276+ runtime .GC () // call GC to run finalizers
277+ }
You can’t perform that action at this time.
0 commit comments