Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 01-29-2014, 11:01 PM
delphinn delphinn is offline
Member
 
Join Date: Oct 2012
Posts: 80
Default Memory leaks

How to manage with it?
I do not want spend many words about my trouble, because i can just come out more ignorant than i really am.
I have noticed that my code acts sometimes in one way and then
in different. And while app is runing more and more strange situations come out.
I have get to point that something is wrong with my system in general in
"freeandnil" use. And some objects should be assigned but they are not.
Ok. I think you have got the idea. And you are turning with your head left and right,
and turning eye up now.
But. Teach me pleas like small 5 years old child, step by step.
How to get ride of this?
  #2  
Old 01-29-2014, 11:38 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi,

Memory leak is a bit of a trick depending of what you are developing. most of the memory leaks I had seen, in which is one of the most common problems is when we open forms, and when we close them we don't free them. Most of the times when we open the form again and again, the form gets recreated thus taking up memory, also another way to eat up memory is if you declare lots of global variables, or even local variable inside forms, and when you think you have closed your forms, their are not really closed.

So this is what I do with my forms and perhaps there are other that can do it diferently, however, for me this works fine freeing the memory taken when the form gets created.

First do not auto create your form when you first run your program. See Project->Options..... Forms

Now, create the main form, then another form. Make sure that the only form you auto create is the Main Form of your program.

Place a button and on the click event write thr following:

Code:
Try
MySecondForm := TMySecondForm.Create(Application); 
MySecondForm.Show;
Finally
End;
Now on your your second form (I Called MySecondForm ) Close event write the following code:

Code:
Action :=caFree;
MySecondForm := Nil;
Now, run your app. Open the Windows task Manager and select the processes Tab, there you will find the name of your exe running, see the Memory is currently pulling.

Now, switch to your app, and click the button to open your second form, you will see in the task manager a slight increase of memory taken by your app. Now, Close your form, and check your app in task manager, you will see a decrease in memory taken by your program. The more components and variables you put in your second form the more memory your second form will take, however, once is closed, it should free all that memory taken giving it back to window.

If after you close your app, the memory hasn't give it back to Windows, then most likely your app is open and you cannot see it.

I hope this helps, and perhaps others can add other stuff to this post.

Good luck

Regards
  #3  
Old 01-30-2014, 09:30 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

@delphinn

Make a small, test application and reproduce the error, memory leak. Pm the source.
  #4  
Old 01-30-2014, 10:45 AM
delphinn delphinn is offline
Member
 
Join Date: Oct 2012
Posts: 80
Default

I am doing it.
Thank you.
  #5  
Old 01-30-2014, 01:24 PM
delphinn delphinn is offline
Member
 
Join Date: Oct 2012
Posts: 80
Default

I have add code with two forms.
1.From first Form by click on Button you get shape.
(you have to move shape before creating another to not get one ower other)
2.By right click on Btton, you get pop-up menu to open new form.
3. There you have to click in menu to get new speedbutton.
4. By click on color you have to change SB caption color.
5.by click on SB ,then Form2 gets color of SB caption color and also shape on Form1.

By making many shapes on Form1 and clicking on SB to change color of shapes and forms, You should get state that
color of shape and form2 does not change any more.

Now can you show with red text in my code where i make mistakes which
produces memory leak?

This will be for me more stimulating way to learn, and also i will get
benefit to have code cleared from problems.
(this is test code but working code works the same)
Attached Files
File Type: zip freeandnilltest.zip (12.2 KB, 10 views)
  #6  
Old 01-30-2014, 02:59 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Compare the two code and tell me what was the problem in your code.

//Edit

I did a mistake(sorry for this), on form2 close, instead off:
Form1 := nil;
type this:
Form2 := nil;


and on form1 add this:

Code:
procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
  ClipCursor(nil);
end;
Attached Files
File Type: zip FreeAndNil.zip (14.0 KB, 5 views)

Last edited by kolbasz; 01-31-2014 at 10:27 AM.
  #7  
Old 01-31-2014, 11:00 AM
delphinn delphinn is offline
Member
 
Join Date: Oct 2012
Posts: 80
Default

