Delphi 自带的JSON内存释放问题

helodd 2016-07-22 11:33:46
使用了 小飞鱼的例子( http://blog.csdn.net/gzxiaorou/article/details/44538193)解释JSON,在没有释放内存的情况不出现问题, 但在解释完释放变量,即出现了错误的指针。

unit Unit2;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.json;

type
TForm2 = class(TForm)
mmo1: TMemo;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
JSONObject: TJSONObject;
LItem: TJSONValue;
LJPair: TJSONPair;
weather: TJSONArray;
StrJson: String;
result: String;
i: Integer;
begin
StrJson := mmo1.text;//假定是上面那个json
JSONObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson), 0)
as TJSONObject;
JSONObject := (JSONObject.GetValue('results') as TJSONArray).Get(0)
as TJSONObject;
weather := JSONObject.GetValue('weather_data') as TJSONArray;

for i := 0 to weather.size - 1 do //应该是4条记录
begin
LItem := (weather.Get(i) as TJSONObject).GetValue('weather'); //得到weather的值
result := result + '|' + LItem.Value;
end;



Memo1.Text:= result;
weather.Free;
JSONObject.Free;
// 加上这个, 会出现内存错误, 请专家帮忙解释一下.



end;

end.



只有json 数据带有数组的情况会出错,没有数组的不出错 。 这个是system.json的一个BUG吗?


...全文
1326 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyhoo163 2016-07-24
  • 打赏
  • 举报
回复 1

procedure TForm2.Button1Click(Sender: TObject);
var
  JSONObject: TJSONObject;
  LItem: TJSONValue;
  LJPair: TJSONPair;
  weather: TJSONArray;
  StrJson: String;
  result: String;
  i: Integer;
begin
  JSONObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson), 0) as TJSONObject;  // 创建JSONObject
  JSONObject := (JSONObject.GetValue('results') as TJSONArray).Get(0) as TJSONObject;            // 指向JSONObject 
  weather := JSONObject.GetValue('weather_data') as TJSONArray;                                  // 指向JSONObject 
  ......
  weather.Free;        //  weather 指向JSONObject 释放了JSONObject
  JSONObject.Free;     //  前一句已释放,再次释放出现内存错误
                       //  前二句只能用一个,建议删除weather.Free; 
end;
helodd 2016-07-24
  • 打赏
  • 举报
回复
引用 5 楼 jankercsdn 的回复:
[quote=引用 4 楼 helodd 的回复:] 打开内存报告,还是有内存泄漏的。 不知这个要怎么整。。
如果这个泄露是固定的几个字节,我一般是不管的。只要不是自己写的代码造成的,查起来麻烦,改起来更麻烦。[/quote] 不要小看那几个字节,如果用到是你的数据,可能就不是几个字节了,还是我代码是想要循环去获取数据的, 时间久了, 泄漏的内存很快就程序out of momey 了。。 所以这个要想办法解决。。 weather.Free; // weather 指向JSONObject 释放了JSONObject JSONObject.Free; // 前一句已释放,再次释放出现内存错误 // 前二句只能用一个,建议删除weather.Free; 无论释放哪一个, 最终内存报告,都是有泄漏的。
看那山瞧那水 2016-07-23
  • 打赏
  • 举报
回复
引用 4 楼 helodd 的回复:
打开内存报告,还是有内存泄漏的。 不知这个要怎么整。。
如果这个泄露是固定的几个字节,我一般是不管的。只要不是自己写的代码造成的,查起来麻烦,改起来更麻烦。
helodd 2016-07-23
  • 打赏
  • 举报
回复
打开内存报告,还是有内存泄漏的。 不知这个要怎么整。。





helodd 2016-07-22
  • 打赏
  • 举报
回复
{ "error":0, "status":"success", "date":"2014-03-04", "results": [{"currentCity":"成都", "weather_data":[ { "date":"周二(今天, 实时:12℃)", "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png", "nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png", "weather":"多云", "wind":"北风微风", "temperature":"15 ~ 6℃" }, { "date":"周三", "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png", "nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png", "weather":"阴转小雨", "wind":"北风微风", "temperature":"14 ~ 7℃" }, { "date":"周四", "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png", "nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png", "weather":"小雨", "wind":"北风微风", "temperature":"12 ~ 7℃" }, { "date":"周五", "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png", "nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png", "weather":"小雨", "wind":"南风微风", "temperature":"9 ~ 6℃" } ] } ]} //JSON 内容用这个。
  • 打赏
  • 举报
回复
同上 同上 同上
看那山瞧那水 2016-07-22
  • 打赏
  • 举报
回复
// weather.Free; //千万不要释放其内部对象,它的父对象会自动释放它 JSONObject.Free; // 现在可以了 参考万老师的博客 http://www.cnblogs.com/del/p/4225871.html

1,594

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