Browse Source

compiler: set to nil after free

mattias 5 days ago
parent
commit
cb4594a155

+ 2 - 0
compiler/gendef.pas

@@ -73,7 +73,9 @@ begin
      ([cs_link_nolink,cs_link_deffile]*current_settings.globalswitches=[]) then
     DeleteFile(FName);
   importlist.Free;
+  importlist := nil;
   exportlist.Free;
+  exportlist := nil;
 end;
 
 

+ 10 - 0
compiler/globals.pas

@@ -1705,15 +1705,25 @@ implementation
      begin
        calldoneprocs;
        librarysearchpath.Free;
+       librarysearchpath := nil;
        unitsearchpath.Free;
+       unitsearchpath := nil;
        objectsearchpath.Free;
+       objectsearchpath := nil;
        includesearchpath.Free;
+       includesearchpath := nil;
        frameworksearchpath.Free;
+       frameworksearchpath := nil;
        LinkLibraryAliases.Free;
+       LinkLibraryAliases := nil;
        LinkLibraryOrder.Free;
+       LinkLibraryOrder := nil;
        packagesearchpath.Free;
+       packagesearchpath := nil;
        namespacelist.Free;
+       namespacelist := nil;
        premodule_namespacelist.Free;
+       premodule_namespacelist := nil;
        current_namespacelist:=Nil;
      end;
 

+ 16 - 3
compiler/htypechk.pas

@@ -26,7 +26,7 @@ unit htypechk;
 interface
 
     uses
-      cclasses,cmsgs,tokens,
+      sysutils,cclasses,cmsgs,tokens,
       node,globtype,compinnr,
       symconst,symtype,symdef,symsym,symbase,
       pgentype;
@@ -807,6 +807,7 @@ implementation
           begin
             candidates.done;
             ppn.free;
+            ppn := nil;
             if not (ocf_check_only in ocf) then
               begin
                 CGMessage2(parser_e_operator_not_overloaded_2,ld.typename,arraytokeninfo[optoken].str);
@@ -828,6 +829,7 @@ implementation
           begin
             candidates.done;
             ppn.free;
+            ppn := nil;
             if not (ocf_check_only in ocf) then
               begin
                 CGMessage2(parser_e_operator_not_overloaded_2,ld.typename,arraytokeninfo[optoken].str);
@@ -853,6 +855,7 @@ implementation
         if ocf_check_only in ocf then
           begin
             ppn.free;
+            ppn := nil;
             result:=true;
             exit;
           end;
@@ -1005,6 +1008,7 @@ implementation
         if (cand_cnt=0) then
           begin
             ppn.free;
+            ppn := nil;
             if not (ocf_check_only in ocf) then
               t:=cnothingnode.create;
             exit;
@@ -1013,6 +1017,7 @@ implementation
         if ocf_check_only in ocf then
           begin
             ppn.free;
+            ppn := nil;
             result:=true;
             exit;
           end;
@@ -1431,6 +1436,7 @@ implementation
                   for i:=0 to typeconvs.Count-1 do
                     ttypeconvnode(typeconvs[i]).assignment_side:=false;
                 typeconvs.free;
+                typeconvs := nil;
               end;
           end;
 
@@ -2209,6 +2215,7 @@ implementation
         sym : tsym;
       begin
         FIgnoredCandidateProcs.free;
+        FIgnoredCandidateProcs := nil;
         { free any symbols for anonymous parameter types that we're used for
           specialization when no specialization was picked }
         TFPList.FreeAndNilObjects(FParaAnonSyms);
@@ -2229,7 +2236,7 @@ implementation
                        break;
                      end;
                  end;
-               hp^.data.free;
+               FreeAndNil(hp^.data);
              end;
            dispose(hp);
            hp:=hpnext;
@@ -2675,6 +2682,7 @@ implementation
                 if tprocsym(pd.procsym).procdeflist.extract(pd)<>pd then
                   internalerror(20150828);
                 pd.free;
