Bizarre viable bug

Returning a table does just that: you get a reference to the table itself. No copying. So changes to one are a change to the “other”.

Also, if you nil the table reference anyhow, wiping its contents clean sounds unnecessary. If that’s the last reference (say, you aren’t still holding the reference in the other file), it will become unreachable and the garbage collector will deal with it at some point for you.

Can you show some more code?

Basically one file returns a table to a file called data using a function called:

data.update(table)

table is structured like so:

table[‘foo’][1] = ‘something’

table[‘bar’][1] = something

In the other scene it calls data.returnTable which looks like such as:

local r = data.returnTable()

The function in data looks like: 

local returnTable = function()

return table

end

data.returnTable = returnTable

In the hide event of composer I have something like:

for i = 1, table.maxn(table) do

table[‘p’] = nil

table = nil

end

This last part of code, no matter what do I do (change the name, change the functions in data, etc…)

is overwriting a global variable that I tore my code apart looking for and couldn’t find. 

On a side how what is are the tags for formatting code?

The formatting codes are and without the space after the [

You should not use a variable called table.  When you nil it, you loose all of the table functions like table.maxn.  This is why globals are bad.  At that point you’ve pretty much broken Lua.

I didn’t actually call it table, that was just to show what it was, I used something far more relevant to what it was actually doing. The table it’s self is not global, I made sure of it by printing out every global lua was running using:

  for k,v in pairs(\_G) do     print("Global key", k, "value", v) end   

and the phantom global never showed it self. I have zero idea whats going on. If I was sure ignoring it wouldn’t affect users I wouldn’t bother to nil it out but I’m not and need to find a solution.

Can you post your real code.  It’s very difficult for anyone in the forum community to help you without seeing it.  Trying to guess based on your psuedo code isn’t helpful.

Rob

Sure.

Here is how my app works. I have a page called data.lua that basically acts as server for all the data (so everything can stay local to it’s respective scene).

In game.lua I have a table called my_defeated_enemies. my_defeated_enemies is a multi deminisional. 

It looks something like:

my\_defeated\_enemies['type'] = 'something' my\_defeated\_enemies['health'] = 'something'

Every now and then it gets sent back to data.lua using:

data.updateMyDefeatedEnemies(my\_defeated\_enemies)

In viewStatics.lua I retive the variable in the composer show stage like so: 

my\_deafed\_enemies = data.returnMyDefeatedEnemies()

Futhermore at the top of the top I declare the variable like so:

  local my\_deafed\_enemies

Then on the hide scene in composer I do:

for i = 1, table.maxn(my\_deafed\_enemies) do my\_deafed\_enemies['type'][i] = nil my\_deafed\_enemies['health'] = nil my\_deafed\_enemies[i] = nil end

And no matter how I do this it is still over riding the variable in data.lua. 

What is the code for those two functions in data?

local returnMyDefeatedEnemies=  function() return my\_defeated\_enemies end data.returnMyDefeatedEnemies = data.returnMyDefeatedEnemies  

local data.updateMyDefeatedEnemies = function(updated) my\_defeated\_enemies = updated end data.updateMyDefeatedEnemies = data.updateMyDefeatedEnemies  

Shouldn’t these be:

local returnMyDefeatedEnemies=  function()     return my\_defeated\_enemies end data.returnMyDefeatedEnemies = returnMyDefeatedEnemies

and

local updateMyDefeatedEnemies = function(updated)     my\_defeated\_enemies = updated end data.updateMyDefeatedEnemies = updateMyDefeatedEnemies

or

data.returnMyDefeatedEnemies=  function()     return my\_defeated\_enemies end

and

data.updateMyDefeatedEnemies = function(updated)     my\_defeated\_enemies = updated end

???
 

Rob

I’m not sure I really see the difference there, isn’t the local declaration making it local to that file?

Anyway I built something similar to this in a brand new app and it still did the same thing. I think there is a bug somewhere in composer or lua that is causing some sort of global override. 

I’m assuming that you have a table called data in your data module.

If you just do:

local function doSomething()

     return something

end

That function is local to that module only can can’t be accessed outside of it.  To make a function available in another module it has to be a member of the table that gets returned.  You can do that one of two ways:

local M = {} – my module table

local function doSomething()

      return something

end

M.doSomething = doSomething

In that form, you create a local function and assign the pointer to the function to the table.  In the second form:

function M.doSomething()

     return something

end

You directly assign the function to the table (in effect creating an anonymous function, but the result is the same to the caller).

The code you posted for the first one isn’t doing this.  You create a local function but in the next line you’re assigning the function to itself:

data.returnMyDefeatedEnemies =  data**.**returnMyDefeatedEnemies

At this point data.returnMyDefetedEnemies is nil since you made a function without the data. piece local above.  In effect no function.  I’m not sure how the second one is compiling at all.  You can’t local a table method like that.  Presumably you’ve already done:

local data = {}

so after that you just have to do:

data.functionName = whatever.

This should be throwing an error.

Rob

Is there a way I can send you a sample app so you can see what I’m talking about? I think we are both misunderstanding each other.

Thanks.

You can post it here (a link to a zip file on Drop box for instance). 

Ok thank you.

That is surely a bad copy + paste? This is more correct:

data.returnMyDefeatedEnemies= function() return my\_defeated\_enemies end

data.updateMyDefeatedEnemies = function(updated) my\_defeated\_enemies = updated end

Alright guys here is some sample code: https://www.dropbox.com/sh/ngr3ufuj8ri9jlr/AAA_aRZ74CZFxdOQXZRoyxfRa

Hit next and then hit back. The app crashes. 

Please let me know what I am doing wrong here. 

Thanks.

-Jeremy

Jeremy, did you get this sorted? I was working on it and wondering if you had your own solution you could share.

It took some research but I did. It turns out returning a table to another scene like I did sets both tables as exactly the same thing (quasi globals) which meant nilling it in one file nilled it in the other.

My solution was to rewrite the table in the scene that had to display it.

Either it’s a Lua bug or I misunderstood how lua works in this regard.

P.S.

I hope that made sense. 

Yup, made perfect sense. When you nil a module’s table somewhere, it’s completely nilled out everywhere.