#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Removing created objects


    Hi
    I have created a TPanel using:
    NewPanel := TPanel.Create(self);
    With NewPanel do
    Begin
    Left := 50
    Top := pnlPerformance.Top;
    Width := 50;
    Height := 41;
    Parent := frmTicket;
    end;

    How do I remove it when I press a button?

    Thanks in advance,
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    If you want to remove the panel you created, then first change this line

    NewPanel := TPanel.Create(self);

    to

    NewPanel := TPanel.Create(nil);

    Passing self makes the form the owner of the panel, which gives the form the responsibility to free it when it itself closes. Passing nil does not set an owner, thus you are controlling the lifetime of the object you created. Just make sure that NewPanel is freed regardless of whether the button is pressed or not. If you don't, you'll have a memory leak in your application.

    then to remove the panel,

    NewPanel.Free;

    One additional note, if you dynamically place other components on the panel, make sure you set their owner property to NewPanel

    NewObject := TNewObject.Create(NewPanel);

    That way, when you free the panel, it'll handle freeing all it's owned objects.
    Last edited by majlumbo; February 14th, 2013 at 08:41 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by majlumbo
    If you want to remove the panel you created, then first change this line

    NewPanel := TPanel.Create(self);

    to

    NewPanel := TPanel.Create(nil);

    Passing self makes the form the owner of the panel, which gives the form the responsibility to free it when it itself closes. Passing nil does not set an owner, thus you are controlling the lifetime of the object you created. Just make sure that NewPanel is freed regardless of whether the button is pressed or not. If you don't, you'll have a memory leak in your application.
    You don't need to do this. Whenever NewPanel freed it will automatically remove itself from its owner's "responsibility" list (if it's not nil of course). Therefore there is no point of this step.

    then to remove the panel,

    NewPanel.Free;
    Yupe, just do this in your button's click.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    Originally Posted by Luthfi
    You don't need to do this. Whenever NewPanel freed it will automatically remove itself from its owner's "responsibility" list (if it's not nil of course). Therefore there is no point of this step.
    While I agree that this step isn't required it is, IMHO, recommended. If I pass an owner object, I'm making a decision that I want the compiler to manage the life of the object, passing nil, I am going to manage its lifetime. If on the other had, I pass in an owner object, and I free the object, what is a maintainer to make of this code??? Simple things like this speaks volumes to a maintainer. And, considering he coded it the way he did, it implied to me that he didn't understand the difference between passing self vs. nil, so I took the extra step to let him know what the parameter meant.

    Just coding it to make it do what you want is OK, doing it the right way is an entirely different matter though.
    Last edited by majlumbo; February 15th, 2013 at 08:19 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Do you really want to dig this? Okay, let's dig in.

    Originally Posted by majlumbo
    While I agree that this step isn't required it is, IMHO, recommended. If I pass an owner object, I'm making a decision that I want the compiler to manage the life of the object, passing nil, I am going to manage its lifetime. If on the other had, I pass in an owner object, and I free the object, what is a maintainer to make of this code??? Simple things like this speaks volumes to a maintainer.
    There is still no point of what you said here. Like I said, even if you set the owner, the object still be okay to be freed by you. Why do you care about the maintainer? You care for nothing. And simple thing like you have done just indicate you love for harder code to maintain AND you don't understand the mechanism behind the ownership.

    Why did I say that with that little thing you just proved that you like harder code to maintain? Okay, let's see.

    * With TPanel.Create(Self):
    The advantage:
    • You still can free the object manually, but it's not mandatory.
    • You don't have to remember to free the object manually.


    * With TPanel.Create(nil)
    The advantage: none
    The disadvantage:
    • You have to remember to free the object manually.


    Now I wonder, which one should I recommend to make my decision speak volumes about my skill?

    And, considering he coded it the way he did, it implied to me that he didn't understand the difference between passing self vs. nil, so I took the extra step to let him know what the parameter meant.
    I admit that I don't have sixth sense or paranormal talent. Therefore I could not get to the same conclusion from the code he wrote. What I saw was that he did a good job in creating the panel.


    Just coding it to make it do what you want is OK, doing it the right way is an entirely different matter though.
    And from no advantage and one disadvantage of using nil (no owner) compared to one advantage and no disadvantage of using specific owner you conclude that option without advantage and a disadvantage is the right way of coding? Nice logic, man!
    Last edited by Luthfi; February 15th, 2013 at 01:35 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    I really suggest you get help. Anger will eat you alive.
    And from no advantage and one disadvantage of using nil (no owner) compared to one advantage and no disadvantage of using specific owner you conclude that option without advantage and a disadvantage is the right way of coding? Nice logic, man!
    After reading the above, I don't think I would ever want to follow your logic....WOW lol....

    And finally, please conduct yourself with a little respect for everyone here in the forum. The ranting you start, just does not help anyone.
    Last edited by majlumbo; February 15th, 2013 at 02:11 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by majlumbo
    I really suggest you get help. Anger will eat you alive.


    After reading the above, I don't think I would ever want to follow your logic....WOW lol....
    And how do you conclude it was anger? Another sixth sense? It looks like you really good at it.

    What? So your logic says you should choose option without advantage? Great logic man!

    And finally, please conduct yourself with a little respect for everyone here in the forum. The ranting you start, just does not help anyone.
    I just proved that you have no basis for your so-called recommendation and you said I was started it? What are you? six years old? Why don't you grow up and explain your reasoning properly instead of throwing accusation?

    And I actually helped by pointing out that your recommendation has no advantage and also has one disadvantage.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    Stunning, the level of stupidity.

    I made my post, the OP is more than welcome to follow or not follow the advice, really it's up to him.

    But, really, get some help...
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by majlumbo
    Stunning, the level of stupidity.

    I made my post, the OP is more than welcome to follow or not follow the advice, really it's up to him.

    But, really, get some help...
    You really are six years old. Instead of proving your point, you just ranting. Grow up, will you?

    I made my post so the OP won't fall victim to your stupid recommendation. It is up to him to follow it. And if you don't like it, don't just call me ranting or stupid. Prove it. Otherwise readers will only see your stupidity.
    Last edited by Luthfi; February 15th, 2013 at 02:42 PM.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    Originally Posted by Luthfi
    You really are six years old. Instead of proving your point, you just ranting. Grow up, will you?

    I made my post so the OP won't fall victim to your stupid recommendation. It is up to him to follow it. And if you don't like it, don't just call me ranting or stupid. Prove it. Otherwise readers will only see your stupidity.
    That's right, he's more than welcome to read all of this and figure it all out on his own.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by majlumbo
    That's right, he's more than welcome to read all of this and figure it all out on his own.
    Yeah, and see the stupid recommendation and how you ranting and call others stupid when you can't prove your point.

    Cheers!
    Last edited by Luthfi; February 15th, 2013 at 02:54 PM.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    Like I said, I made my post, the OP is more than welcome to follow the advice, or to disregard, it's up to him, not you or me.

    I'm sure he is getting more than a few chuckles over this inane conversation. As for me, I just grow weary of it.

    Have a great Delphi day.

    Cheers!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0
    Thank you all very much

    I managed to get the code working,

    thanks again,

    Perran
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    Don't get upset by Luthfi.
    Like you, I have been the object of many of his rants.
    He is actually quite knowledgable and tries to be very helpful.
    However, he (she?) appears to be completely intolerant of any suggestion that does not follow his own, sometimes flawed, opinion.
    Why civility eludes him I do not know. It's a shame as he really does try to help.

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo