Thread: [Cppcms-users] How to avoid "Access to unassigned context"
Brought to you by:
artyom-beilis
From: Marcel H. <ke...@co...> - 2011-12-15 08:08:25
|
Good morning, I'm writing a program that uses sessions. Here's my config: "session" : { "location" : "server", "timeout" : 3600, //1 hour "expire" : "renew", "cookies" : { // "domain" : "localhost", "path" : "/", "prefix" : "bg_", "secure" : false, }, "server" : { "storage" : "files", "dir" : "tmp/cookies", "shared" : false, }, "gc" : 86400 //once a day }, now my problem. I'm using the dispatcher, there I assign the "fill" method, named prepare. Everything works until now. In this "fill" method i declare a new pointer to an Object and give this pointer to an other method, named "fillContent". There i use some methods from the "pointed-Object". In that object I have to, or I want to the session. That for the Constructor of the Object becomes the cppcms::application reference. Now I try to set, or to read a cookie. Both fails with the error "Access to unassigned context". If i remove that "session-line" everything works back to normal. Please, give me some advices. Regards, Marcel Hellwig Hamburg, Germany Here some code: Dispatcher::Dispatcher(cppcms::service &s) : cppcms::application(s) { ..... attach(new Techtree(s), "Techtree", "/techtree/{1}", "/techtree/?(.*)", 1); ..... } Techtree::Techtree(cppcms::service& srv) : BasePage(srv) { mapper().assign("{1}"); mapper().assign(""); dispatcher().assign(".*", &Techtree::prepare, this, 0); } void Techtree::prepare(std::string /*page*/) { User* user; ...... user = new User(_srv, session().get<int>("uID")); ...... session().set("uID", "1"); //this works! ..... fillContent(c, user); //c is the struct of template strings delete user; } void Techtree::fillContent(data::techtree::main c, User* user) { c.MetalMineDep = "<tr><td>---</td></tr>"; user->getBuildingLevel(101, 1); c.SolarPowerPlantDep = "<tr><td>---</td></tr>"; render("techtree", c); } User::User(cppcms::service& srv, int id) : cppcms::application(srv), _srv(srv), _userID(id) { _conn_str = _srv.settings().get<std::string>("bg.connection_string"); _sql.open(_conn_str); _init(); } void User::_init() { ........ session().set("test, "test123"); //here the error occurs } |
From: Artyom B. <art...@ya...> - 2011-12-15 09:08:24
|
Hello Marcel, I'll explain, what you are trying to do is to create a "User" class that is derived from cppcms::session on the fly and then destroy it and this "User" object is not connected to the application. cppcms::application objects are designed to be long-living objects. When a request arrives and an application is selected from the pool it, and **all its children** receive a cppcms::http::context, which holds the connection, session, request, response. And once the request is complete, the context is destroyed and the application object returns to the pool. Now, what you are trying to do is to create an application on the fly, and then destroy it. It is not the way to use application because they should be either: a) Stand alone application that are created from the pool (assigned to the applications_pool() of the main service) b) They are connected long living children of the application in the pool, sub-applications. In order to work properly they must be "added" or "attached" to the main application. It is usually done in the constructor of such application. Once they connected they share and use the same context. and they live together with this application as long as it lives. So when you try to call user->session() you get an exception as user does not know about the specific request context and is not connected to any application. In your case User should not be derived from CppCMS application, or it should be attached the same way to the Techtree the way Techree is attached to Dispatcher. I hope it makes it clear. (I think this would be a good entry for wiki, if it is clear enough) Best, Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.sf.net/ CppDB - C++ SQL Connectivity: http://cppcms.sf.net/sql/cppdb/ ----- Original Message ----- > From: Marcel Hellwig <ke...@co...> > To: cpp...@li... > Cc: > Sent: Thursday, December 15, 2011 10:08 AM > Subject: [Cppcms-users] How to avoid "Access to unassigned context" > >G ood morning, > > I'm writing a program that uses sessions. Here's my config: > > "session" : { > "location" : "server", > "timeout" : 3600, //1 hour > "expire" : "renew", > "cookies" : { > // "domain" : "localhost", > "path" : "/", > "prefix" : "bg_", > "secure" : false, > }, > "server" : { > "storage" : "files", > "dir" : "tmp/cookies", > "shared" : false, > }, > "gc" : 86400 //once a day > }, > > now my problem. > I'm using the dispatcher, there I assign the "fill" method, named > prepare. Everything works until now. In this "fill" method i declare a > > new pointer to an Object and give this pointer to an other method, named > "fillContent". There i use some methods from the > "pointed-Object". In > that object I have to, or I want to the session. That for the > Constructor of the Object becomes the cppcms::application reference. > Now I try to set, or to read a cookie. Both fails with the error "Access > to unassigned context". If i remove that "session-line" > everything works > back to normal. > > Please, give me some advices. > > Regards, > Marcel Hellwig > Hamburg, Germany > > > > Here some code: > > Dispatcher::Dispatcher(cppcms::service &s) : cppcms::application(s) { > ..... > attach(new Techtree(s), > "Techtree", > "/techtree/{1}", > "/techtree/?(.*)", 1); > ..... > } > > Techtree::Techtree(cppcms::service& srv) : BasePage(srv) { > mapper().assign("{1}"); > mapper().assign(""); > dispatcher().assign(".*", &Techtree::prepare, this, 0); > } > > void Techtree::prepare(std::string /*page*/) { > User* user; > ...... > user = new User(_srv, session().get<int>("uID")); > ...... > session().set("uID", "1"); //this works! > ..... > fillContent(c, user); //c is the struct of template strings > delete user; > } > > void Techtree::fillContent(data::techtree::main c, User* user) { > > c.MetalMineDep = "<tr><td>---</td></tr>"; > user->getBuildingLevel(101, 1); > c.SolarPowerPlantDep = > "<tr><td>---</td></tr>"; > render("techtree", c); > } > > User::User(cppcms::service& srv, int id) : cppcms::application(srv), > _srv(srv), _userID(id) { > _conn_str = > _srv.settings().get<std::string>("bg.connection_string"); > _sql.open(_conn_str); > _init(); > } > > void User::_init() { > ........ > session().set("test, "test123"); //here the error occurs > } > > ------------------------------------------------------------------------------ > 10 Tips for Better Server Consolidation > Server virtualization is being driven by many needs. > But none more important than the need to reduce IT complexity > while improving strategic productivity. Learn More! > http://www.accelacomm.com/jaw/sdnl/114/51507609/ > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > |
From: Marcel H. <ke...@co...> - 2011-12-15 09:30:26
|
Hello Artyom, I thank you for this great explanation. This makes some things clear. It makes sense and I will costumize the User Class in that way. Thanks again Am 15.12.2011 10:08, schrieb Artyom Beilis: > Hello Marcel, > > > I'll explain, > > what you are trying to do is to create a "User" class that is derived from cppcms::session > on the fly and then destroy it and this "User" object is not connected to the application. > > cppcms::application objects are designed to be long-living objects. When a request > arrives and an application is selected from the pool it, and **all its children** > receive a cppcms::http::context, which holds the connection, session, request, > response. And once the request is complete, the context is destroyed and > the application object returns to the pool. > > Now, what you are trying to do is to create an application on the fly, > and then destroy it. It is not the way to use application because > they should be either: > > a) Stand alone application that are created from the pool > > (assigned to the applications_pool() of the main service) > b) They are connected long living children of the application > in the pool, sub-applications. In order to work properly > they must be "added" or "attached" to the main application. > > > It is usually done in the constructor of such application. > Once they connected they share and use the same context. > and they live together with this application as long as it > lives. > > > So when you try to call user->session() you get an exception > as user does not know about the specific request context and > > is not connected to any application. > > > In your case User should not be derived from CppCMS application, > or it should be attached the same way to the Techtree the way > Techree is attached to Dispatcher. > > I hope it makes it clear. > > (I think this would be a good entry for wiki, if it is clear enough) > > Best, > > > Artyom Beilis > -------------- > CppCMS - C++ Web Framework: http://cppcms.sf.net/ > CppDB - C++ SQL Connectivity: http://cppcms.sf.net/sql/cppdb/ > > > ----- Original Message ----- >> From: Marcel Hellwig<ke...@co...> >> To: cpp...@li... >> Cc: >> Sent: Thursday, December 15, 2011 10:08 AM >> Subject: [Cppcms-users] How to avoid "Access to unassigned context" >> >> G ood morning, >> >> I'm writing a program that uses sessions. Here's my config: >> >> "session" : { >> "location" : "server", >> "timeout" : 3600, //1 hour >> "expire" : "renew", >> "cookies" : { >> // "domain" : "localhost", >> "path" : "/", >> "prefix" : "bg_", >> "secure" : false, >> }, >> "server" : { >> "storage" : "files", >> "dir" : "tmp/cookies", >> "shared" : false, >> }, >> "gc" : 86400 //once a day >> }, >> >> now my problem. >> I'm using the dispatcher, there I assign the "fill" method, named >> prepare. Everything works until now. In this "fill" method i declare a >> >> new pointer to an Object and give this pointer to an other method, named >> "fillContent". There i use some methods from the >> "pointed-Object". In >> that object I have to, or I want to the session. That for the >> Constructor of the Object becomes the cppcms::application reference. >> Now I try to set, or to read a cookie. Both fails with the error "Access >> to unassigned context". If i remove that "session-line" >> everything works >> back to normal. >> >> Please, give me some advices. >> >> Regards, >> Marcel Hellwig >> Hamburg, Germany >> >> >> >> Here some code: >> >> Dispatcher::Dispatcher(cppcms::service&s) : cppcms::application(s) { >> ..... >> attach(new Techtree(s), >> "Techtree", >> "/techtree/{1}", >> "/techtree/?(.*)", 1); >> ..... >> } >> >> Techtree::Techtree(cppcms::service& srv) : BasePage(srv) { >> mapper().assign("{1}"); >> mapper().assign(""); >> dispatcher().assign(".*",&Techtree::prepare, this, 0); >> } >> >> void Techtree::prepare(std::string /*page*/) { >> User* user; >> ...... >> user = new User(_srv, session().get<int>("uID")); >> ...... >> session().set("uID", "1"); //this works! >> ..... >> fillContent(c, user); //c is the struct of template strings >> delete user; >> } >> >> void Techtree::fillContent(data::techtree::main c, User* user) { >> >> c.MetalMineDep = "<tr><td>---</td></tr>"; >> user->getBuildingLevel(101, 1); >> c.SolarPowerPlantDep = >> "<tr><td>---</td></tr>"; >> render("techtree", c); >> } >> >> User::User(cppcms::service& srv, int id) : cppcms::application(srv), >> _srv(srv), _userID(id) { >> _conn_str = >> _srv.settings().get<std::string>("bg.connection_string"); >> _sql.open(_conn_str); >> _init(); >> } >> >> void User::_init() { >> ........ >> session().set("test, "test123"); //here the error occurs >> } >> >> ------------------------------------------------------------------------------ >> 10 Tips for Better Server Consolidation >> Server virtualization is being driven by many needs. >> But none more important than the need to reduce IT complexity >> while improving strategic productivity. Learn More! >> http://www.accelacomm.com/jaw/sdnl/114/51507609/ >> _______________________________________________ >> Cppcms-users mailing list >> Cpp...@li... >> https://lists.sourceforge.net/lists/listinfo/cppcms-users >> > ------------------------------------------------------------------------------ > 10 Tips for Better Server Consolidation > Server virtualization is being driven by many needs. > But none more important than the need to reduce IT complexity > while improving strategic productivity. Learn More! > http://www.accelacomm.com/jaw/sdnl/114/51507609/ > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users |