Recent

Author Topic: howto delete a compent that is being moved  (Read 5371 times)

chrgra

  • Jr. Member
  • **
  • Posts: 69
howto delete a compent that is being moved
« on: June 08, 2017, 05:27:54 pm »
Dear Members,

If have a component on a panel.
If I move that component to another panel, It has to be deleted. (something like a trashbin)
I could do this with freeandnil(aComponent) and normally that works great.

But because I call it in a procedure of the  component that has to be removed, I get a runtime error.

Anyone a hint, sample code somewhere?

Regards Chris

sky_khan

  • Guest
Re: howto delete a compent that is being moved
« Reply #1 on: June 08, 2017, 05:42:01 pm »
Code: Pascal  [Select][+][-]
  1. TForm1 = class(TForm)
  2. ...
  3.     procedure FreeThis(data:ptrInt);
  4.     procedure TrashBinDragDrop(Sender, Source: TObject; X, Y: Integer);
  5. end;
  6.  
  7. implementation
  8.  
  9. procedure TForm1.FreeThis(data: PtrInt);
  10. begin
  11.   TComponent(data).Free;
  12. end;
  13.  
  14. procedure TForm1.TrashBinDragDrop(Sender, Source: TObject; X, Y: Integer);
  15. begin
  16.   Application.QueueAsyncCall(@FreeThis,PtrInt(Source));
  17. end;
  18.  

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: howto delete a compent that is being moved
« Reply #2 on: June 08, 2017, 05:49:23 pm »
Dear Members,

If have a component on a panel.
If I move that component to another panel, It has to be deleted. (something like a trashbin)
I could do this with freeandnil(aComponent) and normally that works great.

But because I call it in a procedure of the  component that has to be removed, I get a runtime error.

Anyone a hint, sample code somewhere?

Regards Chris
1) you never delete a control while it is dragged only after it has been released (ee mouse button up).
2) you can delegate the deletion to a later time using the queueasynccall mechanism to add a method to be executed when the application goes idle.
3) if you still want to delete on the spot then you have to
  a) end the moving operation manually ee trigger the mouseup manually, make sure it will not receive any more messages
     during destruction. The easiest way is to set its parent to nil, in some cases it might be required to set its owner to nil as well(highly unlikely but keep in mind).
  b) make sure that the component become nil (ee use of freeandnil is mandatory)
  c) change your code to test for nil before accessing it. I'm referring to the event handlers you have written and the sender parameter.
Well thats all from the top of my head. In all the above fail create a small demo to showcase the problem and I'll try to solve the problem for you.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Ondrej Pokorny

  • Full Member
  • ***
  • Posts: 220
Re: howto delete a compent that is being moved
« Reply #3 on: June 08, 2017, 06:23:31 pm »
Code: Pascal  [Select][+][-]
  1. TForm1 = class(TForm)
  2. ...
  3.     procedure FreeThis(data:ptrInt);
  4.     procedure TrashBinDragDrop(Sender, Source: TObject; X, Y: Integer);
  5. end;
  6.  
  7. implementation
  8.  
  9. procedure TForm1.FreeThis(data: PtrInt);
  10. begin
  11.   TComponent(data).Free;
  12. end;
  13.  
  14. procedure TForm1.TrashBinDragDrop(Sender, Source: TObject; X, Y: Integer);
  15. begin
  16.   Application.QueueAsyncCall(@FreeThis,PtrInt(Source));
  17. end;
  18.  

Your code isn't safe although mostly it will work correctly. Unsafe situation: you close the form before FreeThis is called.

Use the ready-to-go Application.ReleaseComponent instead. There's no need to reinvent the wheel.

sky_khan

  • Guest
Re: howto delete a compent that is being moved
« Reply #4 on: June 08, 2017, 06:40:45 pm »
Your code isn't safe although mostly it will work correctly. Unsafe situation: you close the form before FreeThis is called.

Use the ready-to-go Application.ReleaseComponent instead. There's no need to reinvent the wheel.
You have a little something point here, Release Component could also be used but...
Do you have an obligation to prove something? Well, Can you prove me that you can press Alt-F4 under 100ms or so after you drop a component on trashbin ? Thats the question!

See also
Code: Pascal  [Select][+][-]
  1. procedure TApplication.ReleaseComponent(AComponent: TComponent);
  2. begin
  3. ...
  4. ...
  5.     QueueAsyncCall(@FreeComponent, 0); // !!! ATTENTION HERE !!!
  6. end;
  7.  

So ?

Ondrej Pokorny

  • Full Member
  • ***
  • Posts: 220
Re: howto delete a compent that is being moved
« Reply #5 on: June 08, 2017, 07:21:01 pm »
Your code isn't safe although mostly it will work correctly. Unsafe situation: you close the form before FreeThis is called.