+                pd := nil;
               end;
           end;
 {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
@@ -2689,6 +2697,7 @@ implementation
         calc_distance(st,flags);
 
         ProcdefOverloadList.Free;
+        ProcdefOverloadList := nil;
       end;
 
 
@@ -3100,6 +3109,7 @@ implementation
                    eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
                    check_valid_var:=false;
                    n.free;
+                   n := nil;
                  end
               else if is_open_array(def_to) and
                       is_class_or_interface_or_dispinterface_or_objc_or_java(tarraydef(def_to).elementdef) and
@@ -3201,7 +3211,10 @@ implementation
 
               { maybe release temp currpt }
               if releasecurrpt then
-                currpt.free;
+               begin
+                 currpt.free;
+                 currpt := nil;
+               end;
 
               { next parameter in the call tree }
               pt:=tcallparanode(pt.right);

+ 1 - 0
compiler/import.pas

@@ -122,6 +122,7 @@ procedure DoneImport;
 begin
   if assigned(importlib) then
     importlib.free;
+    importlib := nil;
 end;
 
 end.

+ 23 - 2
compiler/link.pas

@@ -199,6 +199,7 @@ Implementation
         if CStreamError<>0 then
           begin
             fs.Free;
+            fs := nil;
             Comment(V_Error,'Can''t open file: '+fn);
             exit;
           end;
@@ -209,6 +210,7 @@ Implementation
         until bufcount<bufsize;
         buf:=nil;
         fs.Free;
+        fs := nil;
       end;
 
 
@@ -376,10 +378,15 @@ Implementation
     Destructor TLinker.Destroy;
       begin
         ObjectFiles.Free;
+        ObjectFiles := nil;
         SharedLibFiles.Free;
+        SharedLibFiles := nil;
         StaticLibFiles.Free;
+        StaticLibFiles := nil;
         FrameworkFiles.Free;
+        FrameworkFiles := nil;
         OrderedSymbols.Free;
+        OrderedSymbols := nil;
         inherited;
       end;
 
@@ -638,6 +645,7 @@ Implementation
         for i:=0 to p.count-1 do
           src.insert(p[i].Key);
         p.free;
+        p := nil;
       end;
 
 
@@ -677,6 +685,7 @@ Implementation
         symfile.WriteToDisk;
         result:=symfile.fn;
         symfile.Free;
+        symfile := nil;
       end;
 
 
@@ -770,6 +779,7 @@ Implementation
         if result then
           ObjectFiles.concatList(sanitizerlibraryfiles);
         sanitizerlibraryfiles.free;
+        sanitizerlibraryfiles := nil;
       end;
 
 
@@ -1543,9 +1553,13 @@ Implementation
     Destructor TInternalLinker.Destroy;
       begin
         FGroupStack.Free;
+        FGroupStack := nil;
         linkscript.free;
-        StaticLibraryList.Free;
-        ImportLibraryList.Free;
+        linkscript := nil;
+        FStaticLibraryList.Free;
+        FStaticLibraryList := nil;
+        FImportLibraryList.Free;
+        FImportLibraryList := nil;
         if assigned(IsHandled) then
           begin
             IsHandled:=nil;
@@ -1710,7 +1724,9 @@ Implementation
           end;
         { release input object }
         objinput.free;
+        objinput := nil;
         objreader.free;
+        objreader := nil;
       end;
 
 
@@ -1744,7 +1760,9 @@ Implementation
                   TFPObjectList(FGroupStack.Last).Add(stmt);
                 end;
               objinput.Free;
+              objinput := nil;
               objreader.Free;
+              objreader := nil;
             end
           else       { try parsing as script }
             begin
@@ -1752,7 +1770,9 @@ Implementation
               ScriptLexer:=TScriptLexer.Create(objreader);
               ParseLdScript(ScriptLexer);
               ScriptLexer.Free;
+              ScriptLexer := nil;
               objreader.Free;
+              objreader := nil;
             end;
       end;
 
@@ -2244,6 +2264,7 @@ Implementation
       begin
         if assigned(linker) then
          Linker.Free;
+         Linker := nil;
       end;
 
 

+ 2 - 0
compiler/machoutils.pas

@@ -543,6 +543,7 @@ const
         begin
           if AllowFreeWriter then
             ARawWriter.Free;
+            ARawWriter := nil;
           Result:=nil;
         end;
       end;
@@ -587,6 +588,7 @@ const
     begin
       if fown then
         fwriter.Free;
+        fwriter := nil;
       inherited Destroy;
     end;
 

+ 1 - 1
compiler/nadd.pas

@@ -913,7 +913,7 @@ const
               have a side effect }
             if (is_constintnode(left) and might_have_sideeffects(right)) or
               (is_constintnode(right) and might_have_sideeffects(left)) then
-              t.free
+              t.free // no nil needed
             else
               result:=t;
             exit;

+ 7 - 3
compiler/nbas.pas

@@ -863,6 +863,7 @@ implementation
                                 Flags := Flags + (p.left.flags * [nf_usercode_entry]);
 
                                 p.Free;
+                                p := nil;
                                 Continue;
                               end;
                           else
