Recent

Author Topic: [SOLVED]Have troubles checking if a TPicture is created.  (Read 2571 times)

CM630

  • Hero Member
  • *****
  • Posts: 1077
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[SOLVED]Have troubles checking if a TPicture is created.
« on: April 19, 2018, 11:59:56 am »

I want to create ImagePicture: TPicture; if not created.

When I execute the code for the first time and ImagePicture is not yet created (or that is what I think) Assigned(ImagePicture) returns True ?!

I tried this
Code: Pascal  [Select][+][-]
  1. if  Assigned(ImagePicture) then
  2. begin
  3.     ShowMessage ('Assigned');
  4.     if ImagePicture is TPicture then ShowMessage ('is TPicture');
  5. end;
  6.  

and a msgbox Assigned is shown and after that my application crtashes.
What should I do?

EDIT: I tried
Code: Pascal  [Select][+][-]
  1.  if  Assigned(ImagePicture) then
  2.             begin
  3.                 ShowMessage ('Assigned');
  4.                 try
  5.                   if ImagePicture is TPicture then ShowMessage ('is TPicture');
  6.                 finally
  7.                 end;
  8.             end;

Application keeps crashing.
« Last Edit: April 19, 2018, 07:25:27 pm by CM630 »
Лазар 3,0 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Have troubles checking if a TPicture is created.
« Reply #1 on: April 19, 2018, 03:15:02 pm »
Assigned() works, as you are using it. But i think the issue is more about the code that creates and frees it. You need to FreeAndNil(), doing only .Free will not be enough if the variable is used later.

CM630

  • Hero Member
  • *****
  • Posts: 1077
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Have troubles checking if a TPicture is created.
« Reply #2 on: April 19, 2018, 03:36:08 pm »
I have created a brand new application.
All it contains is
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   MyPicture: tPicture;
  4. begin
  5.   if Assigned(MyPicture) then ShowMessage('My Picture is assigned');
  6. end;  

Upon pressing the button it shows „My Picture is assigned‟.


Shall I understand that there is nothing wrong in executing MyPicture:=TPicture.Create; multiple times without freeing it?
« Last Edit: April 19, 2018, 03:37:40 pm by CM630 »
Лазар 3,0 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Have troubles checking if a TPicture is created.
« Reply #3 on: April 19, 2018, 03:36:43 pm »
That will cause memory leaks. You need to free it before creating it again.

CM630

  • Hero Member
  • *****
  • Posts: 1077
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Have troubles checking if a TPicture is created.
« Reply #4 on: April 19, 2018, 03:37:59 pm »
But I cannot check if it is free or not :(
It if I try to .Free when it is already freed or not yet created application crashes. TRY..FINALY does not help.
« Last Edit: April 19, 2018, 03:42:56 pm by CM630 »
Лазар 3,0 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Have troubles checking if a TPicture is created.
« Reply #5 on: April 19, 2018, 03:42:30 pm »
But I cannot check if it is free or not :(

Free it always, before creating it, so you're sure it's free.

Use FreeAndNil(MyPicture)

And you can do this also

var
  MyPicture: tPicture = nil;

CM630

  • Hero Member
  • *****
  • Posts: 1077
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Have troubles checking if a TPicture is created.
« Reply #6 on: April 19, 2018, 03:45:01 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   MyPicture: tPicture;
  4. begin
  5.   FreeAndNil(MyPicture);
  6. end;  
throws an exception.


Edit:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   MyPicture: tPicture=Nil;
  4. begin
  5.   FreeAndNil(MyPicture);
  6. end;
does not throw an exception.
« Last Edit: April 19, 2018, 03:48:30 pm by CM630 »
Лазар 3,0 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Have troubles checking if a TPicture is created.
« Reply #7 on: April 19, 2018, 03:51:34 pm »
@CM630
Although you already discovered your culprit, perhaps have a look at assigned documentation:
Quote
Assigned returns True if P is non-nil and retuns False of P is nil. The main use of Assigned is that Procedural variables, method variables and class-type variables also can be passed to Assigned.
fwiw: there is a typo in there (underlined)

When you define a variable as you did, also the compiler warns:
Quote
Warning: Local variable "MyPicture" does not seem to be initialized

Which gives you a clue as well.

Simplest solution ?:
Code: [Select]
var  MyPicture: tPicture=Nil;
:)

edit: drats  >:D, stop editing your post  ;)

CM630

  • Hero Member
  • *****
  • Posts: 1077
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Have troubles checking if a TPicture is created.
« Reply #8 on: April 19, 2018, 07:28:47 pm »

Thanks for the help, issue is solved!

Simplest solution ?:
Code: [Select]
var  MyPicture: tPicture=Nil;
For some reason I have always thought that for some reason more complex variables get somehow initialised.
Probably because examples in wiki, etc. never bother to init them. %)
Лазар 3,0 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Have troubles checking if a TPicture is created.
« Reply #9 on: April 19, 2018, 09:57:16 pm »
For some reason I have always thought that for some reason more complex variables get somehow initialised.
Managed types, yes.

Quote
Probably because examples in wiki, etc. never bother to init them. %)
Well, wiki examples are a) examples to get you going (not necessarily always showing good practice) and b) you had a specific reason to make use of it.

Always take the hint from compiler (warnings).

More information on initialized variables and managed types. And using default intrinsic (has example with heap vs stack
« Last Edit: April 19, 2018, 09:59:54 pm by molly »

 

TinyPortal © 2005-2018