| View previous topic :: View next topic |
| Author |
Message |
George Kuascha Guest
|
Posted: Sat May 28, 2005 1:17 am Post subject: Destroying a TImage |
|
|
How can I destroy an existing image on a form? I've tried image.free &
image.destroy and I keep getting access violations
|
|
| Back to top |
|
 |
David Ridgway Guest
|
Posted: Sat May 28, 2005 4:58 am Post subject: Re: Destroying a TImage |
|
|
George Kuascha wrote:
| Quote: | How can I destroy an existing image on a form? I've tried image.free &
image.destroy and I keep getting access violations
|
ImageControlName.Free;
ImageControlName := nil;
....is all you need (substitute your control name for ImageControlName).
You can also use FreeAndNil(ImageControlName).
Does the AV happen when you call free, or elsewhere in the code?
Cheers,
Dave
--
David Ridgway : Manager
glyFX : Icons & graphics for developers : www.glyfx.com
|
|
| Back to top |
|
 |
Peter Below (TeamB) Guest
|
Posted: Sat May 28, 2005 9:14 am Post subject: Re: Destroying a TImage |
|
|
In article <4297c5ed$1 (AT) newsgroups (DOT) borland.com>, George Kuascha wrote:
| Quote: | How can I destroy an existing image on a form? I've tried image.free &
image.destroy and I keep getting access violations
|
Note that there is a huge difference between *clearing* an image and
*destroying* it. image.picture.bitmap := nil will clear the image (make
it empty), but the image object itself still exists and can be used to
load a new picture into it. image.Free destroys the object itself, after
that any attempt to use it will result in an access violation.
--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be
|
|
| Back to top |
|
 |
George Kuascha Guest
|
Posted: Sat May 28, 2005 11:31 am Post subject: Re: Destroying a TImage |
|
|
The AV is happening on the img.free method. Here's the code:
if not OpenPictureDialog1.Execute then
begin
BackFileName:= '';
Statusbar1.Panels[2].Text:= 'Background='+ BackFileName;
img.free; <---- error occurs on this statement
img:= nil;
exit;
end;
BGImageFileName:= OpenPictureDialog1.FileName;
BackFileName:= ExtractFileName(BGImageFileName);
img:= TImage.Create(PaintBoxPShape);
img.Parent:= TabSheetDesign;
img.Picture.LoadFromFile(OpenPictureDialog1.FileName);
img.Stretch:= true;
img.Visible:= true;
img.Align:= alClient;
img.SendToBack;
Statusbar1.Panels[2].Text:= 'Background='+ BackFileName;
The onclick method opens a picture dialog box. The user can select an
background image if one doesn't exist or delete an existing image. The
delete occurs when the user hits the cancel button in the dialog box. This
is when the AV occurs.
"David Ridgway"
| Quote: | George Kuascha wrote:
How can I destroy an existing image on a form? I've tried image.free &
image.destroy and I keep getting access violations
ImageControlName.Free;
ImageControlName := nil;
...is all you need (substitute your control name for ImageControlName).
You can also use FreeAndNil(ImageControlName).
Does the AV happen when you call free, or elsewhere in the code?
Cheers,
Dave
--
David Ridgway : Manager
glyFX : Icons & graphics for developers : www.glyfx.com
|
|
|
| Back to top |
|
 |
Ben Hochstrasser Guest
|
Posted: Sat May 28, 2005 12:03 pm Post subject: Re: Destroying a TImage |
|
|
George Kuascha wrote:
| Quote: | if not OpenPictureDialog1.Execute then
begin
BackFileName:= '';
Statusbar1.Panels[2].Text:= 'Background='+ BackFileName;
img.free; <---- error occurs on this statement
^^^^^^^^^
img:= nil;
exit;
end;
BGImageFileName:= OpenPictureDialog1.FileName;
BackFileName:= ExtractFileName(BGImageFileName);
img:= TImage.Create(PaintBoxPShape);
^^^^^^^^^^^^^^^^^^^
img.Parent:= TabSheetDesign;
img.Picture.LoadFromFile(OpenPictureDialog1.FileName);
img.Stretch:= true;
img.Visible:= true;
img.Align:= alClient;
img.SendToBack;
Statusbar1.Panels[2].Text:= 'Background='+ BackFileName;
|
No big surprise. Apparently, img isn't created in the first place (because
you only create it later).
Looks as if your create/free sequences are a bit out of order.
--
Ben
|
|
| Back to top |
|
 |
Andrew Jameson Guest
|
Posted: Sat May 28, 2005 12:35 pm Post subject: Re: Destroying a TImage |
|
|
Hi,
I question why TImage needs to be destroyed and created again ... but no
matter ... what your code reduces to is :
img.Free;
img := nil;
img := TImage.Create(...);
Now you don't need to assign img to a nil as you're about to create it again
....
img.Free;
img := TImage.Create(...);
The only way that calling img.Free will give an a/v is when img <> nil (if
not Assigned(img)) and img has already been freed or given a bad pointer
somewhere else in your code.
Andrew
|
|
| Back to top |
|
 |
