Running table.copy to many times makes my app crash

Hi,

When I run table.copy to many times in a for loop it crashes in one part but not in another. Explanation with code:

when turns, in the for loop is set to 1 or 2 it runs okay but when I increase it to even as little as 5 it crashes. The strange thing is if I move the first three rows (local h1, h2 and b table.copy) outside the loop it works even though I have more table copies below.

The idea is that in h1, h2 and b I have original value from the user and then the app fills up/randomizes the rest of the table so I need them to be their own values (not reference types).

for yCount = 1, turns, 1 do local h1 = table.copy(hand1) local h2 = table.copy(hand2) local b = table.copy(board) h1,h2,b = randomizeCards(h1,h2,b) -- Player1 hands local player1Value local hands = {} hands[1] = table.copy(h1) h1[6] = b[1] hands[2] = table.copy(h1) h1[6] = b[2] hands[3] = table.copy(h1) h1[6] = b[3] hands[4] = table.copy(h1) end 

I have also tried using the shallowcopy and deepcopy function from the LUA page.

The tables are small and contains 5 records and that’s it.

Can somebody help me with this?

Best regards,

Tomas 

I also tried this:

for count = 1, 5, 1 do h1[count] = hand1[count] h2[count] = hand2[count] b[count] = board[count] end

It seems that it can process 4 times without any problem but on the fifth it always crashes.

Maybe some form of memory leak or some referencing still going on?

Best regards,

Tomas

What error message does it give you?

In your code above, you only create 4 entries in your hands[] table.  When you hit 5, it’s nil and likely generating your error.

Hi,

sorry, it was a rookie mistake!

In the code I change a field for the tables, h1[i].occupied = true, and it got stuck in a while loop later in the program as they weren’t updated correctly to do the table.copy.

Thanks for your time!

Best regards,

Tomas

I also tried this:

for count = 1, 5, 1 do h1[count] = hand1[count] h2[count] = hand2[count] b[count] = board[count] end

It seems that it can process 4 times without any problem but on the fifth it always crashes.

Maybe some form of memory leak or some referencing still going on?

Best regards,

Tomas

What error message does it give you?

In your code above, you only create 4 entries in your hands[] table.  When you hit 5, it’s nil and likely generating your error.

Hi,

sorry, it was a rookie mistake!

In the code I change a field for the tables, h1[i].occupied = true, and it got stuck in a while loop later in the program as they weren’t updated correctly to do the table.copy.

Thanks for your time!

Best regards,

Tomas