Awesome Code of the Day:
Awesome Code of the Day:
procedure TMainForm.mnuItemSecMgrClick(Sender: TObject);
begin
TfrmSecMgr.Create(Application).ShowModal;
end;
procedure TMainForm.mnuItemSecMgrClick(Sender: TObject);
begin
TfrmSecMgr.Create(Application).ShowModal;
end;
OnClose -> Action := caFree; // right?
ReplyDeleteMaxim Abramovich Why ?
ReplyDeleteHow would you do that in a better way? Why you consider this a bad practice? If you post your solution along with, it would be much more useful.
ReplyDeleteERTUNÇ EFEOĞLU I believe the generic rule is "the creator is responsable for freeing", i.e.:
ReplyDeletevar
LForm: TSomeFormClass;
begin
LForm := TSomeFormClass.Create(Application|NIL); // create instance
try
LForm.ShowModal; // play with the instance
finally
LForm.Free; // free it
end;
end;
within 1 screen, you can see the class being created, shown to user and freed, using the "Action := caFree" is OK, but the thing is that your code(see Nick's post) looks like voodoo, it's not really clear where it's being created, played with or freed.
Dorin Duminica Would not Delphi compiler manage what is to be disposed when a variable gets out of scope? I thought such a mechanism existed. Maybe mistaken. I've not been using Delphi professionaly for a long time. Just following the product for emotional reasons :)
ReplyDeleteRefactoring some of your old code again? ;) Seriously though, where are you getting these code WTFs?
ReplyDeleteMaxim Abramovich Thanks, your suggestion is more clear than original code
ReplyDeleteERTUNÇ EFEOĞLU you got the answer from Maxim Abramovich, he was faster (:
ReplyDeleteIt is sad that I can't use Delphi professionally. You get rusty when you are not actively using any tool. Thanks both Maxim Abramovich and Dorin Duminica for quick reply.
ReplyDeleteKevin Powick Old code from my new job.
ReplyDeleteNick Hodges You do use hip-waders, I presume? ;)
ReplyDeleteERTUNÇ EFEOĞLU I'm actually very disappointed that Delphi is not used more widely, there are companies(soft) out there that don't know what Delphi is or even heard of it, can you believe that?
ReplyDeleteHowever, Delphi is used widely for Windows "side-work", quick and dirty but it gets the job done (:
Dorin Duminica As long as 10 years ago, I knew of fresh CS grads refusing Delphi jobs in Silicon Valley because they did not want it on ther resumes. And the jobs they were turning down were 6 figures.
ReplyDeleteBill Meyer WOW, currently I do about 99% of my work in Delphi(over 20 projects, a bit less than half are quite big) desktop and web, I feel very lucky (:
ReplyDeleteTurning down Delphi(or any other) job seems to me very weird(to say the least), I'm doing any kind of work even the graphics(most devs wouldn't do that...).
Dorin Duminica I agree, it seemed very strange to me that these people would turn down the work, especially so soon after the dot com crash, but they did. And considering (despite their egos) that it would take them 6-12 months to become sufficiently conversant with the very large systems on which they would have been working, incredibly foolish. However, I also think it was a good thing for the company, that these (probable jerks) disqualified themselves.
ReplyDeleteBill Meyer I don't think of "work" only, relationships are a reword also, even small jobs can lead to big things, so, turn down something? hell no!!
ReplyDeleteI'd probably consider this to be acceptable code, provided the form is being freed in OnClose. But when doing ShowModal it's probably better to explicitly free it. If you are doing just a Show, then it is often easier to let the form you are showing be responsible for its own memory management.
ReplyDeleteHow about:
ReplyDeleteclass function TfrmSecMgr.Execute: Boolean;
var
F: TfrmSecMgr;
begin
F := TfrmSecMgr.Create(nil);
try
Result := IsPositiveResult(F.ShowModal);
finally
F.Free;
end;
end;
There's an easy trick to let automatically free objects as soon as they get out of scope, at least within methods/procedures (but not within classes). I have posted the implementation of such an helper class at http://members.adug.org.au/2011/12/05/smart-pointers/ (see unit Core.Classes.ObjectGuard;).
ReplyDeletePS: The helper class is also to be found within my ToolBox (an extract of some generic classes and stuff) which can be found on my website: http://ddobjects.de/free-software
ReplyDelete