 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mauro Guest
|
Posted: Fri Jan 23, 2004 6:41 am Post subject: Why does this not work??? Mysterious... |
|
|
Graphics::TBitmap *SrcBitmap=new Graphics::TBitmap;
Graphics::TBitmap *DestBitmap=new Graphics::TBitmap;
SrcBitmap = Image1->Picture->Bitmap; "<------ problem statement
Image1->Picture->Bitmap=DestBitmap;
delete DestBitmap;
delete SrcBitmap; "<-------- PROBLEM, WHY ???
Hi all,
I cannot understand why there is an acces violation for this
example. As I do: SrcBitmap = Image1->Picture->Bitmap;
, it means that I copy the bitmap to SrcBitmap, or not??
If I use SrcBitmap->Assign(Image1->Picture->Bitmap) no
problem occurs...
Can somebody explain me what is the problem here??
Thanks Mauro
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Fri Jan 23, 2004 7:03 am Post subject: Re: Why does this not work??? Mysterious... |
|
|
"Mauro" <Mauro23 (AT) gmx (DOT) de> wrote:
| Quote: | I cannot understand why there is an acces violation for this
example.
Graphics::TBitmap *SrcBitmap=new Graphics::TBitmap;
|
Here you define a pointer and assign it the memory location of
the newly allocated TBitmap.
| Quote: | SrcBitmap = Image1->Picture->Bitmap;
|
Here you assign the pointer again thus not only causing a memory
leak (because you just lost the pointer to the allocated TBitmap)
but also setting yourself up for the Access Violation when you
try to free (delete) the allocated TBitmap.
To be clear, SrcBitmap is a pointer to a TBitmap which is not
the same as a Bitmap property.
What you wanted to do was to copy the Image1 bitmap over to the
new TBitmap right? You would do that using the TBitmap::Assign
method:
SrcBitmap->Assign( Image1->Picture->Bitmap );
~ JD
|
|
| Back to top |
|
 |
Mauro Guest
|
Posted: Fri Jan 23, 2004 7:25 am Post subject: Re: Why does this not work??? Mysterious... |
|
|
That's right... I haven't sleep so much this night :-)
But if I do Image->Picture->Bitmap = Bitmap then
the Bitmap is copied, right?
Regards,
Mauro
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Jan 23, 2004 7:30 pm Post subject: Re: Why does this not work??? Mysterious... |
|
|
"Mauro" <Mauro23 (AT) gmx (DOT) de> wrote
| Quote: | Graphics::TBitmap *SrcBitmap=new Graphics::TBitmap;
snip
SrcBitmap = Image1->Picture->Bitmap; "<------ problem statement
|
You instantiated SrcBitmap to point to one TBitmap instance, but then you
are assigning the pointer to point elsewhere. Thus you lose the pointer to
the first TBitmap and have a memory leak since you can never free the
TBitmap you instantiated.
| Quote: | delete SrcBitmap; "<-------- PROBLEM, WHY ???
|
Because you are deleting the TImage's Bitmap directly. TBitmap does not
have an '=' operator inplemented, you can't just assign the TImage's Bitmap
property to your SrcBitmap pointer directly like you are. That is not
making a copy of the Bitmap data. If you meant to copy the data, then you
need to use the Assign() method instead of the '=' operator:
SrcBitmap->Assign(Image1->Picture->Bitmap);
| Quote: | As I do: SrcBitmap = Image1->Picture->Bitmap;
, it means that I copy the bitmap to SrcBitmap, or not??
|
No, it does not. It assigns SrcBitmap to point to the original TImage
Bitmap directly, it does not make a copy of it at all. You are probably
getting confused by the fact that you can safely use the '=' operator when
assigning the TImage's Bitmap property with a new image. That works because
the Bitmap property has a setter method that calls Assign() internally, such
as the following example:
void __fastcall TPicture::SetBitmap(TBitmap *Value)
{
FBitmap->Assign(Value);
}
Thus, the statement:
Image1->Picture->Bitmap=DestBitmap;
Is functionally the same as the following:
Image1->Picture->Bitmap->Assign(DestBitmap);
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Jan 23, 2004 7:31 pm Post subject: Re: Why does this not work??? Mysterious... |
|
|
"Mauro" <Mauro23 (AT) gmx (DOT) de> wrote
| Quote: | But if I do Image->Picture->Bitmap = Bitmap then
the Bitmap is copied, right?
|
Yes, because the Bitmap property calls Assign() internally.
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|