vlang/v

pointer being freed was not allocate

Closed this issue · 5 comments

Describe the bug

vprod(12545,0x700001eff000) malloc: *** error for object 0x7972: pointer being freed was not allocated
vprod(12545,0x700001eff000) malloc: *** set a breakpoint in malloc_error_break to debug
Terminated by signal  6 (SIGABRT)

Reproduction Steps

v -d veb_liveload watch run .

Expected Behavior

no error

Current Behavior

avey@avey vprod % v -d veb_liveload watch run .
2024-09-30 16:01:08.281: "/Applications/v/v" -d veb_liveload run . | pid:   12533 | reload cycle:     0
2024-09-30 16:01:15.010747 [INFO ] 执行函数:check_config_toml src.config.check_toml.v:26
2024-09-30 16:01:15.011644 [INFO ] 配置文件已存在
2024-09-30 16:01:15.011667 [INFO ] 执行函数:check_config_toml_data src.config.check_toml.v:56
2024-09-30 16:01:15.011682 [INFO ] 开始检测必要配置
2024-09-30 16:01:15.012048 [INFO ] 必要配置检测完毕
2024-09-30 16:01:15.012080 [INFO ] 执行函数:database_mysql src.config.check_toml.v:104
2024-09-30 16:01:15.012102 [INFO ] 开始连接mysql数据库
2024-09-30 16:01:15.923178 [INFO ] mysql数据库连接成功
2024-09-30 16:01:15.923230 [INFO ] 数据库连接关闭
2024-09-30 16:01:15.923466 [INFO ] 执行函数:cr_task_images src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 16:01:15.923499 [INFO ] 执行函数:database_mysql src.config.check_toml.v:104
2024-09-30 16:01:15.923518 [INFO ] 开始连接mysql数据库
2024-09-30 16:01:15.923575 [INFO ] 执行函数:new_app src.api.app_start.v:32
2024-09-30 16:01:15.923779 [INFO ] 执行函数:set_web_port src.config.check_toml.v:87
[veb] Running app on http://localhost:9009/
2024-09-30 16:01:16.675897 [INFO ] mysql数据库连接成功
2024-09-30 16:01:17.188618 [INFO ] 数据库连接关闭
2024-09-30 16:01:17.188750 [INFO ] 执行函数:up_task_images src.internal.mall.pf1688.translateTiming.v:37
2024-09-30 16:01:17.188786 [INFO ] 执行函数:database_mysql src.config.check_toml.v:104
2024-09-30 16:01:17.188812 [INFO ] 开始连接mysql数据库
2024-09-30 16:01:17.876717 [INFO ] mysql数据库连接成功
2024-09-30 16:01:18.332841 [INFO ] 数据库连接关闭
2024-09-30 16:01:28.337899 [INFO ] 执行函数:cr_task_images src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 16:01:28.337957 [INFO ] 执行函数:database_mysql src.config.check_toml.v:104
2024-09-30 16:01:28.337974 [INFO ] 开始连接mysql数据库
2024-09-30 16:01:29.056977 [INFO ] mysql数据库连接成功
2024-09-30 16:01:29.670271 [INFO ] 数据库连接关闭
2024-09-30 16:01:29.670591 [INFO ] 执行函数:up_task_images src.internal.mall.pf1688.translateTiming.v:37
2024-09-30 16:01:29.670656 [INFO ] 执行函数:database_mysql src.config.check_toml.v:104
2024-09-30 16:01:29.670723 [INFO ] 开始连接mysql数据库
2024-09-30 16:01:30.468373 [INFO ] mysql数据库连接成功
2024-09-30 16:01:30.986851 [INFO ] 数据库连接关闭
2024-09-30 16:01:40.990314 [INFO ] 执行函数:cr_task_images src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 16:01:40.990362 [INFO ] 执行函数:database_mysql src.config.check_toml.v:104
2024-09-30 16:01:40.990383 [INFO ] 开始连接mysql数据库
2024-09-30 16:01:42.497844 [INFO ] mysql数据库连接成功
2024-09-30 16:01:43.003639 [INFO ] 数据库连接关闭
2024-09-30 16:01:43.003730 [INFO ] 执行函数:up_task_images src.internal.mall.pf1688.translateTiming.v:37
2024-09-30 16:01:43.003750 [INFO ] 执行函数:database_mysql src.config.check_toml.v:104
2024-09-30 16:01:43.003765 [INFO ] 开始连接mysql数据库
2024-09-30 16:01:43.706639 [INFO ] mysql数据库连接成功
2024-09-30 16:01:44.222202 [INFO ] 数据库连接关闭
2024-09-30 16:01:54.224968 [INFO ] 执行函数:cr_task_images src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 16:01:54.225049 [INFO ] 执行函数:database_mysql src.config.check_toml.v:104
2024-09-30 16:01:54.225083 [INFO ] 开始连接mysql数据库
vprod(12545,0x700001eff000) malloc: *** error for object 0x7972: pointer being freed was not allocated
vprod(12545,0x700001eff000) malloc: *** set a breakpoint in malloc_error_break to debug
Terminated by signal  6 (SIGABRT)

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 04aba8f