@@ -1032,7 +1033,10 @@ implementation
     destructor tasmnode.destroy;
       begin
         if assigned(p_asm) then
-          p_asm.free;
+          begin
+            p_asm.free;
+            p_asm := nil;
+          end;
         inherited destroy;
       end;
 
@@ -1826,8 +1830,8 @@ implementation
 
     destructor ttempdeletenode.destroy;
       begin
-        tempinfo^.withnode.free;
-        tempinfo^.tempinitcode.free;
+        tempinfo^.withnode.free; // no nil needed
+        tempinfo^.tempinitcode.free; // no nil needed
         dispose(tempinfo);
         inherited destroy;
       end;

+ 25 - 1
compiler/ncal.pas

@@ -582,6 +582,7 @@ implementation
 
         { old argument list skeleton isn't needed anymore }
         parametersnode.free;
+        parametersnode := nil;
 
         pvardatadef:=tpointerdef(search_system_type('PVARDATA').typedef);
 
@@ -614,6 +615,7 @@ implementation
           )
         );
         tcb.free;
+        tcb := nil;
 
         if variantdispatch then
           begin
@@ -971,7 +973,9 @@ implementation
 
       begin
          fparainit.free;
+         fparainit := nil;
          fparacopyback.free;
+         fparacopyback := nil;
          inherited destroy;
       end;
 
@@ -1181,6 +1185,7 @@ implementation
                  left:=ttypeconvnode(left).left;
                  ttypeconvnode(hp).left:=nil;
                  hp.free;
+                 hp := nil;
                end;
              maybe_global_proc_to_nested(left,parasym.vardef);
 
@@ -1726,15 +1731,26 @@ implementation
     destructor tcallnode.destroy;
       begin
          methodpointer.free;
+         methodpointer := nil;
          callinitblock.free;
+         callinitblock := nil;
          callcleanupblock.free;
+         callcleanupblock := nil;
          funcretnode.free;
+         funcretnode := nil;
          if assigned(varargsparas) then
+         begin
            varargsparas.free;
+           varargsparas := nil;
+         end;
          call_self_node.free;
+         call_self_node := nil;
          call_vmt_node.free;
+         call_vmt_node := nil;
          vmt_entry.free;
+         vmt_entry := nil;
          spezcontext.free;
+         spezcontext := nil;
          inherited destroy;
       end;
 
@@ -2396,6 +2412,7 @@ implementation
                         p:=left;
                         Tcallparanode(tinlinenode(temp).left).left:=nil;
                         temp.free;
+                        temp := nil;
 
                         typecheckpass(hightree);
                       end
@@ -2425,6 +2442,7 @@ implementation
                           right:=nil;
                         end;
                       temp.free;
+                      temp := nil;
 
                       {Tree changed from p[l..h] to p[l], recalculate resultdef.}
                       p.resultdef:=nil;
@@ -2446,6 +2464,7 @@ implementation
                         end
                       else
                         temp.free;
+                        temp := nil;
                     end;
                 end;
             end;
@@ -3764,6 +3783,7 @@ implementation
           end;
         if assigned(parents) then
           parents.free;
+          parents := nil;
         { Finally give out a warning for each abstract method still in the list }
         for i:=0 to AbstractMethodsList.Count-1 do
           begin
@@ -3775,7 +3795,10 @@ implementation
               end;
           end;
         if assigned(AbstractMethodsList) then
-          AbstractMethodsList.Free;
+          begin
+            AbstractMethodsList.Free;
+            AbstractMethodsList := nil;
+          end;
       end;
 
 
@@ -5746,6 +5769,7 @@ implementation
         if assigned(n) then
           begin
             inlineblock.free;
+            inlineblock := nil;
             result:=n;
           end;
 

+ 3 - 0
compiler/ncgcon.pas

@@ -363,6 +363,7 @@ implementation
                         internalerror(2013120103);
                    end;
                    datatcb.free;
+                   datatcb := nil;
                    lab_str:=lastlabel.lab;
                    entry^.Data:=lastlabel.lab;
                 end;
@@ -420,6 +421,7 @@ implementation
         current_asmdata.asmlists[al_typedconsts].concatlist(tcb.get_final_asmlist(
           result,resultdef,sec_rodata_norel,result.name,const_align(8)));
         tcb.free;
+        tcb := nil;
       end;
 
 
@@ -515,6 +517,7 @@ implementation
                  current_asmdata.asmlists[al_typedconsts].concatList(
                    datatcb.get_final_asmlist(lastlabel,rec_tguid,sec_rodata_norel,lastlabel.name,const_align(16)));
                  datatcb.free;