Use the ready-to-go Application.ReleaseComponent instead. There's no need to reinvent the wheel.
You have a little something point here, Release Component could also be used but...
Do you have an obligation to prove something? Well, Can you prove me that you can press Alt-F4 under 100ms or so after you drop a component on trashbin ? Thats the question!

If you ask yourself such questions, you are a bad programmer. A good programmer writes code that doesn't break at any possible situation. And yes, the form (or its parent) can be also destroyed by code - and then you'll have a hard time debugging why your application crashes.

See also
Code: Pascal  [Select][+][-]
  1. procedure TApplication.ReleaseComponent(AComponent: TComponent);
  2. begin
  3. ...
  4. ...
  5.     QueueAsyncCall(@FreeComponent, 0); // !!! ATTENTION HERE !!!
  6. end;
  7.  

So ?

You should study the ReleaseComponent code more carefully. The situation I describe above is handled with the AComponent.FreeNotification(Self); call and the corresponding TApplication.Notification method.

The rule is: never send any object to QueueAsyncCall without handling the case it is destroyed with FreeNotification.

sky_khan

  • Guest
Re: howto delete a compent that is being moved
« Reply #6 on: June 08, 2017, 08:00:08 pm »
Obviously, it was a joke, obviously. If you didnt understand that then you're an idiot.

Did not you notice my signature or you did but you choose to ignore it? That's the real question.
Because you might express your concerns about my example by addressing chrgra directly but you did not.

I HATE THIS!!!

Because my English is not enough to defend my idea.

Whatever...

At least show me your code where you release/free the owner form and using ReleaseComponent will work but my example will fail.
Otherwise shut the f* up.

Ondrej Pokorny

  • Full Member
  • ***
  • Posts: 220
Re: howto delete a compent that is being moved
« Reply #7 on: June 08, 2017, 08:30:36 pm »
Obviously, it was a joke, obviously. If you didnt understand that then you're an idiot.

Did not you notice my signature or you did but you choose to ignore it? That's the real question.
Because you might express your concerns about my example by addressing chrgra directly but you did not.

I HATE THIS!!!

It's your problem that you like to send code that has defects and hate people correcting you.

At least show me your code where you release/free the owner form and using ReleaseComponent will work but my example will fail.
Otherwise shut the f* up.

Here you go: https://bugs.freepascal.org/view.php?id=30281. It's even a real world example within the LCL - nothing I would invent myself now just to prove you wrong.

sky_khan

  • Guest
Re: howto delete a compent that is being moved
« Reply #8 on: June 08, 2017, 09:00:48 pm »
Your link is not quite related. Proposed fix is in TApplication code. There is no mention of ReleaseComponent either. Do you expect me to examine full anchordocking code history for this?
Is it really this hard to write a few lines of code where calling ReleaseComponent will work but QueueAsyncCall will fail in this thread's situation ?
You still not gave an example and you still not answered if you noticed my signature or not.

Btw, I dont hate people correcting me. I hate wasting a lot of time trying answer these. I have to think to choose words, sometimes I have to look dictionary. Still, my posts are full of grammar errors when I try to write in English. Because I can read but I cant write, I cant think in English etc etc. 

And all I ask from forum members was not adressing me but questioners. This way I wont have to answer these. For example, It took 2-3 minutes to write (faulty, according to you) example in my first post but this post took more than 15 minutes. Why should I do that ? I tried to ignore these but it is not a good solution either.

sky_khan

  • Guest
Re: howto delete a compent that is being moved
« Reply #9 on: June 08, 2017, 09:58:55 pm »
No answer, no code. Just talking...

I finally found a solution for my problem. I dont want to be a burden here. So I'm giving you a favor and releasing (pun intended) all of you from correcting my mistakes by just talking. You will never ever have to do this again. Lucky you.

chrgra

  • Jr. Member
  • **
  • Posts: 69
Re: howto delete a compent that is being moved
« Reply #10 on: June 08, 2017, 10:36:14 pm »
@sky_kahn
@Ondrej Pokorny
@taazz

At first : Thank all of you for helping me with this issue.
At second : sky_kahn and Ondrej Pokorny. Please don't argue with each other. Live is too short. I started to program again as a therapy. (Had too much stress the past years. So it is really just hobby). You are both willing to help other people. So you are both good persons!!!!!!!!

At third:  I changed the  line in 'Application.ReleaseComponent(TA);' and now it works at a charme.....

Enjoy life. I tried this for the past 10 years and I still can't handle that.......

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: howto delete a compent that is being moved
« Reply #11 on: June 09, 2017, 05:55:29 am »
TGIF I guess. As for the rest I did not knew about releaseComponent. Thank you Ondrej.
Sky_khan you should relax a bit, you should be thankful that others help out instead of letting all the burden on you. After all time is limited. Then again I have problems leaving my own life what do I know about yours. Have fun what ever you do.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018