Browse Source

* always initialize/finalize global refcounted variables in units, also
when they are not used in the unit where they are defined (since they
may be used elsewhere, discovered while analyzing mantis #13345)

git-svn-id: trunk@12954 -

Jonas Maebe 16 years ago
parent
commit
5cf4ab7642
4 changed files with 50 additions and 2 deletions
  1. 2 0
      .gitattributes
  2. 15 2
      compiler/ncgutil.pas
  3. 17 0
      tests/webtbs/tw13345x.pp
  4. 16 0
      tests/webtbs/tw13345y.pp

+ 2 - 0
.gitattributes

@@ -8804,6 +8804,8 @@ tests/webtbs/tw13313.pp svneol=native#text/plain
 tests/webtbs/tw13313a.pp svneol=native#text/plain
 tests/webtbs/tw13313a.pp svneol=native#text/plain
 tests/webtbs/tw1333.pp svneol=native#text/plain
 tests/webtbs/tw1333.pp svneol=native#text/plain
 tests/webtbs/tw13343.pp svneol=native#text/plain
 tests/webtbs/tw13343.pp svneol=native#text/plain
+tests/webtbs/tw13345x.pp svneol=native#text/plain
+tests/webtbs/tw13345y.pp svneol=native#text/plain
 tests/webtbs/tw1348.pp svneol=native#text/plain
 tests/webtbs/tw1348.pp svneol=native#text/plain
 tests/webtbs/tw1351.pp svneol=native#text/plain
 tests/webtbs/tw1351.pp svneol=native#text/plain
 tests/webtbs/tw1364.pp svneol=native#text/plain
 tests/webtbs/tw1364.pp svneol=native#text/plain

+ 15 - 2
compiler/ncgutil.pas

@@ -1076,7 +1076,13 @@ implementation
         hp : tnode;
         hp : tnode;
       begin
       begin
         if (tsym(p).typ in [staticvarsym,localvarsym]) and
         if (tsym(p).typ in [staticvarsym,localvarsym]) and
-           ((tabstractvarsym(p).refs>0) or
+            { local (procedure or unit) variables only need initialization if
+              they are used }
+            ((tabstractvarsym(p).refs>0) or
+            { global (unit) variables always need initialization, since
+              they may also be used in another unit
+            }
+            (tabstractvarsym(p).owner.symtabletype=globalsymtable) or
             { managed return symbols must be inited }
             { managed return symbols must be inited }
             ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
             ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
            ) and
            ) and
@@ -1135,7 +1141,14 @@ implementation
         case tsym(p).typ of
         case tsym(p).typ of
           staticvarsym :
           staticvarsym :
             begin
             begin
-              if (tstaticvarsym(p).refs>0) and
+                  { local (procedure or unit) variables only need finalization
+                    if they are used
+                  }
+              if ((tstaticvarsym(p).refs>0) or
+                  { global (unit) variables always need finalization, since
+                    they may also be used in another unit
+                  }
+                  (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
                  (tstaticvarsym(p).varspez<>vs_const) and
                  (tstaticvarsym(p).varspez<>vs_const) and
                  not(vo_is_funcret in tstaticvarsym(p).varoptions) and
                  not(vo_is_funcret in tstaticvarsym(p).varoptions) and
                  not(vo_is_external in tstaticvarsym(p).varoptions) and
                  not(vo_is_external in tstaticvarsym(p).varoptions) and

+ 17 - 0
tests/webtbs/tw13345x.pp

@@ -0,0 +1,17 @@
+{ %opt=-gh }
+{ %recompile }
+
+{$mode delphi}
+
+uses
+  tw13345y;
+
+type
+  tc = class(tinterfacedobject,ta)
+  end;
+
+begin
+  HaltOnNotReleased:=true;
+  { should be automatically freed by the finalization code of tw13345y }
+  c:=tc.create;
+end.

+ 16 - 0
tests/webtbs/tw13345y.pp

@@ -0,0 +1,16 @@
+unit tw13345y;
+
+{$mode delphi}
+
+interface
+
+type
+  ta = interface
+  end;
+
+var
+  c: ta;
+
+implementation
+
+end.