+                 datatcb := nil;
                  lab_set:=lastlabel;
                  entry^.Data:=lastlabel;
                end;

+ 3 - 0
compiler/ncgflw.pas

@@ -371,8 +371,10 @@ implementation
              { add all lists together }
              org_list.concatlist(then_list);
              then_list.free;
+             then_list := nil;
              org_list.concatlist(else_list);
              else_list.free;
+             else_list := nil;
              org_list.concatlist(current_asmdata.CurrAsmList);
              current_asmdata.CurrAsmList.free;
              current_asmdata.CurrAsmList := org_list;
@@ -1009,6 +1011,7 @@ implementation
 
              current_asmdata.CurrAsmList.concatList(tmplist);
              tmplist.free;
+             tmplist := nil;
            end
          else
            cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,exceptframekind,finallyexceptionstate,excepttemps);

+ 16 - 0
compiler/ncgrtti.pas

@@ -485,6 +485,7 @@ implementation
             );
 
             loctcb.free;
+            loctcb := nil;
 
             entry^.data:=loclab;
           end
@@ -638,6 +639,7 @@ implementation
             tcb.emit_tai(tai_const.Create_sym(tbllbl),voidpointertype);
           end;
         list.free;
+        list := nil;
       end;
 
 
@@ -725,6 +727,7 @@ implementation
             maybe_add_comment(tcb,'RTTI end field '+tostr(i)+': '+sym.prettyname);
           end;
         fields.free;
+        fields := nil;
       end;
 
 
@@ -885,6 +888,7 @@ implementation
           end;
         tcb.end_anonymous_record;
         list.free;
+        list := nil;
       end;
 
 
@@ -1226,6 +1230,7 @@ implementation
                       tbltcb.get_final_asmlist(tbllab,tbldef,sec_rodata,tbllab.name,const_align(sizeof(pint)))
                     );
                     tbltcb.free;
+                    tbltcb := nil;
                     { write the pointer to the prop info }
                     maybe_add_comment(tcb,#9'property info reference');
                     tcb.emit_tai(Tai_const.Create_sym(tbllab),voidpointertype);
@@ -1720,6 +1725,7 @@ implementation
               tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,
               sizeof(PInt)));
             tcb.free;
+            tcb := nil;
           end;
 
 
@@ -1733,6 +1739,7 @@ implementation
             collect_propnamelist(propnamelist,def,visibilities);
             properties_write_rtti_data(tcb,propnamelist,def.symtable,true,visibilities);
             propnamelist.free;
+            propnamelist := nil;
           end;
 
         var
@@ -1968,6 +1975,7 @@ implementation
             collect_propnamelist(propnamelist,def,visibilities);
             properties_write_rtti_data(tcb,propnamelist,def.symtable,true,visibilities);
             propnamelist.free;
+            propnamelist := nil;
           end;
 
           procedure objectdef_rtti_class_full(def:tobjectdef);
@@ -2024,6 +2032,7 @@ implementation
             tcb.end_anonymous_record;
 
             propnamelist.free;
+            propnamelist := nil;
           end;
 
           procedure objectdef_rtti_interface_full(def:tobjectdef);
@@ -2089,6 +2098,7 @@ implementation
             tcb.end_anonymous_record;
 
             propnamelist.free;
+            propnamelist := nil;
           end;
 
         begin
@@ -2234,6 +2244,7 @@ implementation
             );
 
             argtcb.free;
+            argtcb := nil;
 
             { write argument size and the reference to the argument entry }
             tbltcb.emit_ord_const(argdef.size,u16inttype);
@@ -2295,6 +2306,7 @@ implementation
       );
 
       tbltcb.free;
+      tbltcb := nil;
 
       { write the reference to the attribute table }
       tcb.emit_tai(Tai_const.Create_sym(tbllab),voidpointertype);
@@ -2441,6 +2453,7 @@ implementation
               rttilab,tabledef,sec_rodata,
               rttilab.name,sizeof(PInt)));
             tcb.free;
+            tcb := nil;
 
             current_module.add_public_asmsym(rttilab);
         end;
@@ -2487,6 +2500,7 @@ implementation
             rttilab,tabledef,sec_rodata,
             rttilab.name,sizeof(PInt)));
           tcb.free;
+          tcb := nil;
 
           current_module.add_public_asmsym(rttilab);
         end;