@kolbasz now i have two problems.
Study your code and to repare my.
(1.but that is what i have asking,2. I am still thinking why of _curshape:=_shape,
3. and other .....)

1.I have to nil forms when i am closing them.
I am using try and nil (@delphimpd).
Next: When i ceate an F2 from F1 with:
Form2:=form2.create(Application);
(In test code i can do it from itch shape)
I can also nil F2 from F1 on close of F1.
But in my working (primar code) app i get error in this case
in formActivate event.
(I get access violation error)


So, my question:
Howto find out
how to nil F2 from F1 im my working code without problems.
Just to mention that niling F2 from F2 at close of F2 is not problem.

This is first step.

Last edited by delphinn; 01-31-2014 at 11:02 AM.
  #8  
Old 01-31-2014, 11:49 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Quote:
1. but that is what i have asking
I was hoping that you find the error by yourself. The main problem in your code is this line:
_Shape := nil;
You nil the shape on MouseUp, then from form2 you try to change the shape's color... This is how you can reproduce the error:
a. Create 2 shapes
b. Click on shape1(right click), open form2, change color, don't close form2
c. Click on shape2(left click), now on mouse up _shape is nil, go to form 2 and try to change color. You can't because _shape is nil.

Quote:
2. I am still thinking why of _curshape:=_shape
The name doesn't matter. I find the name _curshape more intuitive then the name _shape.

Quote:
3. I am using try and nil (@delphimpd). Next: When i ceate an F2 from F1 with: Form2:=form2.create(Application); (In test code i can do it from itch shape) I can also nil F2 from F1 on close of F1. But in my working (primar code) app i get error in this case in formActivate event. (I get access violation error)
You should free and nil the form2, on form2 close. I did a mistake in my original code, but I modified the post(before your last comment). Don't forget to remove form2 from autocreate forms.
  #9  
Old 01-31-2014, 02:43 PM
delphinn delphinn is offline
Member
 
Join Date: Oct 2012
Posts: 80
Default

Thank you abot point 1.



Quote:
You should free and nil the form2, on form2 close.
I do nil F2 in F2 (is it nil also free ?).
But i have set somwhere in F1 to close also F2 when F1
is closed. And i can not find it how closing of F1 drags also to close F2.
I have checked on closing event of F1 and i haven't found anything.

I have found that ClipCursor(nil) (stops mouse to go out of form riht mouse was pressed) makes problems by myself, so i have
inserted ClipCursor(nil); in ssRight shift.

And it looks if i close F1 that automaticaly all forms created as
FX:=FX.create(application) will close. But are they nil?

Last edited by delphinn; 01-31-2014 at 04:37 PM.
  #10  
Old 01-31-2014, 04:47 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Quote:
I do nil F2 in F2 (is it nil also free ?).
When you say Form2.Free you free the memory occupied by Form2.
When you say Form2 := nil you telling to your application, that Form2 doesn't point to a memory address, although that memory address isn't freed.

1. Incorrect pointer usage:
Code:
var
  P: Pointer;
begin
  GetMem(p, 1024);
  FreeMem(p);
  if Assigned(p) then
     ShowMessage('Assigned');
end;
We freed the pointer p with FreeMem, but our application still thinks that p points to a valid address, that's why Assigned(p) will return true.

2. Incorrect pointer usage:
Code:
var
  P: Pointer;
begin
  GetMem(p, 1024);
  p := nil
  if Assigned(p) then
     ShowMessage('Assigned');
end;
We don't free the pointer(we have a memory leak here), but
p don't point to a valid addres and Assigned(p) will return false.

3. Correct pointer usage:
Code:
var
  P: Pointer;
begin
  GetMem(p, 1024);
  FreeMem(p);
  p := nil
  if Assigned(p) then
     ShowMessage('Assigned');
end;
We freed the memory address and p is nil. I hope everything is clear now.



Quote:
But i have set somwhere in F1 to close also F2 when F1
is closed. And i can not find it how closing of F1 drags also to close F2.
I have checked on closing event of F1 and i haven't found anything.
On form1 Close do the following:

Code:
if Assigned(Form2) then
  Form2.Close;

Last edited by kolbasz; 01-31-2014 at 04:54 PM.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 12:18 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.