From 5e4182caee768221dd88fac94e1ab6ec754f2fa8 Mon Sep 17 00:00:00 2001 From: Peter Kleiweg Date: Fri, 30 Aug 2019 15:09:46 +0200 Subject: [PATCH] close projections when closing context --- v5/v5.go | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/v5/v5.go b/v5/v5.go index e48d87a..e0e5c8b 100644 --- a/v5/v5.go +++ b/v5/v5.go @@ -14,13 +14,16 @@ import ( ) type Context struct { - pj_context *C.PJ_CONTEXT - opened bool + pj_context *C.PJ_CONTEXT + opened bool + counter uint64 + projections map[uint64]*Proj } type Proj struct { pj *C.PJ context *Context + index uint64 opened bool } @@ -35,8 +38,10 @@ var ( func NewContext() *Context { ctx := Context{ - pj_context: C.proj_context_create(), - opened: true, + pj_context: C.proj_context_create(), + counter: 0, + projections: make(map[uint64]*Proj), + opened: true, } runtime.SetFinalizer(&ctx, (*Context).Close) return &ctx @@ -44,10 +49,23 @@ func NewContext() *Context { func (ctx *Context) Close() { if ctx.opened { + indexen := make([]uint64, 0, len(ctx.projections)) + for i := range ctx.projections { + indexen = append(indexen, i) + } + for _, i := range indexen { + p := ctx.projections[i] + if p.opened { + C.proj_destroy(p.pj) + p.context = nil + p.opened = false + } + delete(ctx.projections, i) + } + C.proj_context_destroy(ctx.pj_context) ctx.pj_context = nil ctx.opened = false - // TODO: destroy projections } } @@ -68,10 +86,11 @@ func (ctx *Context) Create(definition string) (*Proj, error) { p := Proj{ opened: true, context: ctx, + index: ctx.counter, pj: pj, } - - // TODO: projection toevoegen aan context + ctx.projections[ctx.counter] = &p + ctx.counter++ runtime.SetFinalizer(&p, (*Proj).Close) return &p, nil @@ -80,9 +99,11 @@ func (ctx *Context) Create(definition string) (*Proj, error) { func (p *Proj) Close() { if p.opened { C.proj_destroy(p.pj) + if p.context.opened { + delete(p.context.projections, p.index) + } p.context = nil p.opened = false - // todo: projection verwijderen uit context } }