scroll & memory leak problem

**My dear friends,

I tried very hard to add scroll function in my storyboard, and now I can turn pages.
The only problem is my app still crashes sometimes. I am now pretty sure something wrong happened in my memory control. I was wondering if anyone here could kindly check the code for me, I have spent two days, and haven’t got any luck.

warm regards,
Henry

[code]local p01 = storyboard.newScene (“p01”)

function p01:createScene( event )

local localGroup = self.view

local scrollView = widget.newScrollView{
top = 0, left = 0,
width = display.contentWidth, height = display.contentHeight,
bgColor = { 255, 255, 255, 0 }, --Un-Comment this to set the background color of the scrollView.
– scrollBarColor = { 255, 0, 128 }, --Un-Comment this to set the scrollBar to a custom color.
– hideScrollBar = true, --Un-Comment this to hide the scrollBar
– listener = scrollListener
}

local lotsOfText = “\n\n\n\n\n\n\n\n\np01\n\n\n”

local lotsOfTextObject = display.newText(lotsOfText, 0, 0,display.contentWidth-100,0, “Helvetica”, 30, display.contentWidth*.05)
lotsOfTextObject:setTextColor( 0 )
lotsOfTextObject:setReferencePoint( display.TopCenterReferencePoint )
lotsOfTextObject.x = display.contentCenterX
lotsOfTextObject.y = 0

localGroup:insert(lotsOfTextObject)
scrollView:insert(lotsOfTextObject)

local background = display.newRect(localGroup,0, 0, display.contentWidth, display.contentHeight)
background:setFillColor(255, 255, 255)
localGroup:insert(scrollView)

local backbutton = display.newImage (“Graphics/backbutton.png”)
backbutton.x = 390
backbutton.y = 1150
localGroup:insert(backbutton)

local function pressBack (event)
–if event.phase == “ended” then
require (“toc”)
storyboard.gotoScene( “toc” )
storyboard.removeScene( “p01” )
if (scrollView) then
scrollView:removeSelf()
scrollView = nil

end

– end
end

backbutton:addEventListener (“tap”, pressBack)


local narrator = display.newImage (“Graphics/audio.png”)
narrator.x = 520
narrator.y = 1000
localGroup:insert(narrator)

–>This places the "narrator"button

local stop = display.newImage (“Graphics/stop.png”)
stop.x = 290
stop.y = 1000
localGroup:insert(stop)

–> This places the “stop” button

audio01 = audio.loadStream(“Audio/1.mp3”)
function narrator:tap(event)
audio.play(audio01)
end

function stop: tap(event)
audio.stop()
end

narrator:addEventListener (“tap”, narrator)
stop: addEventListener (“tap”, stop)
backbutton:addEventListener (“tap”, stop)



local nextpagebutton = display.newImage (“Graphics/nextpage.png”)
nextpagebutton.x = 620
nextpagebutton.y = 1150
localGroup:insert(nextpagebutton)

local function pressNextpagebutton (event)
– if event.phase == “ended” then

require (“p02”)
storyboard.gotoScene (“p02”)
storyboard.removeScene( “p01” )

if (scrollView) then
scrollView:removeSelf()
scrollView = nil
end

– end
end

nextpagebutton:addEventListener (“tap”, pressNextpagebutton)
nextpagebutton:addEventListener (“tap”, stop)
local previouspagebutton = display.newImage (“Graphics/previouspage.png”)
previouspagebutton.x = 170
previouspagebutton.y = 1150
localGroup:insert(previouspagebutton)

local function pressPreviouspagebutton (event)
– if event.phase == “ended” then

require (“toc”)
storyboard.gotoScene (“toc”)
storyboard.removeScene( “p01” )

if (scrollView) then
scrollView:removeSelf()
scrollView = nil
end

– end
end