Environment details (OS name and version, etc.)

macos

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

What is the code for reproduce such error?

What is the code for reproduce such error?

module config

import os
import log
import toml
import db.mysql

const file_name = 'config.toml'

pub fn check_all() {
	check_config_toml() //检查配置文件是否存在
	check_config_toml_data() //检查配置文件内必要数据是否配置
	//检查mysql数据库连接
	mut conn := database_mysql() or {
		log.fatal('数据库连接检测失败,请检查配置文件: ${config.file_name}')
		return
	}
	defer {
		log.info('数据库连接关闭')
		conn.close()
	}
}

//检查配置文件是否存在
fn check_config_toml() {
	log.info('${@METHOD}  ${@MOD}.${@FILE_LINE}')

	if !os.exists(config.file_name) {
		log.warn('配置文件不存在,生成新配置文件')
		mut f := os.create(config.file_name) or {
			log.fatal('配置文件创建失败')
			return
		}
		log.info('配置文件已创建')

		log.info('初始化配置数据')
		os.write_file(config.file_name, data) or {
			log.error('配置数据初始化失败')
			return
		}
		log.info('配置数据初始化完成')

		log.info('开始检测必要配置')
		check_config_toml_data()
		log.info('必要配置检测完毕')
		defer {
			f.close()
		} // 记得关闭文件句柄
	} else {
		log.info('配置文件已存在')
	}
}

//检查配置文件内必要数据是否配置
fn check_config_toml_data() {
	log.info('${@METHOD}  ${@MOD}.${@FILE_LINE}')

	log.info('开始检测必要配置')
	doc := toml.parse_file(config.file_name) or { return }

	doc.value_opt('web.port') or {
		log.warn('配置数据:web.port 键无效或键没有值,将使用默认监听端口')
	}

	doc.value_opt('mysql.host') or {
		log.fatal('必要配置数据:mysql.host 键无效或键没有值,请检查配置数据')
	}
	doc.value_opt('mysql.port') or {
		log.fatal('必要配置数据:mysql.port 键无效或键没有值,请检查配置数据')
	}
	doc.value_opt('mysql.username') or {
		log.fatal('必要配置数据:mysql.username 键无效或键没有值,请检查配置数据')
	}
	doc.value_opt('mysql.password') or {
		log.fatal('必要配置数据:mysql.password 键无效或键没有值,请检查配置数据')
	}
	doc.value_opt('mysql.dbname') or {
		log.fatal('必要配置数据:mysql.dbname 键无效或键没有值,请检查配置数据')
	}
	doc.value_opt('mysql.ssl') or { log.warn('配置数据:mysql.ssl 键无效或键没有值') }

	log.info('必要配置检测完毕')
}

