From 5d188130e5598de6ff03feb90d5e6560163a983f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 21 Jul 2019 15:05:47 +0200 Subject: [PATCH] fix memory free --- compiler/parser.v | 11 ++++++++--- examples/tetris/tetris.v | 1 + vlib/builtin/array.v | 9 ++++----- vlib/gl/gl.v | 2 -- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/compiler/parser.v b/compiler/parser.v index 51d076ee00..3a05e86cd7 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -18,6 +18,7 @@ mut: args []Var // function args attr string // [json] etc is_mut bool + is_alloc bool ptr bool ref bool parent_fn string // Variables can only be defined in functions @@ -27,7 +28,6 @@ mut: is_global bool // __global (translated from C only) is_used bool scope_level int - is_alloc bool } struct Parser { @@ -71,6 +71,7 @@ mut: v_script bool // "V bash", import all os functions into global space var_decl_name string // To allow declaring the variable so that it can be used in the struct initialization building_v bool + is_alloc bool // Whether current expression resulted in an allocation } const ( @@ -117,10 +118,12 @@ fn (p mut Parser) next() { } fn (p &Parser) log(s string) { +/* if !p.pref.is_verbose { return } println(s) +*/ } fn (p mut Parser) parse() { @@ -1017,6 +1020,7 @@ fn (p mut Parser) close_scope() { p.genln('free($v.name); // close_scope free') } } + } p.cur_fn.var_idx = i + 1 // println('close_scope new var_idx=$f.var_idx\n') @@ -1193,6 +1197,7 @@ fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) { } fn (p mut Parser) var_decl() { + p.is_alloc = false is_mut := p.tok == .key_mut || p.prev_tok == .key_for is_static := p.tok == .key_static if p.tok == .key_mut { @@ -1219,7 +1224,6 @@ fn (p mut Parser) var_decl() { // Generate expression to tmp because we need its type first // [TYP .name =] bool_expression() pos := p.cgen.add_placeholder() - mut typ := p.bool_expression() // Option check ? or { or_else := p.tok == .key_orelse @@ -1252,7 +1256,7 @@ fn (p mut Parser) var_decl() { name: name typ: typ is_mut: is_mut - is_alloc: typ.starts_with('array_') + is_alloc: p.is_alloc }) mut cgen_typ := typ if !or_else { @@ -2424,6 +2428,7 @@ fn (p mut Parser) map_init() string { // [1,2,3] fn (p mut Parser) array_init() string { + p.is_alloc = true p.check(.lsbr) is_integer := p.tok == .integer lit := p.lit diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 353c50bf25..735eab6dd2 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -176,6 +176,7 @@ fn (g mut Game) move_tetro() { x := block.x + g.pos_x // Reached the bottom of the screen or another block? // TODO: if g.field[y][x] != 0 + //if g.field[y][x] != 0 { row := g.field[y] if row[x] != 0 { // The new tetro has no space to drop => end of the game diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 5de3a0d2c3..5fab9eef0c 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -5,7 +5,6 @@ module builtin struct array { - is_slice bool pub: // Using a void pointer allows to implement arrays without generics and without generating // extra code for every type. @@ -146,7 +145,7 @@ pub fn (s array) slice(start, _end int) array { data: s.data + start * s.element_size len: l cap: l - is_slice: true + //is_slice: true } return res } @@ -220,9 +219,9 @@ pub fn (a []int) str() string { //pub fn (a []int) free() { pub fn (a array) free() { - if a.is_slice { - return - } + //if a.is_slice { + //return + //} C.free(a.data) } diff --git a/vlib/gl/gl.v b/vlib/gl/gl.v index f5d109d8bc..7dec8432d1 100644 --- a/vlib/gl/gl.v +++ b/vlib/gl/gl.v @@ -20,10 +20,8 @@ import const ( ) #flag -I @VROOT/thirdparty/glad - #include "glad.h" #flag @VROOT/thirdparty/glad/glad.o -//#include "glad.c" pub fn init_glad() { ok := C.gladLoadGL()