Yannis Guest
|
Posted: Mon May 30, 2005 7:54 am Post subject: Re: Destroying a TImage |
|
|
George Kuascha wrote:
| Quote: | The AV is happening on the img.free method. Here's the code:
if not OpenPictureDialog1.Execute then
begin
BackFileName:= '';
Statusbar1.Panels[2].Text:= 'Background='+ BackFileName;
img.free; <---- error occurs on this statement
img:= nil;
exit;
end;
BGImageFileName:= OpenPictureDialog1.FileName;
BackFileName:= ExtractFileName(BGImageFileName);
img:= TImage.Create(PaintBoxPShape);
img.Parent:= TabSheetDesign;
img.Picture.LoadFromFile(OpenPictureDialog1.FileName);
img.Stretch:= true;
img.Visible:= true;
img.Align:= alClient;
img.SendToBack;
Statusbar1.Panels[2].Text:= 'Background='+ BackFileName;
The onclick method opens a picture dialog box. The user can select an
|
Why oh why you do destroy the image when you are inside the onclick
event of the image?? The AV is the only acceptable behavior in this
situation. Use Peter's suggestion and clean the bitmap instead.
Regards
Yannis.
|
|
| Back to top |
|
 |
Avatar Zondertau Guest
|
Posted: Mon May 30, 2005 8:14 am Post subject: Re: Destroying a TImage |
|
|
| Quote: | I question why TImage needs to be destroyed and created again ... but
no matter ... what your code reduces to is :
img.Free;
img := nil;
img := TImage.Create(...);
|
Not true; you're missing the Exit which follows img.Free. This makes
your suggestion dangerous, because it's leave a dangling pointer.
|
|
| Back to top |
|
 |
Andrew Jameson Guest
|
Posted: Tue May 31, 2005 8:41 pm Post subject: Re: Destroying a TImage |
|
|
Dead right ... I did miss the exit !
Andrew
"Avatar Zondertau" <avatarzt (AT) gmail (DOT) com (please reply to newsgroup)> wrote in
message news:429acb6b$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I question why TImage needs to be destroyed and created again ... but
no matter ... what your code reduces to is :
img.Free;
img := nil;
img := TImage.Create(...);
Not true; you're missing the Exit which follows img.Free. This makes
your suggestion dangerous, because it's leave a dangling pointer.
|
|
|
| Back to top |
|
 |
Ben Hochstrasser Guest
|
Posted: Tue May 31, 2005 8:46 pm Post subject: Re: Destroying a TImage |
|
|
Andrew Jameson wrote:
| Quote: | Dead right ... I did miss the exit !
|
Uh, don't drive on unknown highways. <eg>
--
Ben
|
|
| Back to top |
|
 |
Hertwig Guest
|
Posted: Thu Jun 02, 2005 5:22 pm Post subject: Re: Destroying a TImage |
|
|
I think you'll need to tell us how the img var is defined.
Is this a TImage dropped on the form? Is it a local var within the OnClick
handler? Is it a private/protected/public var as defined within the form
declaration?
Hertwig van Zwietering
idimager
http://www.idimager.com
[email]support (AT) idimager (DOT) com[/email]
----------------------------------------------------
"George Kuascha" <gkhokie (AT) hotmail (DOT) com> wrote
| Quote: | The AV is happening on the img.free method. Here's the code:
if not OpenPictureDialog1.Execute then
begin
BackFileName:= '';
Statusbar1.Panels[2].Text:= 'Background='+ BackFileName;
img.free; <---- error occurs on this statement
img:= nil;
exit;
end;
BGImageFileName:= OpenPictureDialog1.FileName;
BackFileName:= ExtractFileName(BGImageFileName);
img:= TImage.Create(PaintBoxPShape);
img.Parent:= TabSheetDesign;
img.Picture.LoadFromFile(OpenPictureDialog1.FileName);
img.Stretch:= true;
img.Visible:= true;
img.Align:= alClient;
img.SendToBack;
Statusbar1.Panels[2].Text:= 'Background='+ BackFileName;
The onclick method opens a picture dialog box. The user can select an
background image if one doesn't exist or delete an existing image. The
delete occurs when the user hits the cancel button in the dialog box. This
is when the AV occurs.
"David Ridgway"
news:4297fa6d (AT) newsgroups (DOT) borland.com...
George Kuascha wrote:
How can I destroy an existing image on a form? I've tried image.free &
image.destroy and I keep getting access violations
ImageControlName.Free;
ImageControlName := nil;
...is all you need (substitute your control name for ImageControlName).
You can also use FreeAndNil(ImageControlName).
Does the AV happen when you call free, or elsewhere in the code?
Cheers,
Dave
--
David Ridgway : Manager
glyFX : Icons & graphics for developers : www.glyfx.com
|
|
|
| Back to top |
|
 |
|