previouspagebutton:addEventListener (“tap”, pressPreviouspagebutton)
previouspagebutton:addEventListener (“tap”, stop)
local switchbutton = display.newImage (“Graphics/switch.png”)
switchbutton.x = 170
switchbutton.y = 10000
localGroup:insert(switchbutton)

local function pressSwitchbutton (event)
– if event.phase == “ended” then
require (“p01t”)
storyboard.gotoScene (“p01t”)
storyboard.removeScene( “p01” )

if (scrollView) then
scrollView:removeSelf()
scrollView = nil
end

– end
end

switchbutton:addEventListener (“tap”, pressSwitchbutton)
switchbutton:addEventListener (“tap”, stop)

local title = display.newImage (“Graphics/title2.png”, true, “zoomEven”)
title.x = 390;
title.y = 135;
localGroup:insert(title)

return p01;

end


function p01:enterScene( event )
local group = self.view

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)

end

– Called when scene is about to move offscreen:
function p01:exitScene( event )
local group = self.view

– INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function p01:destroyScene( event )
local group = self.view
print(“scene p01 destroyed”)
p01 = nil

audio.dispose(audio01)
audio01 = nil

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
p01:addEventListener( “createScene”, p01 )

– “enterScene” event is dispatched whenever scene transition has finished
p01:addEventListener( “enterScene”, p01 )

– “exitScene” event is dispatched whenever before next scene’s transition begins
p01:addEventListener( “exitScene”, p01 )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
p01:addEventListener( “destroyScene”, p01 )[code]
[import]uid: 150748 topic_id: 33560 reply_id: 333560[/import]**

You have several things going on that needs addressed. Please bear with me…

 localGroup:insert(lotsOfTextObject)  
 scrollView:insert(lotsOfTextObject)  

An object can only be in one group at a time. In this case it ends up in the scrollView which is what you want, but you don’t need the line above it.

 local function pressBack (event)  
 --if event.phase == "ended" then  
 require ("toc")  
 storyboard.gotoScene( "toc" )  
 storyboard.removeScene( "p01" )   
 if (scrollView) then   
 scrollView:removeSelf()   
 scrollView = nil   
  
 end  
  
 -- end  
 end  
  1. You don’t need to require (“toc”), that’s what Storyboard does for you.
  2. The lines after storyboard.gotoScene(“toc”) will not be executed.
  3. You don’t need to remove the scroll view. Since you added your scroll view to localGroup, it will be managed by Storyboard’s removal methods.
  4. You should not try and remove the current scene from itself. I think it protects itself, but at a minimum it’s not going to work. At worst, it may crash your app.

Finally

 nextpagebutton:addEventListener ("tap", pressNextpagebutton)  
 nextpagebutton:addEventListener ("tap", stop)  

You can’t have a button do two different things like this. It needs to either call pressNextpagebutton or call stop. I think the way you have it, it’s going to try and call stop, not pressNextpagebutton. Regardless, there is no "stop"function that I can see. Is it global to the rest of your program? You probably should remove the one that’s calling stop, because I don’t think its doing what you want it to.

Rob

[import]uid: 199310 topic_id: 33560 reply_id: 133435[/import]

Thank you so much, Rob Miracle.
I will take your advice and amend my code.
Should you need any translation service (from English to Chinese for your app listing details) please do not hesitate to contact me.
My email address is: Dr.Henry.Chen@gmail.com
I am an ebook publisher, I learn Corona SDK for publishing purpose.
Warm regards, :slight_smile:
Henry [import]uid: 150748 topic_id: 33560 reply_id: 133460[/import]

Dear Rob Miracle,

Yes, you are right.
I found that I cannot have a button do two different things like that.
In my audio e-books, Page A should only play Audio A, and Page B should only play Audio B.
When I turned to Page B, the Audio A did stop (this is what I want),
but when I pressed narrator button for Audio B,
I was surprised that Audio A continued to play, rather than Audio B.
Obviously, I failed to dispose audio files when I turned pages.
I have also checked the following code in my lua files,
and my understanding is the following several lines of code should be correct.
How come I could not completely dispose audio files when I turned pages?
Please advise, thank you.

