Thread View: alt.comp.lang.borland-delphi
1 messages
1 total messages
Started by Terry
Wed, 07 Jul 2004 04:36
PChar in a user defined type access violation problem
Author: Terry
Date: Wed, 07 Jul 2004 04:36
Date: Wed, 07 Jul 2004 04:36
71 lines
2799 bytes
2799 bytes
Hello,
I am having some difficulty with my code. I am attempting to use a
PChar in a class I have previously defined, but I get an Access
Violation during the StrAlloc() function for my class's PChar.
Any help with this problem would be appreciated!
Code follows with some comments:
-----------------------------------------------------------
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
// my user defined class that is the troublemaker
tDefinition = class
text : pchar;
moreText : pchar;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
DataStream : TMemoryStream;
S:string;
testData, testData2 : tDefinition;
s1, s2 : PChar;
begin
DataStream := TMemoryStream.Create;
try
// read the data from the file on the hard drive
// and store it in a String
DataStream.LoadFromFile('c:\sample.htm');
SetLength(S, DataStream.Size);
// store the file into a string memory buffer
Move(DataStream.Memory^, S[1], DataStream.Size);
// this allocation to a PChar works fine
s1 := StrAlloc( DataStream.Size );
s1 := StrPCopy(s1,S);
// this allocation to a PChar also works fine
s2 := StrAlloc( DataStream.Size );
s2 := StrPCopy(s2,S);
// this allocation to a user defined PChar works sometimes
testData.text := StrAlloc( DataStream.Size );
testData.text := StrPCopy( testData.text,S);
// this second one always fails with an Access Violation
testData2.text := StrAlloc( DataStream.Size );
testData2.text := StrPCopy( testData2.text,S);
finally
DataStream.Free;
end;
end;
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads