fix memory free

pull/1257/head
Alexander Medvednikov 2019-07-21 15:05:47 +02:00
parent e2364f6285
commit 5d188130e5
4 changed files with 13 additions and 10 deletions

View File

@ -18,6 +18,7 @@ mut:
args []Var // function args args []Var // function args
attr string // [json] etc attr string // [json] etc
is_mut bool is_mut bool
is_alloc bool
ptr bool ptr bool
ref bool ref bool
parent_fn string // Variables can only be defined in functions parent_fn string // Variables can only be defined in functions
@ -27,7 +28,6 @@ mut:
is_global bool // __global (translated from C only) is_global bool // __global (translated from C only)
is_used bool is_used bool
scope_level int scope_level int
is_alloc bool
} }
struct Parser { struct Parser {
@ -71,6 +71,7 @@ mut:
v_script bool // "V bash", import all os functions into global space 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 var_decl_name string // To allow declaring the variable so that it can be used in the struct initialization
building_v bool building_v bool
is_alloc bool // Whether current expression resulted in an allocation
} }
const ( const (
@ -117,10 +118,12 @@ fn (p mut Parser) next() {
} }
fn (p &Parser) log(s string) { fn (p &Parser) log(s string) {
/*
if !p.pref.is_verbose { if !p.pref.is_verbose {
return return
} }
println(s) println(s)
*/
} }
fn (p mut Parser) parse() { fn (p mut Parser) parse() {
@ -1017,6 +1020,7 @@ fn (p mut Parser) close_scope() {
p.genln('free($v.name); // close_scope free') p.genln('free($v.name); // close_scope free')
} }
} }
} }
p.cur_fn.var_idx = i + 1 p.cur_fn.var_idx = i + 1
// println('close_scope new var_idx=$f.var_idx\n') // 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() { fn (p mut Parser) var_decl() {
p.is_alloc = false
is_mut := p.tok == .key_mut || p.prev_tok == .key_for is_mut := p.tok == .key_mut || p.prev_tok == .key_for
is_static := p.tok == .key_static is_static := p.tok == .key_static
if p.tok == .key_mut { 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 // Generate expression to tmp because we need its type first
// [TYP .name =] bool_expression() // [TYP .name =] bool_expression()
pos := p.cgen.add_placeholder() pos := p.cgen.add_placeholder()
mut typ := p.bool_expression() mut typ := p.bool_expression()
// Option check ? or { // Option check ? or {
or_else := p.tok == .key_orelse or_else := p.tok == .key_orelse
@ -1252,7 +1256,7 @@ fn (p mut Parser) var_decl() {
name: name name: name
typ: typ typ: typ
is_mut: is_mut is_mut: is_mut
is_alloc: typ.starts_with('array_') is_alloc: p.is_alloc
}) })
mut cgen_typ := typ mut cgen_typ := typ
if !or_else { if !or_else {
@ -2424,6 +2428,7 @@ fn (p mut Parser) map_init() string {
// [1,2,3] // [1,2,3]
fn (p mut Parser) array_init() string { fn (p mut Parser) array_init() string {
p.is_alloc = true
p.check(.lsbr) p.check(.lsbr)
is_integer := p.tok == .integer is_integer := p.tok == .integer
lit := p.lit lit := p.lit

View File

@ -176,6 +176,7 @@ fn (g mut Game) move_tetro() {
x := block.x + g.pos_x x := block.x + g.pos_x
// Reached the bottom of the screen or another block? // Reached the bottom of the screen or another block?
// TODO: if g.field[y][x] != 0 // TODO: if g.field[y][x] != 0
//if g.field[y][x] != 0 {
row := g.field[y] row := g.field[y]
if row[x] != 0 { if row[x] != 0 {
// The new tetro has no space to drop => end of the game // The new tetro has no space to drop => end of the game

View File

@ -5,7 +5,6 @@
module builtin module builtin
struct array { struct array {
is_slice bool
pub: pub:
// Using a void pointer allows to implement arrays without generics and without generating // Using a void pointer allows to implement arrays without generics and without generating
// extra code for every type. // 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 data: s.data + start * s.element_size
len: l len: l
cap: l cap: l
is_slice: true //is_slice: true
} }
return res return res
} }
@ -220,9 +219,9 @@ pub fn (a []int) str() string {
//pub fn (a []int) free() { //pub fn (a []int) free() {
pub fn (a array) free() { pub fn (a array) free() {
if a.is_slice { //if a.is_slice {
return //return
} //}
C.free(a.data) C.free(a.data)
} }

View File

@ -20,10 +20,8 @@ import const (
) )
#flag -I @VROOT/thirdparty/glad #flag -I @VROOT/thirdparty/glad
#include "glad.h" #include "glad.h"
#flag @VROOT/thirdparty/glad/glad.o #flag @VROOT/thirdparty/glad/glad.o
//#include "glad.c"
pub fn init_glad() { pub fn init_glad() {
ok := C.gladLoadGL() ok := C.gladLoadGL()