Home > Back-end >  TServerMethods1Client object is released prompt invalid pointer operation error
TServerMethods1Client object is released prompt invalid pointer operation error

Time:10-12

Write a small program of DATASNAP propagation, prompt invalid when casting TServerMethods1Client object pointer operation error, to obtain each tall person to give directions,
Procedure TForm12. ListView1Click (Sender: TObject);
Const
BufferSize=102400;//100 KB
Var
FStream: TFilestream;
Stream: Tstream;
BytesReadCount: Integer;
Buffer: TBytes;
Filename: the string;
Cs: TServerMethods1Client;
The begin
If ListView1. ItemIndex=1 then the exit;
Image1. Picture. Graphic:=nil;
Cs:=TServerMethods1Client. Create (dm1. SQLConnection1. DBXConnection);
Filename:='C: \ TEMP \ TEMP. JPG';
Then if SysUtils. FileExists (FileName)
FStream:=TFilestream. Create (FileName, fmOpenWrite or fmShareDenyNone)
The else fStream:=TFilestream. Create (FileName, fmCreate);
Stream:=cs DownLoadFile (ClientDataSet2 FieldByName (' storage directory). AsString + ListView1. Items [ListView1. ItemIndex]. Caption);
Try
SetLength (Buffer, BufferSize);
FStream. Position:=0;
Repeat
BytesReadCount:=stream. Read (Buffer [0], BufferSize);
If BytesReadCount & gt; 0 then
FStream. Write (Buffer [0], BytesReadCount);
Until BytesReadCount & lt; BufferSize;
The finally
//cs. Destroy;
//cs. Free;//this two statements indicate invalid pointer errors
FStream. Free;
Stream. Free;
end;
end;

CodePudding user response:

Free interior is invoked destroy (self & lt;> Nil)

CodePudding user response:

Don't Free CS, instead of CS:=nil;

CodePudding user response:

Do not safe, upstairs in a destructor function statement released child object, the object directly assigned to nil can lead to child objects take up memory leaks, with the most safe, free, I think the building Lord failure may be the cause of CS internal problem,,

CodePudding user response:

reference HSFZXJY reply: 3/f
do not safe, upstairs in a destructor function statement released child object, the object directly assigned to nil can lead to child objects take up memory leaks, with the most safe, free, I think the building Lord failure may be the cause of CS internal problem,,


Some classes are inherited from interface, the interface will release,

CodePudding user response:

Alleged child objects, it is in the object is created in the create function of some auxiliary object, such as the TImage created in the create a Picture type TPicture object, called the child object, the object's destructor in the TImage only. In the Free, so if you don't call TImage. Free, Picture object is never destructor!

CodePudding user response:



FreeAndNil (CS);

CodePudding user response:

thank you for your enthusiastic, the above methods are tried, still won't do, execute to the release of CS play invalid pointer operation error

CodePudding user response:

Check the cs related code,

CodePudding user response:

The name TServerMethods1Client how so strange? There is a 1,,,

CodePudding user response:

Today I also encountered this problem, Internet, only with your post to this problem, although the Posting time for a long time, but I posted the code now, for you to analyse, find out why want to share it!
Var
Aserver: TServerMethodsClient;
Cm: TClientModule;
Ja: TJSONArray;
The begin
Cm:=TClientModule. Create (nil);
Try
Aserver:=cm. ServerMethodsClient;
Ja:=TJSONArray. Create;
Try
Ja:=aserver. GetTestJSONArray (' 123 ', '123');
ShowMessage (ja. ToString);
The finally
//ja. Free;//as long as join the execution to cm below. When I was Free to "invalid pointer operation" error
end;
The finally
Cm. Free;
end;
end;

CodePudding user response:

Seemingly cm. When free, ja. Be free;
Ja cm child objects?

CodePudding user response:

I met this problem, also seems to be no solution? Can only use the try the except end solution, in fact, there should be no release,

CodePudding user response:



There are more than a phrase "method of Boolean parameters,
I also met before, after a long time, can't remember what happened,

Cs:=TServerMethods1Client. Create (dm1. SQLConnection1. DBXConnection, True/FALSE).

Try. Anyway the impression of this parameter is sometimes work, just as reference,
  • Related