当前位置:网站首页>Closure problem C Lua
Closure problem C Lua
2022-06-26 06:32:00 【ThomasQxx】
Closure problem summary ( There was a big guy who asked a few questions from time to time , Now let's sum up the experience )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ToggleTest : MonoBehaviour
{
public Toggle[] toggles;
private void Start() {
for (int i = 0; i < toggles.Length; i++)
{
toggles[i].onValueChanged.AddListener((isOn)=>OnToggleChanged(isOn,i));
}
}
private void OnToggleChanged(bool isOn,int index){
Debug.Log($"index==>{
index}..State===>{
isOn}");
}
}
Here you can guess and choose Toggle What will the print result be ?Toggle There are three radio buttons .
You can see Index Always be 3...
Why would the result be 3 Well ? The concept of closure will be mentioned here : The inner function can refer to the variables of the function contained in its outer layer , Even if the execution of the outer function has terminated . However, the value provided by this variable is not the value at the time of variable creation , But the final value in the range of the parent function . Normally speaking For When the loop is finished i The memory of variables will change with For The cycle ends and life ends . But because anonymous functions refer to i, Lead to i Can't go with For The end of the cycle . So at this point i=3. When we execute a click index=3. Here is the problem , So how to solve it ?
Solution

That's it . Want to figure out the solution , We still have to start with memory . The last one did not int t = i In the code of .i There are several memory addresses ? You can see it i Only one memory address . In the current figure t There are several memory addresses ? There are obviously three , Every time For Loop in will declare a t Memory address of . also t The value of the For The end of the loop is different . Namely 0,1,2. So the problem can be fully understood here .

Lua Closure in
function newCounter()
local i = 0
return function()
i = i + 1
return i
end
end
c1 = newCounter()
print(c1())
print(c1())
The result should be 1,2.i It should have been NewCounter At the end of the call, the memory is released . But because of the internal function reference , So memory continues to exist , First execution completed c1()i = 0+1 = 1; Second execution c1() End i It should be for i = 1+1 =2;
function newCounter()
local i = 0
return function()
i = i + 1
return i
end
end
c1 = newCounter()
print(c1())
c1 = nil
c2 = newCounter()
print(c2())
The result should be 1,1. Because after the first execution c1()i = i + 1 = 0 + 1 = 1; But next c1=nil It frees up memory .c2 = newCounter() perform c2() At this time i by 0. therefore c2() The result is i = i + 1 = 0 + 1 = 1; So the end result is 1,1.
边栏推荐
- EFK升级到ClickHouse的日志存储实战
- Alarm operation and Maintenance Center | build an efficient and accurate alarm collaborative processing system
- vs code 使用 prettier 格式化 js 的时候, 函数定义的名称和括号之间有一个空格, 而 eslit 又不允许这个空格.
- Go learning notes 1.3- data types of variables
- Dpdk - tcp/udp protocol stack server implementation (I)
- Pytorch mixing accuracy principle and how to start this method
- Evolution history of qunar Bi platform construction
- Load balancer does not have available server for client: userService问题解决
- Logstash -- send an alert message to the nail using the throttle filter
- Volatile application scenarios
猜你喜欢
How can an enterprise successfully complete cloud migration?
The sysdig 2022 cloud native security and usage report found that more than 75% of the running containers have serious vulnerabilities

Ppt template crawler case

Message queue - function, performance, operation and maintenance comparison

连接数服务器数据库报:错误号码2003Can‘t connect to MySQL server on ‘服务器地址‘(10061)

架构设计方法

成水最多的容器

低代码实时数仓构建系统的设计与实践

Reasons why MySQL indexes are not effective

Five solutions across domains
随机推荐
Laravel 实现 groupBy 查询分组数量
数据治理工作的几种推进套路
Efk Upgrade to clickhouse log Storage Reality
Five solutions across domains
视图简析
Everything is a vector. The service practice of iqiyi online vector recall project
技术能力的思考和总结
University Information Management System
自顶向下的变成方法
EFK升级到ClickHouse的日志存储实战
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
MySQL 索引底层原理
宝塔服务器搭建及数据库远程连接
Gof23 - abstract factory pattern
GoF23—建造者模式
Research Report on market supply and demand and strategy of natural organic beauty industry in China
Experience the new features of Milvus 2.0 together
Use the fast proxy to build your own proxy pool (mom doesn't have to worry about IP being blocked anymore)
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
Web components series (10) -- realize the basic layout of mycard