{"id":25312,"date":"2022-12-09T08:46:55","date_gmt":"2022-12-09T03:16:55","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/"},"modified":"2022-12-09T08:46:55","modified_gmt":"2022-12-09T03:16:55","slug":"solved-access-violation-while-calling-forms-method","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/","title":{"rendered":"[Solved] Access Violation While Calling Form&#8217;s Method"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-31484718\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"31484718\" data-parentid=\"31470216\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>In your question you left out a very important piece of the puzzle. You&#8217;ve mentioned it in comments, but I repeat it here because it&#8217;s the direct trigger of your problem. In comments you said the form is created as follows:<\/p>\n<pre><code>with TForm1.Create(Self) do\nbegin\n  try\n    ShowModal;\n  finally\n    Free;\n  end;\nend;\n<\/code><\/pre>\n<p>Your problem is that within the call-chain of <code>ShowModal<\/code> you expect <code>Form1<\/code> to be assigned <em>specifically to the instance you&#8217;ve just created<\/em>. But clearly you haven&#8217;t yet done anything to set <code>Form1<\/code>.<\/p>\n<p>Your &#8220;solution&#8221; was to assign <code>Form1<\/code> within the <code>ShowModal<\/code> call-chain. While this solves your immediate problem, it is far from correct, and I&#8217;ll explain why later. First I&#8217;ll show the simple solution that would have avoided the problem altogether (note it&#8217;s not a complete solution because there are far more problems with your code than you realise).<\/p>\n<p>Solution, get rid of <strong>with<\/strong>:<\/p>\n<pre><code>\/\/This first line is the most important.\n\/\/It explicitly sets which variable must be assigned to the new form.\nForm1 := TForm1.Create(Self);\ntry\n  Form1.ShowModal;\nfinally\n  Form1.Free;\nend;\n<\/code><\/pre>\n<p>Vishal, try it out. If you do, you&#8217;ll see it works. And hopefully I have your full attention now. You didn&#8217;t believe at first, but perhaps you now realise I really <em><strong>do<\/strong> understand correctly<\/em>.<\/p>\n<hr>\n<p>So why do I say your &#8220;solution&#8221; wrong? After all it seems to solve the problem&#8230;.<\/p>\n<blockquote>\n<p>Well, you said it yourself: you expected <code>Form1<\/code> to be assigned at the start of <code>FormShow<\/code>. You&#8217;re right, it should be. By assigning <code>Form1 := Self;<\/code> inside <code>FormShow<\/code> you&#8217;re just patching up the earlier mistake. Surely you agree it&#8217;s better to fix the original mistake than to just patch over it later?<\/p>\n<\/blockquote>\n<p>But there&#8217;s a deeper problem here&#8230; I&#8217;m not sure you understand the difference between &#8220;object instances&#8221; and &#8220;classes&#8221;. (<em>If you do, then please still read the next few paragraphs for revision.<\/em>)<\/p>\n<p>It seems you expected that when <code>TForm1<\/code> was created it should be automatically assigned to the <code>Form1<\/code> variable. It&#8217;s as if you expected there would only ever be one <code>TForm1<\/code> in memory at any point in time. But <code>TForm1<\/code> is a class type; meaning it generally defines the behaviour for <strong>any number of<\/strong> object instances of the same type. Each time you create a <code>TForm1<\/code>, it&#8217;s a new separate instance of the form. And each instance can have its own variable that is assigned to it. E.g.<\/p>\n<pre><code>JohnsForm1 := TForm1.Create(Self);\nPaulsForm1 := TForm1.Create(Self);\n<\/code><\/pre>\n<p>Consider for a moment what happens with your incorrect &#8220;solution&#8221; if you needed 2 form variable? What would you write inside the <code>FormShow<\/code> method? <code>JohnsForm1 := Self;<\/code> <strong><em>or<\/em><\/strong> <code>PaulsForm1 := Self;<\/code>?<\/p>\n<p>Of course you can still <em>choose<\/em> to keep only 1 instance of <code>TForm1<\/code> in memory at a time. But Delphi has no way to automatically know that is what you intend. So you should still do any desired assignments explicitly as above.<\/p>\n<hr>\n<p>I mentioned there are still more serious problems with your code. It&#8217;s related to the above discussion about object instance and class types.<\/p>\n<p>Your <code>TTest<\/code> class makes a bunch of unnecessary assumptions:<\/p>\n<ul>\n<li>It assumes there will only ever be 1 instance of <code>TForm1<\/code>.<\/li>\n<li>It assumes the form will always be available when needed.<\/li>\n<li>And it assumes the form will be assigned to the <code>Form1<\/code> variable.<\/li>\n<\/ul>\n<p>So again, if you need a <code>JohnsForm1<\/code> and a <code>PaulsForm1<\/code>, your code won&#8217;t work.<\/p>\n<p>A tiny change to <code>TTest.LoadMyData<\/code> and <code>TForm1.FormShow<\/code> solves these problems.<\/p>\n<pre><code>\/\/Write LoadMyData so it can be told which form instance to load the data into\nprocedure TTest.LoadMyData(ALoadForm: TForm1);\nbegin\n  ALoadForm.LoadData;\nend;\n\n\/\/Change FormShow to tell Test which form to use in LoadMyData\nprocedure TForm1.FormShow(Sender: TObject);\nbegin\n  Initilize;\n  Test := TTest.Create;\n  Test.LoadMyData(Self);\nend;\n<\/code><\/pre>\n<p>For the record, David already gave you this information in his answer. His answer also demonstrates the proper resource protection for the new <code>TTest<\/code> instance, whereas I&#8217;ve left it out for the sake of simplicity.<\/p>\n<blockquote>\n<p>By the way, these 2 little changes would have also solved your problem.<br \/>\n  Basically, you had 2 mistakes in your code. The combination of both mistakes caused your problem.<br \/>\n  You can fix either one to make the problem go away. But you should fix both to make your code better.<\/p>\n<\/blockquote><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Access Violation While Calling Form&#8217;s Method <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] In your question you left out a very important piece of the puzzle. You&#8217;ve mentioned it in comments, but I repeat it here because it&#8217;s the direct trigger of your problem. In comments you said the form is created as follows: with TForm1.Create(Self) do begin try ShowModal; finally Free; end; end; Your problem is &#8230; <a title=\"[Solved] Access Violation While Calling Form&#8217;s Method\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/\" aria-label=\"More on [Solved] Access Violation While Calling Form&#8217;s Method\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[740,5282],"class_list":["post-25312","post","type-post","status-publish","format-standard","hentry","category-solved","tag-delphi","tag-delphi-2010"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Access Violation While Calling Form&#039;s Method - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Access Violation While Calling Form&#039;s Method - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] In your question you left out a very important piece of the puzzle. You&#8217;ve mentioned it in comments, but I repeat it here because it&#8217;s the direct trigger of your problem. In comments you said the form is created as follows: with TForm1.Create(Self) do begin try ShowModal; finally Free; end; end; Your problem is ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-09T03:16:55+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Access Violation While Calling Form&#8217;s Method\",\"datePublished\":\"2022-12-09T03:16:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/\"},\"wordCount\":603,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"delphi\",\"delphi-2010\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/\",\"name\":\"[Solved] Access Violation While Calling Form's Method - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-09T03:16:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Access Violation While Calling Form&#8217;s Method\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Access Violation While Calling Form's Method - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Access Violation While Calling Form's Method - JassWeb","og_description":"[ad_1] In your question you left out a very important piece of the puzzle. You&#8217;ve mentioned it in comments, but I repeat it here because it&#8217;s the direct trigger of your problem. In comments you said the form is created as follows: with TForm1.Create(Self) do begin try ShowModal; finally Free; end; end; Your problem is ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/","og_site_name":"JassWeb","article_published_time":"2022-12-09T03:16:55+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Access Violation While Calling Form&#8217;s Method","datePublished":"2022-12-09T03:16:55+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/"},"wordCount":603,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["delphi","delphi-2010"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/","url":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/","name":"[Solved] Access Violation While Calling Form's Method - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-09T03:16:55+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-access-violation-while-calling-forms-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Access Violation While Calling Form&#8217;s Method"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/25312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=25312"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/25312\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=25312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=25312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=25312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}