@@ -2526,6 +2540,7 @@ implementation
           syms.sort(@enumsym_compare_value);
           enumdef_rtti_ord2stringindex(rttidef,syms);
           syms.free;
+          syms := nil;
         end;
 
 
@@ -2672,6 +2687,7 @@ implementation
         current_asmdata.AsmLists[al_rtti].concatList(
           tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,min(target_info.alignment.maxCrecordalign,SizeOf(QWord))));
         tcb.free;
+        tcb := nil;
 
         current_module.add_public_asmsym(rttilab);
 

+ 6 - 0
compiler/ncgvmt.pas

@@ -718,6 +718,7 @@ implementation
           end;
 
         classtablelist.free;
+        classtablelist := nil;
       end;
 
 
@@ -923,6 +924,7 @@ implementation
             s,
             sizeof(pint)));
           tcb.free;
+          tcb := nil;
           current_module.add_public_asmsym(sym);
         end;
       s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
@@ -936,6 +938,7 @@ implementation
         s,
         sizeof(pint)));
       tcb.free;
+      tcb := nil;
       current_module.add_public_asmsym(sym);
     end;
 
@@ -1232,6 +1235,7 @@ implementation
            )
          );
          tcb.free;
+         tcb := nil;
 {$ifdef vtentry}
          { write vtinherit symbol to notify the linker of the class inheritance tree }
          hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
@@ -1325,6 +1329,7 @@ implementation
                            current_debuginfo.insertlineinfo(tmplist);
                       list.concatlist(tmplist);
                       tmplist.Free;
+                      tmplist := nil;
                       current_filepos:=oldfileposinfo;
                     end;
                   end;
@@ -1365,6 +1370,7 @@ implementation
                       if (oo_has_vmt in tobjectdef(def).objectoptions) then
                         vmtwriter.writevmt;
                       vmtwriter.free;
+                      vmtwriter := nil;
                       include(def.defstates,ds_vmt_written);
                     end;
                   if is_class(def) then

+ 11 - 2
compiler/ncnv.pas

@@ -612,7 +612,9 @@ implementation
                               for l:=tordconstnode(p2).value.svalue to tordconstnode(p3).value.svalue do
                                 do_set(l);
                               p2.free;
+                              p2 := nil;
                               p3.free;
+                              p3 := nil;
                             end
                            else
                             begin
@@ -649,6 +651,7 @@ implementation
 
                           do_set(tordconstnode(p2).value.svalue);
                           p2.free;
+                          p2 := nil;
                         end
                        else
                         begin
@@ -676,7 +679,7 @@ implementation
               hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
               tarrayconstructornode(p2).right:=nil;
               if freep then
-                p2.free;
+                p2.free; // no nil needed
               current_filepos:=oldfilepos;
               first:=false;
             end;
@@ -687,7 +690,7 @@ implementation
          begin
            { empty set [], only remove node }
            if freep then
-             p.free;
+             p.free; // no nil needed
          end;
         { set the initial set type }
         constp.resultdef:=csetdef.create(hdef,constsetlo.svalue,constsethi.svalue,true);
@@ -895,6 +898,7 @@ implementation
             typecheckpass(fromnode);
             ttypeconvnode(hp).left:=nil;
             hp.free;
+            hp := nil;
             result:=true;
           end;
       end;
@@ -929,6 +933,7 @@ implementation
             typecheckpass(fromnode);
             ttypeconvnode(hp).left:=nil;
             hp.free;
+            hp := nil;
             result:=true;
           end;
       end;
@@ -2716,6 +2721,7 @@ implementation
             typecheckpass(left);
             ttypeconvnode(hp).left:=nil;
             hp.free;
+            hp := nil;
           end;
 
         intfdef:=capturer_add_procvar_or_proc(current_procinfo,left,capturer,hp);
@@ -3481,6 +3487,7 @@ implementation
                       n:=newblock;
                       do_typecheckpass(n);
                       originaldivtree.free;
+                      originaldivtree := nil;
                     end;
                 end
               else
@@ -3618,6 +3625,7 @@ implementation
                   if ([nf_explicit,nf_internal] * flags <> []) then
                     include(result.flags, nf_explicit);
                   hp.free;
+                  hp := nil;
                 end;
             end;
 
@@ -5137,6 +5145,7 @@ implementation
 
       begin
         call.free;
+        call := nil;
         inherited destroy;
       end;
 

+ 22 - 1
compiler/nflw.pas

@@ -760,7 +760,9 @@ implementation
             result:=cnothingnode.create;
             // free unused nodes
             hloopvar.free;
+            hloopvar := nil;
             hloopbody.free;
