Forum > General

[SOLVED]Have troubles checking if a TPicture is created.

(1/2) > >>

CM630:

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if  Assigned(ImagePicture) thenbegin    ShowMessage ('Assigned');    if ImagePicture is TPicture then ShowMessage ('is TPicture');end; 
and a msgbox Assigned is shown and after that my application crtashes.
What should I do?

EDIT: I tried

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- if  Assigned(ImagePicture) then            begin                ShowMessage ('Assigned');                try                  if ImagePicture is TPicture then ShowMessage ('is TPicture');                finally                end;            end;
Application keeps crashing.

User137:
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:
I have created a brand new application.
All it contains is

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  MyPicture: tPicture;begin  if Assigned(MyPicture) then ShowMessage('My Picture is assigned');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?

lainz:
That will cause memory leaks. You need to free it before creating it again.

CM630:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version