//设置app默认监听端口
pub fn set_web_port() u32 {
	log.info('${@METHOD}  ${@MOD}.${@FILE_LINE}')

	doc := toml.parse_file(config.file_name) or {
		log.info('config.toml文件不存在,设置默认监听端口:9090')
		return 9009 // 设置默认端口号
	}

	web_port := doc.value('web.port').string().u32()
	if web_port == 0 {
		log.info('web.port端口配置不存在,设置默认监听端口:9090')
		return 9009 // 设置默认端口号
	}
	return web_port
}

//检查mysql数据库连接
pub fn database_mysql() !mysql.DB {
	log.info('${@METHOD}  ${@MOD}.${@FILE_LINE}')

	_log.info('开始连接mysql数据库')_
	doc := toml.parse_file(config.file_name) or {
		log.error('config.toml文件不存在')
		return err
	}
	mut conn := mysql.connect(
		host: doc.value('mysql.host').string()
		port: doc.value('mysql.port').string().u32()
		username: doc.value('mysql.username').string()
		password: doc.value('mysql.password').string()
		dbname: doc.value('mysql.dbname').string()
	) or {
		log.error('mysql数据库连接失败:${doc.value('mysql')}')
		return err
	}
	log.info('mysql数据库连接成功')
	return conn
}


module main

import api { new_app }
import config { check_all }
import internal.mall.pf1688 { cr_task_images, up_task_images }
import log
import time

fn main() {
	check_all() //启动前配置检查

	// 使用协程运行定时任务
	go fn () {
		for {
			cr_task_images() or { log.warn('cr_task_images jump: ${err}') }
			up_task_images() or { log.warn('up_task_images jump: ${err}') }
			time.sleep(10 * time.second)
		}
	}()
    new_app()
}

