Hi
I found my memory leak, so it seems. My app is a set of flashcards for exam prep. There are 439 questions, divided into 6 categories. The user can choose a category, or answer all the questions. The questions live in a json file that is read into a table when the program starts. In addition to the table of questions there is another array of numbers from 1 to the length of the array (439 to start with, then 438, 437 …).
Before each card the app randomly picks a number from 1 to the length of the array. It then removes that number from the array and uses that number as the index to grab a question from the question table. That way no question gets asked twice and the questions always appear in a different sequence.
Also before each card it checks to see whether the question at the index value is of the type that the user wants to answer. If so, it display the question. If not, it just gets another random index.
Here’s the mystery: When playing the app I found that the only time I had no memory leak was when I chose to answer ALL the questions, not focusing on a category, ie. no cards were being skipped over. The category with the fewest questions (“score excerpts”) had the biggest memory leak.
I tried various things to no avail but then tried deleting every question from the json file except the score excerpt questions. Suddenly there was no memory leak at all. I have since put each question type into its own json file and have 7 question tables instead of 1. No more memory leak :) but I’m baffled by what was causing it.
Any thoughts? I’ve posted the relevant code below
thanks,
David
----------------------------------- FUNCTION: initializeNewGame -- gets called each time user start new game ----------------------------------- initializeNewGame = function() local myrandomSeed = math.randomseed( os.time() ) myData.questionsAnswered = 0 myData.score = 0 -- there are two types of layout required I need to know what type to use: myData.inRegularCard = false myData.inExcerptCard = false -- create an array with as many elements as there are questions: local rq for rq=1, #myData.questions do myData.arrayToRandomizeQuestions[rq] = rq end end -- end of function initializeNewGame() ----------------------------------- -- FUNCTION: getRandomIndexForQuestions -- this generates a number at random from 1 to the number of questions remaining -- it then assigns this value to myData.questionIndex and takes that number out of the array ----------------------------------- getRandomIndexForQuestions = function () local randomNumber = math.random(1, #myData.arrayToRandomizeQuestions ) myData.questionIndex = table.remove(myData.arrayToRandomizeQuestions, randomNumber) end -- end of function getRandomIndexForQuestions() ------------------------------------------------ -- FUNCTION findTheNextCardOfRightType ------------------------------------------------ findTheNextCardOfRightType = function() getRandomIndexForQuestions() myData.displayQuestionCardGroup = display.newGroup() if (myData.questionChoice == "Everything") then -- user want all questions if (myData.questions[myData.questionIndex].question\_type == "Score excerpts") then displayExcerptCard() -- one type of layout else displayRegularCard() -- the other type of layout end elseif (myData.questionChoice == "Score excerpts") then -- user want excerpts if (myData.questions[myData.questionIndex].question\_type == "Score excerpts") then displayExcerptCard() else -- this question is not an excerpt: ignore and continue continueToNextCardOrQuit() end else -- all other types of questions need displayRegularCard() layout if (myData.questionChoice == myData.questions[myData.questionIndex].question\_type) then displayRegularCard() else -- ignore that question and continue continueToNextCardOrQuit() end end return myData.displayQuestionCardGroup -- DO I NEED THIS? end -- end of function findTheNextCardOfRightType()