Autor Beitrag
GlasshouseManor
Hält's aus hier
Beiträge: 10


Delphi Enterprise
BeitragVerfasst: Do 26.03.09 21:44 
Ich habe eine Klasse programmiert, die u.a. mehrere TBitmaps deklariert:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
Type TFirma = class
               private
                fCode:              String;     // Kurzname
                fLongName:          String;     // ausführliche Beschreibung
                fLaufwerk:          String;      // auf diesem Laufwerk sind die Daten gemappt
                fLogoName:          String;     // Name des Firmenlogos
                fMarkenLogoName:    String;     // Name des zusätzlichen Markenlogos
                fPromotionLogoName: String;     // Name des Promotion-Logos
                fLogo:              TBitmap;
                fMarkenLogo:        TBitmap;
                fPromotionLogo:     TBitmap;
                fFirmaNr:           ShortInt;
               public
                Function  Init:          Boolean;
                Function  Dispose:       Boolean;
                Function  Lesen:         Boolean;
                Function  Schreiben:     Boolean;
                Function  Wechseln(fnr:  ShortInt):  Boolean; overload;
                function  Wechseln:      Boolean; overload;
               published 
                Property  FirmaNr:       ShortInt read fFirmaNr;
                Property  Code:          String read fCode;
                Property  Name:          String read fLongName;
                Property  Laufwerk:      String read fLaufwerk;
                Property  Logo:          TBitmap read fLogo;
                Property  MarkenLogo:    TBitmap read fMarkenLogo;
                Property  PromotionLogo: TBitmap read fPromotionLogo;
              end;

Function TFirma.Init: Boolean;
begin
 Result := True;
 Firma := TFirma.Create;
 Firma.fFirmaNr := -1;   // Initialisierung
 try
  firma.fLogo := TBitmap.Create;
  Firma.fMarkenLogo := TBitmap.Create;
  Firma.fPromotionLogo := TBitmap.Create;
 except
  Result := False;
 end;
end;

function TFirma.Lesen: Boolean;
const Modul: String = 'Firma.Lesen';
 var IniFileApplication: TiniFile;
     b:  TBitmap;
     fn: String;
 begin
  Result := True;
...
  try
    Firma.fLogo := nil;
    Firma.fMarkenLogo  := nil;
    Firma.fPromotionLogo := nil;
    b := TBitmap.Create;
// Firmenlogo laden
    try
     fn := IniLaufwerk+':\Data\Firma_'+IntToStr(Firma.FirmaNr)+'\'+Firma.fLogoName;
     b.LoadFromFile(fn);
     Firma.fLogo := b;
    except
     Assert(false, 'Firmenlogo kann nicht geladen werden: '+fn);
     Firma.fLogo := nil;
    end;
...
end;


Firma.Init und Firma.Laden gehen wundervoll.
Sobald ich aber auf eine Grafik zugreifen will (z.B.
ausblenden Delphi-Quelltext
1:
if (not Firma.Logo.Empty then ...)					


erhalte ich eine access violation.

Was mache ich falsch?
wazap
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 25



BeitragVerfasst: Do 26.03.09 21:56 
Hast du die Klasse vorher erstellt bevor du auf sie zugegriefen hast?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
FFirmenLogo := TBitmap.Create;
...

if (Firma.Logo.Empty)...
GlasshouseManor Threadstarter
Hält's aus hier
Beiträge: 10


Delphi Enterprise
BeitragVerfasst: Do 26.03.09 21:58 
Hab' ich (siehe Zeile 36)
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Do 26.03.09 22:34 
Äh.. ja.. mag sein dass ich mich irre, aber ich schreib mal meine Theorie auf:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
function TFirma.Lesen: Boolean;
const Modul: String = 'Firma.Lesen';
 var IniFileApplication: TiniFile;
     b:  TBitmap;
     fn: String;
 begin
  Result := True;
...
  try
    Firma.fLogo := nil;  // Erstmal schmeißen wir flogo, bzw. was da vorher created wurde weg -> schöönes Speicherloch.
    Firma.fMarkenLogo  := nil;
    Firma.fPromotionLogo := nil;
    b := TBitmap.Create;
// Firmenlogo laden
    try
     fn := IniLaufwerk+':\Data\Firma_'+IntToStr(Firma.FirmaNr)+'\'+Firma.fLogoName;
     b.LoadFromFile(fn);  // Bild in lokale Variable "b" laden
     Firma.fLogo := b;  // Pointer von lokaler Variablen "b" an FLogo zuweisen.
    except
     Assert(false, 'Firmenlogo kann nicht geladen werden: '+fn);
     Firma.fLogo := nil;
    end;
...
end; // Prozedur beendet - von nun an ist "b" und somit auch FLogo nicht mehr gültig (und du hast gleichzeitig noch ein Speicherleck)


Wie gesagt.. ist nur ne Theorie. Versuch mal, das Logo direkt in FLogo zu laden, nicht den Umweg über die lokale Variable.

_________________
PROGRAMMER: A device for converting coffee into software.
GlasshouseManor Threadstarter
Hält's aus hier
Beiträge: 10


Delphi Enterprise
BeitragVerfasst: Do 26.03.09 22:47 
WUNDERBAR!!!

Ich bin Dir was schuldig!