Regards,
Henry

[code]function p01:destroyScene( event )
local group = self.view
print(“scene p01 destroyed”)
p01 = nil

audio.dispose(audio01)
audio01 = nil
end[code]

[import]uid: 150748 topic_id: 33560 reply_id: 133518[/import]

You have several things going on that needs addressed. Please bear with me…

 localGroup:insert(lotsOfTextObject)  
 scrollView:insert(lotsOfTextObject)  

An object can only be in one group at a time. In this case it ends up in the scrollView which is what you want, but you don’t need the line above it.

 local function pressBack (event)  
 --if event.phase == "ended" then  
 require ("toc")  
 storyboard.gotoScene( "toc" )  
 storyboard.removeScene( "p01" )   
 if (scrollView) then   
 scrollView:removeSelf()   
 scrollView = nil   
  
 end  
  
 -- end  
 end  
  1. You don’t need to require (“toc”), that’s what Storyboard does for you.
  2. The lines after storyboard.gotoScene(“toc”) will not be executed.
  3. You don’t need to remove the scroll view. Since you added your scroll view to localGroup, it will be managed by Storyboard’s removal methods.
  4. You should not try and remove the current scene from itself. I think it protects itself, but at a minimum it’s not going to work. At worst, it may crash your app.

Finally

 nextpagebutton:addEventListener ("tap", pressNextpagebutton)  
 nextpagebutton:addEventListener ("tap", stop)  

You can’t have a button do two different things like this. It needs to either call pressNextpagebutton or call stop. I think the way you have it, it’s going to try and call stop, not pressNextpagebutton. Regardless, there is no "stop"function that I can see. Is it global to the rest of your program? You probably should remove the one that’s calling stop, because I don’t think its doing what you want it to.

Rob

[import]uid: 199310 topic_id: 33560 reply_id: 133435[/import]

Thank you so much, Rob Miracle.
I will take your advice and amend my code.
Should you need any translation service (from English to Chinese for your app listing details) please do not hesitate to contact me.
My email address is: Dr.Henry.Chen@gmail.com
I am an ebook publisher, I learn Corona SDK for publishing purpose.
Warm regards, :slight_smile:
Henry [import]uid: 150748 topic_id: 33560 reply_id: 133460[/import]

Dear Rob Miracle,

Yes, you are right.
I found that I cannot have a button do two different things like that.
In my audio e-books, Page A should only play Audio A, and Page B should only play Audio B.
When I turned to Page B, the Audio A did stop (this is what I want),
but when I pressed narrator button for Audio B,
I was surprised that Audio A continued to play, rather than Audio B.
Obviously, I failed to dispose audio files when I turned pages.
I have also checked the following code in my lua files,
and my understanding is the following several lines of code should be correct.
How come I could not completely dispose audio files when I turned pages?
Please advise, thank you.

Regards,
Henry

[code]function p01:destroyScene( event )
local group = self.view
print(“scene p01 destroyed”)
p01 = nil

audio.dispose(audio01)
audio01 = nil
end[code]

[import]uid: 150748 topic_id: 33560 reply_id: 133518[/import]

**Dear Rob Miracle,
Just want to inform you that I have finally solved the problem. Thank you so much for your kind assistance. :slight_smile:
Should you need any translation help please feel free to contact me. (My email: Dr.Henry.Chen@gmail.com I will do my best to help you.)
Regards,
Henry

[import]uid: 150748 topic_id: 33560 reply_id: 133989[/import]**

**Dear Rob Miracle,
Just want to inform you that I have finally solved the problem. Thank you so much for your kind assistance. :slight_smile:
Should you need any translation help please feel free to contact me. (My email: Dr.Henry.Chen@gmail.com I will do my best to help you.)
Regards,
Henry

[import]uid: 150748 topic_id: 33560 reply_id: 133989[/import]**