+            hloopbody := nil;
             exit;
           end;
         { result is a block of statements }
@@ -941,7 +943,9 @@ implementation
               begin
                 result:=cerrornode.create;
                 hloopvar.free;
+                hloopvar := nil;
                 hloopbody.free;
+                hloopbody := nil;
                 MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
               end
             else
@@ -958,7 +962,9 @@ implementation
                 if result.nodetype=errorn then
                   begin
                     hloopvar.free;
+                    hloopvar := nil;
                     hloopbody.free;
+                    hloopbody := nil;
                   end;
               end
             { "for x in [] do ..." always results in a never executed loop body }
@@ -997,7 +1003,9 @@ implementation
                       begin
                         result:=cerrornode.create;
                         hloopvar.free;
+                        hloopvar := nil;
                         hloopbody.free;
+                        hloopbody := nil;
                         MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
                       end
                     else
@@ -1007,7 +1015,9 @@ implementation
                           begin
                             result:=cerrornode.create;
                             hloopvar.free;
+                            hloopvar := nil;
                             hloopbody.free;
+                            hloopbody := nil;
                             MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
                           end
                         else
@@ -1038,7 +1048,9 @@ implementation
                       begin
                         result:=cerrornode.create;
                         hloopvar.free;
+                        hloopvar := nil;
                         hloopbody.free;
+                        hloopbody := nil;
                         MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
                       end;
                     end;
@@ -1061,6 +1073,7 @@ implementation
             n:=tfornode(n).makewhileloop;
             do_firstpass(n);
             hp.Free;
+            hp := nil;
           end;
       end;
 
@@ -1087,7 +1100,9 @@ implementation
 
       begin
          t1.free;
+         t1 := nil;
          t2.free;
+         t2 := nil;
          inherited destroy;
       end;
 
@@ -1325,6 +1340,7 @@ implementation
              left:=Tunarynode(left).left;
              t.left:=nil;
              t.free;
+             t := nil;
              {Symdif operator, in case you are wondering:}
              loopflags:=loopflags >< [lnf_checknegate];
            end;
@@ -1552,6 +1568,7 @@ implementation
                 if code.track_state_pass(exec_known) then
                     track_state_pass:=true;
                 code.free;
+                code := nil;
             end;
         repeat
             condition:=left.getcopy;
@@ -1592,7 +1609,9 @@ implementation
                     done:=true;
                 end;
             code.free;
+            code := nil;
             condition.free;
+            condition := nil;
             firsttest:=false;
         until done;
         {The loop condition is also known, for example:
@@ -1615,7 +1634,7 @@ implementation
             aktstate.store_fact(condition,
              cordconstnode.create(byte(checknegate),pasbool1type,true))
         else
-            condition.free;
+            condition.free; // no nil needed
     end;
 {$endif}
 
@@ -1886,6 +1905,7 @@ implementation
     destructor tfornode.destroy;
       begin
          loopiteration.free;
+         loopiteration := nil;
          inherited destroy;
       end;
 
@@ -2887,6 +2907,7 @@ implementation
         { copied nodes don't need to release the symtable }
         if assigned(excepTSymtable) then
          excepTSymtable.free;
+         excepTSymtable := nil;
         inherited destroy;
       end;
 

+ 20 - 0
compiler/ngenutil.pas

@@ -245,6 +245,7 @@ implementation
          tlocalvarsym(tloadnode(p).symtableentry).inparentfpstruct then
         begin
           p.free;
+          p := nil;
           result:=cnothingnode.create;
         end
       else
@@ -294,6 +295,7 @@ implementation
          tlocalvarsym(tloadnode(p).symtableentry).inparentfpstruct then
         begin
           p.free;
+          p := nil;
           result:=cnothingnode.create;
         end
       else
@@ -498,6 +500,7 @@ implementation
             end;
         end;
       structlist.free;
+      structlist := nil;
     end;
 
 
@@ -866,6 +869,7 @@ implementation
                       trash_large(stat,trashn,caddnode.create(addn,cinlinenode.create(in_high_x,false,trashn.getcopy),genintconstnode(1)),trashintval)
                     else
                       trashn.free;
+                      trashn := nil;
                   end;
                 1: trash_small(stat,
                   ctypeconvnode.create_internal(trashn,s8inttype),
@@ -898,6 +902,7 @@ implementation
         end
       else
         trashn.free;
+        trashn := nil;
     end;
 
 
@@ -1273,6 +1278,7 @@ implementation
       );
 
       unitinits.free;
+      unitinits := nil;
     end;
 
 
@@ -1342,6 +1348,7 @@ implementation
       { set the count at the start }
       placeholder.replace(tai_const.Create_sizeint(count),sizesinttype);
       placeholder.free;
+      placeholder := nil;
       { insert in data segment }
       tabledef:=tcb.end_anonymous_record;
       sym:=current_asmdata.DefineAsmSymbol('FPC_THREADVARTABLES',AB_GLOBAL,AT_DATA,tabledef);
@@ -1351,6 +1358,7 @@ implementation
         )
       );
       tcb.free;
