Help with memory leak

@M.Y. Developers but global variable also cause memory leak right.

and just a few questions I got to ask

the way the money was adding up in the game do I change it or keep it the same?

For telling the person they had enough money to buy something how would I do it this is how I had it with ice

local function change () if bank:retrieve( "total" ) \> 10 then amount = 1 bank:increment( "total", amount ) saveFile("twinb.txt", 2) elseif bank:retrieve( "total" ) \< -1 then print("not") end end box:addEventListener("tap", change)

How would it look in json?

Also how would I display the money again in another scene
using text?

[import]uid: 17058 topic_id: 22119 reply_id: 88257[/import]

@M.Y. Developers Ok I answered one of your question about adding but there is another one I have how do I save my number here

local function change3 () gameState.money = gameState.money + 1 bankText2.text = "".. gameState.money end box3:addEventListener("tap", change3)

As you see I have the gameState money adding every time upon a click. Now every time I exit the money returns to 0 instead of were it left off. How do I make it save so when I exit, enter application, and change scene the money will still have the same number without resetting to 0. [import]uid: 17058 topic_id: 22119 reply_id: 88260[/import]

Hello Sebitttas,

You are on the right track. Everything should get saved upon application exit. Are you perhaps setting gameState.money to zero somewhere else in your application? Where is this change3() function? Can we see some more code, perhaps another zip of your project.

Thanks,
M.Y. Developers [import]uid: 55057 topic_id: 22119 reply_id: 88261[/import]

M.Y.developers this is how it looks

[code]module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

gameState = require(“GameState”)

—require( “ice” )
–[[
local bank = ice:loadBox( “bank” )
local banks = bank:retrieve(“total”)
local amount = 0
–]]

gameState.money = 0
local bankText2 = display.newText("", 160, 30,“Arial”,20)
bankText2.text = “”… gameState.money
localGroup:insert(bankText2)

local box = display.newRect(160, 240, 30, 30)
localGroup:insert(box)

local box2 = display.newRect(10, 10, 30, 30)
localGroup:insert(box2)

local box3 = display.newRect(250, 160, 30, 30)
localGroup:insert(box3)

local bankText = display.newText("", 160, 30,“Arial”,20)
localGroup:insert(bankText)

local highText = display.newText("", 0,0,“Arial”,22)
highText.x = display.contentWidth/2
highText.y = 100
localGroup:insert(highText)

local function change ()
if bank:retrieve( “total” ) > 10 then
gameState.money = gameState.money + 1
bankText2.text = “”… gameState.money
saveFile(“twinb.txt”, 2)
elseif bank:retrieve( “total” ) < -1 then
print(“not”)
end
end
box:addEventListener(“tap”, change)

local function change3 ()
gameState.money = gameState.money + 1
bankText2.text = “”… gameState.money
end
box3:addEventListener(“tap”, change3)
–[[
function endGame( )
bank:save()
highText.text = “$” … bank:retrieve( “total” )
end

Runtime:addEventListener(“enterFrame”, endGame )
–]]

local function change2 ()
director:changeScene(“menu”)
end
box2:addEventListener(“touch”, change2)
return localGroup
end [/code]

also another thing doesn’t global variable cause memory leak?, and how would It look in coding for me wanting to save money? [import]uid: 17058 topic_id: 22119 reply_id: 88264[/import]

Sebitttas,
The reason it keeps getting reset to zero is because you inadvertently set it to 0 every time the application starts. What you intend to do is set it only if it is the first time starting the application (ie when the user installs.) You can do it like this:
replace

 gameState = require("GameState")  
  
 ---require( "ice" )  
--[[   
local bank = ice:loadBox( "bank" )  
local banks = bank:retrieve("total")  
local amount = 0  
--]]  
   
gameState.money = 0  

with this

gameState = require("GameState")  
if(gameState.money == nil) then  
 gameState.money = 0  
 --other init code can go here  
end  

What the code above does is first checks to see if the .money key is set, if not then it must be the first time we start the application so we want to initialize all of our variables. You can add other stuff like highscores, avatars, and other things that you would have to save.

Thats it! Should work smoothly now. Saving and loading is taken care of for you.

As for your question about globals causing memory leaks. You only get a leak when you forget to delete something, this can occur with globals because they persist throughout your application lifetime (as opposed to locals which end up getting deleted once they go out of scope.) However, if you keep track of everything and practice good coding habits you will avoid leaks. In your case the easiest way for modules to communicate to each other are globals. No leaks will occur because gameState will always have just one element (ie we are not adding any additional elements to it.)

Thanks,
M.Y. Developers
[import]uid: 55057 topic_id: 22119 reply_id: 88278[/import]

@M.Y. Developers so when I want to show my score in the next Scene using global I do

 G\_global gameState.money = 0 

And also how would make sure my global is not causing a leak? You said something about remove how would that work? [import]uid: 17058 topic_id: 22119 reply_id: 88281[/import]

Are you trying to display it via text? Perhaps you are trying to do this:

highText.text ="$ " ..gameState.money  

Is this what you are trying to do?


And also how would make sure my global is not causing a leak?

In this case gameState is a table with only 1 element (money.) Since this never gets bigger (ie we don’t add other stuff to it) you do not have to worry about leaks here.

You said something about remove how would that work?

This does not apply here. This generally only applies when we are working with arrays and for loops.
[import]uid: 55057 topic_id: 22119 reply_id: 88285[/import]

Yeah for the text that is what I’m trying to do. But how do I get that to display to another scene without anything being reset? [import]uid: 17058 topic_id: 22119 reply_id: 88290[/import]

Have you tried the code above? In your menus.lua and menu.lua you use that line just replace it with the line above. it should work since gameState is already a global variable. Global variables are visible everywhere, between scenes, between modules too. [import]uid: 55057 topic_id: 22119 reply_id: 88292[/import]

So all I got to do is just this

 high.text = "$:" .. gameState.money = 0 

To display in other scene without being reset
I’m not in my computer using a cell phone. [import]uid: 17058 topic_id: 22119 reply_id: 88294[/import]

Are you guys using Windows and the Corona Simulator with Ice?

I submitted a bug a couple weeks ago about an issue with multiple records being created.

http://developer.anscamobile.com/code/ice?page=3&destination= [import]uid: 38820 topic_id: 22119 reply_id: 88309[/import]

@M.Y.developers Thanks so much for helping me everything is running smoother I appreciate your help alot.

@glennbjr I’m using MAC OS X for ice but is ok I’m json instead and it helps alot. [import]uid: 17058 topic_id: 22119 reply_id: 88367[/import]

@glennbrj
Ice is a great library because it uses the database capabilities of Corona sdk. The JSON approach (code above) is much simpler if all you want to do is save a few keys. Maybe if you are working on an RPG game with thousands of keys you will need to use a fully fledged database.

@sebitttas
Glad we can help, did the memory leak go away? [import]uid: 55057 topic_id: 22119 reply_id: 88474[/import]

@M.Y.developers yes it did go away it actually runs smooth because before it had lags and stutters but adding json improved it thank you again for helping me :slight_smile: [import]uid: 17058 topic_id: 22119 reply_id: 88476[/import]

Absolutely. Ice library is great. I’m using it myself in my current game that I’m developing :slight_smile: [import]uid: 38820 topic_id: 22119 reply_id: 88479[/import]