2024-09-30 23:04:48.786: "/Applications/v/v" -d veb_liveload run . | pid:   20712 | reload cycle:     2
2024-09-30 23:04:55.523155 [INFO ] check_config_toml  src.config.check_toml.v:26
2024-09-30 23:04:55.526197 [INFO ] 配置文件已存在
2024-09-30 23:04:55.526218 [INFO ] check_config_toml_data  src.config.check_toml.v:56
2024-09-30 23:04:55.526233 [INFO ] 开始检测必要配置
2024-09-30 23:04:55.527011 [INFO ] 必要配置检测完毕
2024-09-30 23:04:55.527065 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:04:55.527087 [INFO ] 开始连接mysql数据库
2024-09-30 23:04:56.477341 [INFO ] mysql数据库连接成功
2024-09-30 23:04:56.477376 [INFO ] 数据库连接关闭
2024-09-30 23:04:56.477519 [INFO ] cr_task_images  src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 23:04:56.477546 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:04:56.477566 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:06.179: "/Applications/v/v" -d veb_liveload run . | pid:   20801 | reload cycle:     3
2024-09-30 23:05:11.906786 [INFO ] check_config_toml  src.config.check_toml.v:26
2024-09-30 23:05:11.908198 [INFO ] 配置文件已存在
2024-09-30 23:05:11.908220 [INFO ] check_config_toml_data  src.config.check_toml.v:56
2024-09-30 23:05:11.908235 [INFO ] 开始检测必要配置
2024-09-30 23:05:11.908524 [INFO ] 必要配置检测完毕
2024-09-30 23:05:11.908552 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:11.908572 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:13.300964 [INFO ] mysql数据库连接成功
2024-09-30 23:05:13.301020 [INFO ] 数据库连接关闭
2024-09-30 23:05:13.301268 [INFO ] cr_task_images  src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 23:05:13.301327 [INFO ] new_app  src.api.app_start.v:32
2024-09-30 23:05:13.301394 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:13.301507 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:13.301576 [INFO ] set_web_port  src.config.check_toml.v:87
[veb] Running app on http://localhost:9009/
2024-09-30 23:05:14.305519 [INFO ] mysql数据库连接成功
2024-09-30 23:05:15.834924 [INFO ] 数据库连接关闭
2024-09-30 23:05:15.835056 [INFO ] up_task_images  src.internal.mall.pf1688.translateTiming.v:37
2024-09-30 23:05:15.835089 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:15.835112 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:16.538078 [INFO ] mysql数据库连接成功
2024-09-30 23:05:18.226755 [INFO ] 数据库连接关闭
2024-09-30 23:05:28.229573 [INFO ] cr_task_images  src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 23:05:28.229628 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:28.229644 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:29.488397 [INFO ] mysql数据库连接成功
2024-09-30 23:05:30.025184 [INFO ] 数据库连接关闭
2024-09-30 23:05:30.025313 [INFO ] up_task_images  src.internal.mall.pf1688.translateTiming.v:37
2024-09-30 23:05:30.025340 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:30.025368 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:33.282551 [INFO ] mysql数据库连接成功
2024-09-30 23:05:33.938869 [INFO ] 数据库连接关闭
2024-09-30 23:05:43.943737 [INFO ] cr_task_images  src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 23:05:43.943799 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:43.943830 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:45.626435 [INFO ] mysql数据库连接成功
2024-09-30 23:05:46.080269 [INFO ] 数据库连接关闭
2024-09-30 23:05:46.080521 [INFO ] up_task_images  src.internal.mall.pf1688.translateTiming.v:37
2024-09-30 23:05:46.080558 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:46.080579 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:46.900796 [INFO ] mysql数据库连接成功
2024-09-30 23:05:48.160942 [INFO ] 数据库连接关闭
2024-09-30 23:05:58.163261 [INFO ] cr_task_images  src.internal.mall.pf1688.translateTiming.v:13
2024-09-30 23:05:58.163315 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:58.163338 [INFO ] 开始连接mysql数据库
2024-09-30 23:05:58.983686 [INFO ] mysql数据库连接成功
2024-09-30 23:05:59.426859 [INFO ] 数据库连接关闭
2024-09-30 23:05:59.427009 [INFO ] up_task_images  src.internal.mall.pf1688.translateTiming.v:37
2024-09-30 23:05:59.427054 [INFO ] database_mysql  src.config.check_toml.v:104
2024-09-30 23:05:59.427084 [INFO ] 开始连接mysql数据库
signal 11: segmentation fault
0   libsystem_platform.dylib            0x00007ff81b227c1d _sigtramp + 29
1   ???                                 0x0000000000000000 0x0 + 0
2   Network                             0x00007ff8220e3f3b nw_path_snapshot_agents + 587
3   Network                             0x00007ff8220e32f9 nw_path_snapshot_path + 249
4   Network                             0x00007ff8220e2ac9 nw_path_evaluator_evaluate + 3241
5   Network                             0x00007ff8220e170a nw_path_create_evaluator_for_endpoint + 58
6   Network                             0x00007ff821f3472c nw_nat64_v4_address_requires_synthesis + 220
7   libsystem_info.dylib                0x00007ff81b233a15 si_addrinfo + 2605
8   libsystem_info.dylib                0x00007ff81b232f4e getaddrinfo + 176
9   libmariadb.3.dylib                  0x000000010b7a4a4c pvio_socket_connect + 604
10  libmariadb.3.dylib                  0x000000010b7ac7fa mthd_my_real_connect + 1600
11  libmariadb.3.dylib                  0x000000010b7ac18c mysql_real_connect + 718
12  vprod                               0x000000010b4c99dc db__mysql__connect + 108
13  vprod                               0x000000010b52e8f2 src__config__database_mysql + 1042
14  vprod                               0x000000010b44504a src__internal__mall__pf1688__up_task_images + 138
15  vprod                               0x000000010b443a94 anon_fn_143c1662f75e5a6c__246 + 196
16  vprod                               0x000000010b433464 anon_fn_143c1662f75e5a6c__246_thread_wrapper + 20
17  vprod                               0x000000010b5c2218 GC_start_routine + 100
18  libsystem_pthread.dylib             0x00007ff81b1fd259 _pthread_start + 125
19  libsystem_pthread.dylib             0x00007ff81b1f8c7b thread_start + 15

What is fn up_task_images definition?

already been solved