+      tcb := nil;
     end;
 
 
@@ -1406,6 +1414,7 @@ implementation
            current_module.add_public_asmsym(sym);
          end;
        tcb.Free;
+       tcb := nil;
     end;
 
 
@@ -1447,6 +1456,7 @@ implementation
       { Insert TableCount at start }
       countplaceholder.replace(Tai_const.Create_sizeint(count),sizesinttype);
       countplaceholder.free;
+      countplaceholder := nil;
       { insert in data segment }
       tabledef:=tcb.end_anonymous_record;
       current_asmdata.asmlists[al_globals].concatlist(
@@ -1457,6 +1467,7 @@ implementation
         )
       );
       tcb.free;
+      tcb := nil;
     end;
 
 
@@ -1497,6 +1508,7 @@ implementation
           current_asmdata.DefineAsmSymbol(s,AB_GLOBAL,AT_DATA,rawdatadef),
           rawdatadef,sec_data,s,const_align(sizeof(pint))));
       tcb.free;
+      tcb := nil;
       include(current_module.moduleflags,unitflag);
     end;
 
@@ -1558,6 +1570,7 @@ implementation
       { Insert TableCount at start }
       countplaceholder.replace(Tai_const.Create_sizeint(count),sizesinttype);
       countplaceholder.free;
+      countplaceholder := nil;
       { Add to data segment }
       tabledef:=tcb.end_anonymous_record;
       current_asmdata.AsmLists[al_globals].concatList(
@@ -1567,6 +1580,7 @@ implementation
         )
       );
       tcb.free;
+      tcb := nil;
     end;
 
 
@@ -1597,6 +1611,7 @@ implementation
           );
 
           tcb.free;
+          tcb := nil;
         end;
     end;
 
@@ -1640,6 +1655,7 @@ implementation
             tcb.get_final_asmlist(sym,ptruinttype,sec_data,'__stklen',const_align(sizeof(pint)))
           );
           tcb.free;
+          tcb := nil;
         end;
 
       { allocate the stack on the ZX Spectrum system }
@@ -1672,6 +1688,7 @@ implementation
            tcb.get_final_asmlist(sym,def,sec_data,'__stack_cookie',sizeof(pint))
          );
          tcb.free;
+         tcb := nil;
        end;
 {$ENDIF POWERPC}
       { Initial heapsize }
@@ -1682,6 +1699,7 @@ implementation
         tcb.get_final_asmlist(sym,ptruinttype,sec_data,'__heapsize',const_align(sizeof(pint)))
       );
       tcb.free;
+      tcb := nil;
 
       { allocate an initial heap on embedded systems }
       if target_info.system in (systems_embedded+systems_freertos+[system_z80_zxspectrum,system_z80_msxdos]) then
@@ -1702,6 +1720,7 @@ implementation
         tcb.get_final_asmlist(sym,u8inttype,sec_data,'__fpc_valgrind',const_align(sizeof(pint)))
       );
       tcb.free;
+      tcb := nil;
     end;
 
 
@@ -1724,6 +1743,7 @@ implementation
             )
           );
           tcb.free;
+          tcb := nil;
         end;
     end;
 

+ 22 - 0
compiler/ngtcon.pas

@@ -188,6 +188,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
              inserttypeconv(n,def);
            tc_emit_orddef(def,n);
            n.free;
+           n := nil;
         end;
 
       procedure ttypedconstbuilder.parse_floatdef(def:tfloatdef);
@@ -197,6 +198,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           n:=comp_expr([ef_accept_equal]);
           tc_emit_floatdef(def,n);
           n.free;
+          n := nil;
         end;
 
       procedure ttypedconstbuilder.parse_classrefdef(def:tclassrefdef);
@@ -216,6 +218,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           end;
           tc_emit_classrefdef(def,n);
           n.free;
+          n := nil;
         end;
 
       procedure ttypedconstbuilder.parse_pointerdef(def:tpointerdef);
