Commit 2536059dd91812a35c729bfad7d9d2965cc701c4

  • avatar
  • Patrick Stählin <pstaehlin @futu…lab.ch>
  • Tue Aug 24 23:21:04 CEST 2010
Prevent destruction in callbacks
core/scripting/jssocket.cpp
(20 / 2)
  
103103 + e.what(), lsError);
104104 }
105105 }
106 //JS_GC(m_pContext->getJSContext());
106 JS_GC(m_pContext->getJSContext());
107107 }
108108
109109 ScriptContext& getContext() const {
134134 }
135135
136136 protected:
137 void blockingCallback() {
138 m_pAttachedObject.reset(new ScriptContextAttachedObject(&getContext()));
139 }
140
141 void leavingBlockingCallback() {
142 m_pAttachedObject.reset();
143 }
144 protected:
137145 SocketScriptContextExtension& m_Extension;
138146 private:
139147 void ensureIOServiceAvailable() {
156156 boost::shared_ptr<ScriptObject> m_pCallbackObject;
157157 boost::shared_ptr<ScriptFunctionRooter> m_pFunctionRooter;
158158 jsval m_CallbackFunction;
159 boost::shared_ptr<ScriptContextAttachedObject> m_pAttachedObject;
159160 }; // SocketHelper
160161
161162 class SocketHelperInstance : public SocketHelper,
191191 this,
192192 boost::asio::placeholders::error,
193193 ++iterator));
194 blockingCallback();
194195 startIOThread();
195196 }
196197
206206 this,
207207 boost::asio::placeholders::error,
208208 boost::asio::placeholders::bytes_transferred));
209 blockingCallback();
209210 startIOThread();
210211 }
211212
212213 void close() {
213214 m_pSocket->close();
215 leavingBlockingCallback();
214216 }
215217
216218 void receive(const int _numberOfBytes) {
226226 this,
227227 boost::asio::placeholders::error,
228228 boost::asio::placeholders::bytes_transferred));
229 blockingCallback();
229230 startIOThread();
230231 }
231232
253253 if(m_pSocket == NULL && m_pAcceptor != NULL) {
254254 createSocket();
255255 m_pAcceptor->async_accept(*m_pSocket, boost::bind(&SocketHelperInstance::acceptCallback, this, boost::asio::placeholders::error));
256 blockingCallback();
256257 startIOThread();
257258 } else {
258259 Logger::getInstance()->log("SocketHelperInstance::accept: Please call bind first", lsFatal);
265265 tcp::resolver::iterator endpoint_iterator) {
266266 Logger::getInstance()->log("*** Connection callback");
267267 AssertLocked lock(&getContext());
268 leavingBlockingCallback();
268269 //JS_SetContextThread(getContext().getJSContext());
269270 JSRequest req(getContext().getJSContext());
270271 if (!error) {
288288 void sendCallback(const boost::system::error_code& error, std::size_t bytesTransfered) {
289289 Logger::getInstance()->log("*** Send callback");
290290 AssertLocked lock(&getContext());
291 leavingBlockingCallback();
291292 //JS_SetContextThread(getContext().getJSContext());
292293 JSRequest req(getContext().getJSContext());
293294 if(!error) {
311311 JSRequest req(getContext().getJSContext());
312312 if(!error) {
313313 if(bytesTransfered == m_BytesToRead) {
314 leavingBlockingCallback();
314315 std::string result(m_DataBuffer, m_BytesToRead);
315316 callDataCallback(result);
316317 m_BytesToRead = 0;
317318 }
318319 } else {
320 leavingBlockingCallback();
319321 Logger::getInstance()->log("SocketHelperInstance::readCallback: error: " + error.message());
320322 callDataCallback("");
321323 }
327327
328328 void acceptCallback(const boost::system::error_code& error) {
329329 AssertLocked lock(&getContext());
330 leavingBlockingCallback();
330331 //JS_SetContextThread(getContext().getJSContext());
331332 JSRequest req(getContext().getJSContext());
332333 if(!error) {
436436 boost::bind(&SocketHelperSendOneShot::handle_write, this,
437437 boost::asio::placeholders::error));
438438 }
439 void req();
440439
441440 private:
442441 void handle_connect(const boost::system::error_code& error,