Hey Alex,

2010/7/29 Alex Sadovsky <[email protected]>:
> Hello!
>
> I use WMemoryResource and WImage to render dynamically generated images.
> Then, I use WTimer to generate time events once per second and need to
> update WImage picture in the timer callback. I do it like this:
>
> initialization:
> res = new WMemoryResource("image/jpeg");
> img = new WImage();
>
> timer callback:
> res->setData(img_data_ptr, img_data_size);
> img->setResource(res);
>
> The problem with this code is that very rearly I get application crashs
> somewhere under WMemoryResource::handleRequset and sometimes I don't see the
> image in the brouser. I guess is that it should be some concurrency issue,
> but as I undestand it should safe to update any Wt object from a
> WTimerCallback, so I was wondering if I should use some different method?

Requests for resources are different from other application requests
(events, timer callbacks), in the sense that requests for resources
are handled concurrently: the application lock is not taken for
handleRequest() (that is explained at:
http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WResource.html).
That explains why you may see crashes from time to time: the data in a
WMemoryResource is not being protected from being changed while being
served.

You can still use WMemoryResource, but then you need to reimplements
its handleRequest() method and grab the applicition UpdateLock:

class MyMemoryResource : public WMemoryResource
{
public:
  ..

  void handleRequest(...) {
     WApplication::UpdateLock lock = WApplication::instance()->getUpdateLock();

     WMemoryResource::handleRequest(...);
  }
};

However, you could consider an alternative: generate the modified
image data on the fly in a reimplemented resource.
A browser will cache the image if it is being referenced multiple
times, so you would only expected a single "GET" for it anyway and
thus no extra work or benefit from keeping the image data in memory.

Then, you do:

timer callback:
res->regenerate(...);

resource:

class MyResource : public WResource
{
  ...

  void regenerate(...)
  {
    // protect state with a mutex
    ... // set new state

    setChanged(); // this automatically notifies widgets (e.g. images)
that use the resource
  }

  void handleRequest(...)
  {
    // protect state with a mutex
    // compute image on the fly based on state
  }

private:
  boost::mutex stateMutex_;

  ... // state from which image is generated
};

Btw, a git pull from the public git will give you new WRasterImage and
WPdfImage classes which may interest you if you are generating images
on-the-fly...

Regards,
koen

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to