@@ -225,6 +228,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           p:=comp_expr([ef_accept_equal]);
           tc_emit_pointerdef(def,p);
           p.free;
+          p := nil;
         end;
 
       procedure ttypedconstbuilder.parse_setdef(def:tsetdef);
@@ -234,6 +238,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           p:=comp_expr([ef_accept_equal]);
           tc_emit_setdef(def,p);
           p.free;
+          p := nil;
         end;
 
       procedure ttypedconstbuilder.parse_enumdef(def:tenumdef);
@@ -243,6 +248,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           p:=comp_expr([ef_accept_equal]);
           tc_emit_enumdef(def,p);
           p.free;
+          p := nil;
         end;
 
       procedure ttypedconstbuilder.parse_stringdef(def:tstringdef);
@@ -252,6 +258,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           n:=comp_expr([ef_accept_equal]);
           tc_emit_stringdef(def,n);
           n.free;
+          n := nil;
         end;
 
     { ttypedconstbuilder }
@@ -479,7 +486,9 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
     destructor tasmlisttypedconstbuilder.Destroy;
       begin
         fdatalist.free;
+        fdatalist := nil;
         ftcb.free;
+        ftcb := nil;
         inherited Destroy;
       end;
 
@@ -1180,6 +1189,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           begin
             incompatibletypes(node.resultdef,def);
             node.free;
+            node := nil;
             consume_all_until(_SEMICOLON);
             result:=false;
             exit;
@@ -1191,6 +1201,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
         if (bp.curbitoffset>=AIntBits) then
           flush_packed_value(bp);
         node.free;
+        node := nil;
       end;
 
     procedure tasmlisttypedconstbuilder.get_final_asmlists(out reslist, datalist: tasmlist);
@@ -1466,6 +1477,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
                end;
              ftcb.maybe_end_aggregate(def);
              n.free;
+             n := nil;
           end
         else
           begin
@@ -1505,6 +1517,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
         if codegenerror then
           begin
             n.free;
+            n := nil;
             exit;
           end;
         { let type conversion check everything needed }
@@ -1512,6 +1525,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
         if codegenerror then
           begin
             n.free;
+            n := nil;
             exit;
           end;
         { in case of a nested procdef initialised with a global routine }
@@ -1605,6 +1619,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           Message(parser_e_illegal_expression);
         ftcb.maybe_end_aggregate(def);
         n.free;
+        n := nil;
       end;
 
 
@@ -1655,6 +1670,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
                   Message(parser_e_illegal_expression);
               end;
             n.free;
+            n := nil;
             exit;
           end;
         if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
@@ -1666,6 +1682,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
             else
               Message(parser_e_illegal_expression);
             n.free;
+            n := nil;
             exit;
           end;
         ftcb.maybe_begin_aggregate(def);
@@ -1861,6 +1878,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
             else
               ftcb.emit_tai(Tai_const.Create_sym(nil),def);
             n.free;
+            n := nil;
             exit;
           end;
 
@@ -2062,6 +2080,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
                   Message(parser_e_illegal_expression);
               end;
             n.free;
+            n := nil;
             exit;
           end;
         if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
@@ -2073,6 +2092,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
             else
               Message(parser_e_illegal_expression);
             n.free;
+            n := nil;
             exit;
           end;
         { bitpacked record? }
@@ -2186,6 +2206,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
            ) then
           Message1(parser_w_skipped_fields_after,sorg);
         orgbasenode.free;
+        orgbasenode := nil;
         basenode:=nil;
 
         consume(_RKLAMMER);
@@ -2225,6 +2246,7 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
                 basenode:=nil;
               end;
             n.free;
+            n := nil;
             exit;
           end;
 

+ 1 - 0
compiler/nmat.pas

@@ -671,6 +671,7 @@ implementation
                     addstatement(statements,ctempdeletenode.create_normal_temp(resulttemp));
                     addstatement(statements,ctemprefnode.create(resulttemp));
                     right.Free;
+                    right := nil;
                   end
                 else
                   begin

+ 3 - 0
compiler/node.pas

@@ -1094,6 +1094,7 @@ implementation
     destructor tunarynode.destroy;
       begin
         left.free;
+        left := nil;
         inherited destroy;
       end;
 
@@ -1201,6 +1202,7 @@ implementation
     destructor tbinarynode.destroy;
       begin
         right.free;
+        right := nil;
         inherited destroy;
       end;
 
@@ -1355,6 +1357,7 @@ implementation
     destructor ttertiarynode.destroy;
       begin
         third.free;
+        third := nil;
         inherited destroy;
       end;