Board index » cppbuilder » How does it work ?

How does it work ?


2007-11-27 09:36:48 PM
cppbuilder107
Hi there
This might be of topic, but I don't know where else
to ask.
I am vondering what is happening when a program
is running.
example:
void PaintBitmap()
{
Lots of things to do with bitmap_A and its
scanlines
}
void Resize()
{
Change the sise of bitmap_A
}
message loop
program is resized and Resize() is called
My question is:
Is it possible that Resize() gets called while
the program is working in PaintBitmap() ??
I as because I one time have got an AV read of
address when accesing ths scanlines during a
resize of the program.
Thanks in advance
Asger
 
 

Re:How does it work ?

Asger Joergensen < XXXX@XXXXX.COM >writes:
Quote
My question is:
Is it possible that Resize() gets called while
the program is working in PaintBitmap() ??
Only if the code in PaintBitmap calls Resize, either directly or
indirectly, some other function you are calling either calls it either
directly or indirectly. The VCL can end up calling it if you call
Application->ProcessMessages(), and that can interrupt any current
work in progress.
If code isn't written to be reentrant, then it's not always safe to
call something like Application->ProcessMessages.
--
Chris (TeamB);
 

Re:How does it work ?

Hi Chris
Chris Uzdavinis (TeamB) says:
Quote
Asger Joergensen < XXXX@XXXXX.COM >writes:

>My question is:
>Is it possible that Resize() gets called while
>the program is working in PaintBitmap() ??

Only if the code in PaintBitmap calls Resize, either directly or
indirectly,
That doesn't happen.
Quote
some other function you are calling either calls it either
directly or indirectly. The VCL can end up calling it if you call
Application->ProcessMessages(), and that can interrupt any current
work in progress.
This might be the case I'll test some more
Quote
If code isn't written to be reentrant,
What does that mean ??
Quote
then it's not always safe to
call something like Application->ProcessMessages.
Thanks for Your quick reply
Kind regards
Asger
 

{smallsort}

Re:How does it work ?

Asger Joergensen < XXXX@XXXXX.COM >writes:
Quote
>If code isn't written to be reentrant,
What does that mean ??
When a function is interrupted and that interruption starts the
function again from the top, does it work? Recursive functions are
one example of code that must be re-entrant. Stuff running with
asynchronous interrupt handlers requires re-entrancy safety, and, of
course, manual interruptions (like running the event loop in the
middle) means that the event loop could invoke your function as well.
For example, assume contrived_resize() always assumes that its member
is not null (as copying str implies it is a valid pointer!):
class X
{
public:
X() : buf(new char[10]), size(10) { /*...*/ }
X(X const & rhs) : buf(new char[10]), size(10) { /*...*/ }
~X() { delete [] buf; }
// basically works but a "bad function"
void contrived_resize(std::size_t newsize)
{
std::vector<char>copy(str, str + size);
delete str[];
str = new char[newsize];
std::memcpy(str, copy.begin(), std::min(copy.size(), newsize));
}
private:
char const * buf;
std::size_t size;
};
As long as new doesn't throuw in contrived_resize, it will work just
fine. (Well, not tested, but you get the point.)
However, if contrived_resize calls to other code between the delete[]
and the new, then the object is left in a bad state.
void contrived_resize(std::size_t newsize)
{
std::vector<char>copy(str, str + size);
delete str[];
Application->Process_Messages(); // Obviously dangerous
str = new char[newsize];
std::memcpy(str, copy.begin(), std::min(copy.size(), newsize));
}
Other types of examples include:
* short-lived objects that process a list of things and then delete
themselves. Suppose each processing chunk is called by a timer, if
the processor invokes the event loop, a nested timer call could be
made, and the SECOND invocation could complete the work and cause it
to delete itself. Then when the stack unwinds we find ourselves in
the middle of the first invocation and it is running in a deleted
object.
* static local data is usually a sign of non-reentrant code
Etc.
--
Chris (TeamB);
 

Re:How does it work ?

Hi Chris
Thank You very much for taking the time to explaine
I'll have to read it a couple of times to understand
it.
Anyway the error is found and once again it was a
programmers error and not some strange coincident.;-)
I handed a NULL pointer to memcpy but only when the
last extra space at the right side of the bitmap was
less then 60 pixels thats why it didn't happen very often.
Thanks again
Kind regards
Asger
 

Re:How does it work ?

"Asger Joergensen" < XXXX@XXXXX.COM >wrote in message
Quote
This might be of topic, but I don't know where else
to ask.
When in doubt, ask in the .non-technical group first.
Quote
Is it possible that Resize() gets called while
the program is working in PaintBitmap() ??
Only if PaintBitmap() does something that pumps the message queue for new
messages. Otherwise, no.
Quote
I as because I one time have got an AV read of
address when accesing ths scanlines during a
resize of the program.
Then your resize code is not accessing the bitmap correctly, or Resize() is
pumping the message queue in a way that allowed PaintBitmap() to alter the
bitmap while Resize() was still using it.
Gambit