Autor Beitrag
NeWsOfTzzz
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 16:00 
das problem ist folgendes: ich habe edit felder und die werden mit pchar umgewandelt
beispiel:
ausblenden Delphi-Quelltext
1:
pchar(edit.text);					

wenn in diesen edit feldern jetzt text ist, der irgendeine funktion/variable von delphi enthält gibt es pointer fehler :(
also funktionierende beispiele:
ausblenden Delphi-Quelltext
1:
2:
edit.text:='beispieltext';
edit.text:='abcdef';

nicht funktionierende:
ausblenden Delphi-Quelltext
1:
2:
edit.text:='TSTRINGLIST1');
edit.text:='ACCOUNT1');

was soll das denn? ich versteh diese fehler nicht..

wobei ich noch sagen muss, dass das bestimmten regeln folgen muss. vor oder nach dem wort (z.b. TSTRINGLIST) darf nur ein buchstabe/zahl stehen und wenn man das wort ganz alleine eingibt also nur TSTRINGLIST passiert auch nix
ps: ich benutze delphi 4

Moderiert von user profile iconraziel: Beiträge zusammengefasst.
Moderiert von user profile iconMotzi: Delphi-Tags hinzugefügt.
NeWsOfTzzz Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 18:50 
mein gott ihr mods seid aber kleinlich
:P
aber ne lösung wisst ihr wohl auch nicht? hier mal ein quelltext so dass jeder den fehler replizieren kann:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure provoke_bug;
var
s: string;
p: pchar;
begin
s:='tstringlist1';
p:=pchar(s);
end;
Radioactive
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: Mo 28.02.05 18:57 
Ich kann den Fehler nicht nachvollziehen. Wo kommt der und was für einer?

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG
NeWsOfTzzz Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 18:59 
ICH KANN DEN FEHLER AUCH NICHT NACHVOLLZIEHEN !!! *HEUL*
der kommt bei
ausblenden Delphi-Quelltext
1:
p:=pchar(s);					

und tritt fast immer auf wenn im string der name einer delphi komponente steht..
Radioactive
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: Mo 28.02.05 19:01 
Wie lautet die genaue Fehlerbeschreibung, die unten steht?

Also du weißt, dass so etwas nicht geht:
ausblenden Delphi-Quelltext
1:
p := PChar(Edit1.Text := 'ACCOUNT'); // p ist ein PChar					

sondern so:
ausblenden Delphi-Quelltext
1:
2:
Edit1.Text := 'ACCOUNT';
p := PChar(Edit1.Text);

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG


Zuletzt bearbeitet von Radioactive am Mo 28.02.05 19:09, insgesamt 1-mal bearbeitet
NeWsOfTzzz Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 19:08 
das ist kein kompilierfehler, das ist ein laufzeit fehler. und naja ich hab nochwas vergessen, nämlich getmem und freemem: hiermit lässt sich der fehler auf jedenfall replizieren(weil ich´s gerade selber kompiliert habe, das andere war nur frei hand geschrieben):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;  
p: pchar;  
begin  
s:='tstringlist1';
getmem(p,length(s));
p:=pchar(s);
freemem(p);
end;

PS: der fehler liegt wohl gar nicht am p:=pchar(s); sondern am freemem(p)..


Zuletzt bearbeitet von NeWsOfTzzz am Mo 28.02.05 19:10, insgesamt 1-mal bearbeitet
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mo 28.02.05 19:09 
Ich glaub du hast noch nicht ganz alles gepostet, das mit dem Fehler zusammenhängt... Was machst du nachher mit dem PChar?
//Edit: Den Post gerade drüber hatte ich natürlich noch nicht gesehn! Das da oben geht natürlich nicht!


Zuletzt bearbeitet von delfiphan am Mo 28.02.05 19:19, insgesamt 1-mal bearbeitet
Radioactive
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: Mo 28.02.05 19:11 
das gleube ich auch.
Du brauchst doch gar kein GetMem/FreeMem nur um einen String in einen PChar umzuwandeln.

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG
NeWsOfTzzz Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 19:11 
doch das ist exakt alles was ich mache, aber der fehler tritt nicht beim p:=pchar(s); auf sondern beim freemem(p);
klar brauch ich getmem und freemem -.-
pchar ist eine zeigerklasse -.-
Radioactive
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: Mo 28.02.05 19:12 
dann spar dir das ganze mit FreeMem/GetMem. Ich habe es bei keiner einzigen Anwendung gebraucht. Es klappt ohne das wunderbar.

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG


Zuletzt bearbeitet von Radioactive am Mo 28.02.05 19:15, insgesamt 1-mal bearbeitet
NeWsOfTzzz Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 19:14 
das stimmt doch gar nicht! was erzählst du denn da?
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: Mo 28.02.05 19:15 
Hallo,

