Hey all, I was hoping this forum could help me out with a problem I've been dealing with. Thanks in advance to all helpful replies.
At times in my application, I'm running into a memory error. I've narrowed it down to how I'm dealing with the local database. Allow me to explain my scenario. I don't think showing ALL of the code is necessary but I'll show what I think are the relevant parts:
- My application's index.erb page is a title screen with New Game/Resume Game options
- The application first searches for an existing game in the local database. If one is found, the Resume Game button is enabled. If a previous game does not exist, the Resume Game button is disabled. I don't know if it matters, but there can only be one existing game at a time
- Since (to my knowledge) there is no Ruby controller for the index page, I have the following javascript code in my index.erb in a body onLoad() function:
db = new Rho.Database(Rho.Application.databaseFilePath('local'), 'local');
gameModel = Rho.ORM.addModel(function(model) {
model.modelName('Game');
... add properties ...
});
if (gameModel.find('count') > 0) {
... leave button enabled ...
... do other things ...
}
else {
... disable button ...
}
db.close
- This seems to work fine. If I had a previous save, the button is enabled and I can load it using my load code. If no save exists, the button is disabled. Here is how I create a new save. Again this is in javascript in my index.erb file:
db = new Rho.Database(Rho.Application.databaseFilePath('local'), 'local');
Rho.ORM.databaseFullResetAndLogout()
- And then once a game save is created, I call a series of ruby helper functions using AJAX calls which manipulate the local database from the Ruby end and adds other models. I call these functions using this javascript/ruby code:
$.ajax({ url: '<%= url_for(:controller => :Game, :action => :initFunction) %>' })
- And these ruby helper functions are set up like follows:
db = Rho::Database.new(Rho::Application.databaseFilePath('local'), 'local')
db.startTransaction()
begin
gameModel = Game.find(:first)
... do database initializing ...
gameSave.save
db.commitTransaction()
rescue
db.rollbackTransaction()
end
db.close()
- This all seems to work fine. I can create games, and I can resume games that I have previously created.
- HOWEVER, I can only resume games I've created after I've quit the application. In other words, if I create a new game, it gets initialized, then I go back to the title screen (via link) and try to create a new game, I get the following error when executing my first ruby helper function
Error: could not prepare statement: 21; Message: out of memory<br/>Trace: <br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/commonAPI/coreapi/RhoDatabaseApi.rb:174:in `execute'<br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/commonAPI/coreapi/RhoDatabaseApi.rb:174:in `_execute_sql'<br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/commonAPI/coreapi/RhoDatabaseApi.rb:133:in `executeSql'<br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/commonAPI/coreapi/RhoDatabaseApi.rb:299:in `select_from_table'<br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/framework/rhom/rhom_object_factory.rb:129:in `metadata'<br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/framework/rho/render.rb:148:in `render'<br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/framework/rho/rhocontroller.rb:115:in `serve'<br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/framework/rho/rhoapplication.rb:217:in `serve'<br/>
/Users/gtabot/.rvm/gems/ruby-1.9.3-p448/gems/rhodes-4.0.0/lib/framework/rho/rho.rb:895:in `serve'
- If I create a new game, close the app then try creating a new one, it works. If I create a new game, I can go back to the title screen and resume it as well.
- For some reason, I believe this has something to do with how I'm opening/closing the local database from the javascript side. I feel like perhaps the database connection is still open when it tries to create a new connection in my ruby helper function. But I have the close methods present and that doesn't make full sense to me.
Anyone else have had similar problems or could offer me any help? I feel like its something simple that I'm just not fully understanding.
Some additional notes which may or may not be helpful:
- I've tried my New Game code with and without the databaseFullResetAndLogout() function. It didn't seem to make a difference
- I am still able to check for an existing game model in javascript even without the database db variable and its constructor. I guess this is because the local partition is the default? Or maybe it checks ALL partitions?
Thanks a lot.