es kommt immer drauf an, was du mit dem PChar machen willst. Wenn du einen String in einen PCHar umwandeln möchtest, dann hilft ein einfacher Typcast. Ansonsten kannst du es mal so versuchen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
var
  S: String;
  Size: Integer;
  P: PChar;
begin
  S := 'testeg4 t34tsf';
  Size := Length(S) + 1;
  GetMem(P, Size);
  StrPCopy(P, S);
  FreeMem(P, Size);
end;
Gruß Hape!

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
Radioactive
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: Mo 28.02.05 19:17 
Also ich habe keine Probleme, wenn ich folgendes habe:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
var s: String;

begin
  s := 'Hallo!';
  Application.MessageBox(PChar(s),'Titel',0+64);
end;

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG
NeWsOfTzzz Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 19:20 
Also es geht darum: ich entwickle ein mmorpg und an mehreren stellen kann man eingaben machen, das läuft auch soweit alles mit dem system welches ich benutze.. mehr oder minder durch zufall ist mir dann letztens aufgefallen dass es bei der eingabe account1 abstürzt :( und dass der fehler halt daran lag, dass account die bezeichnung für eine delphi komponente ist. ich kopier mal den quelltext so dass man sieht, wofür ich pchar brauche und dass es nicht ohne getmem, freemem geht und warum ich length und nicht length+1 nehme (die nullterminierung ist bei mir ohne bedeutung):
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:
25:
procedure TForm1.CreateCharacter;
var
  Len: word;
  MSGType: word;
  MSGSize: longword;
  MSG: pchar;
  sndBytes: ^TByte;
  x: longword;
  s: string;
Begin
Len:=Length(CreateCharacterForm.ENewCharacterName.text);
if Len = 0 then begin showmessage('No new name entered.'); exit; end;
if (Len = 1or (Len = 2then begin showmessage('New name is too short!'); exit; end;
MSGSize := (2+Len);
getmem(sndBytes,MSGSize);
getmem(MSG,len);
MSGType:=7;
Move(msgType,sndBytes^[0],2);
StrLCopy(MSG,PChar(CreateCharacterForm.ENewCharacterName.text),Len);
for x:=0 to len-1 do
  sndBytes^[2+x]:=ord(MSG[x]);
ClientSocket.Socket.SendBuf(sndBytes^,MSGsize);
freemem(sndBytes);
freemem(MSG);
end;


@Radioactive
wenn der string einen beliebigen wert hat, funktioniert auch alles fehlerfrei.. abstürzen tut´s nur wenn der name einer delphi komponente vorkommt im string


Zuletzt bearbeitet von NeWsOfTzzz am Mo 28.02.05 19:27, insgesamt 3-mal bearbeitet
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mo 28.02.05 19:20 
Damit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
Procedure sowieso;
var
 p: PChar;
begin
 p := PChar('hallo');
 //...
end;

erstellst du ein PChar, welches beim Verlassen der Prozedur gelöscht wird. Du brauchst kein Freemem.


Zuletzt bearbeitet von delfiphan am Mo 28.02.05 19:22, insgesamt 1-mal bearbeitet
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: Mo 28.02.05 19:21 
Radioactive hat folgendes geschrieben:
Also ich habe keine Probleme, wenn ich folgendes habe:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
var s: String;

begin
  s := 'Hallo!';
  Application.MessageBox(PChar(s),'Titel',0+64);
end;
Speicher doch einfach mal einen String in einem Zeiger und gib ihn wieder frei. Dann weisst du, was er meint.

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
NeWsOfTzzz Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 19:23 
meinen quellcode von vorhin läuft fehlerfrei wie gesagt, der fehler tritt nur auf wenn in dem string der name einer delphi komponente vorkommt, also zum beispiel s:='tstringlist1';
wenn ich s:='hallo'; nehme, funzt alles fehlerfrei
Radioactive
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: Mo 28.02.05 19:26 
Gut, da bin ich jetzt ratlos, in meinen PChars sind noch nie Namen von Komponenten vorgekommen. Muss ja ein seltsames Prog sein.

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mo 28.02.05 19:26 
Dein Len ist doch eins zu klein, PChar ist nullterminiert und muss um 1 länger sein als ein äquivalenter Pascal String.
NeWsOfTzzz Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 233


D4 Prof
BeitragVerfasst: Mo 28.02.05 19:29 
nein es muss nicht einen länger sein, weil ich auch so schon alle buchstaben habe. aber um das auszuschliessen hab ich das mal getestet aber der fehler besteht nach wie vor...
ausserdem kommen in meinen pchars keine namen von komponenten vor, das sind die eingaben der benutzer..