V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  YsHaNg  ›  全部回复第 2 页 / 共 40 页
回复总数  800
1  2  3  4  5  6  7  8  9  10 ... 40  
TL;DR 扔给 GPT 了
Yes, there are several memory leaks in the provided code. Let's go through them:

1. In the loop where you receive messages via the socket, you allocate memory for `receiveData` using `cJSON_Print`, but you don't free it. You should free it after you're done using it.

2. Similarly, you create a `cJSON` object using `cJSON_Parse`, but you don't free it using `cJSON_Delete`.

3. You allocate memory for `json_str` using `cJSON_Print`, but you don't free it after sending it over the socket.

4. You allocate memory for `input` and `output` inside the loop, but you don't free them before the next iteration of the loop. This can lead to memory leaks if `input` or `output` fail to initialize.

To fix these memory leaks:

- Free `receiveData` after its use with `SAFE_DELETE(receiveData)`.

- Delete the `cjson_receive` object using `cJSON_Delete` after its use.

- Free `json_str` after sending it over the socket.

- Delete `input` and `output` objects at the end of each iteration of the loop before reinitializing them.

Here's how you can do it:

```cpp
SAFE_DELETE(receiveData);
cJSON_Delete(cjson_receive);
SAFE_DELETE(json_str);
SAFE_DELETE(input);
SAFE_DELETE(output);
```

Place these lines at appropriate locations in your code to ensure memory allocated dynamically is properly deallocated.
31 天前
回复了 Dandelion2333 创建的主题 长沙 大龄长沙女生找对象
@hanlin85 年薪 40 还在长沙那不就是减分项了
31 天前
回复了 Dandelion2333 创建的主题 长沙 大龄长沙女生找对象
发现期待对象没有限定性别
34 天前
回复了 ttentau1 创建的主题 分享发现 分享一下我是怎么使用平板看油管的
@efcndi 浏览器插件去广告
1.专门的维护岗 开发 deploy 完了就不再负责 2.轮岗 oncall 轮到的同学发额外一天 oncall 钱 没事基础有事翻个番 自然有人抢着要
36 天前
回复了 wsg123 创建的主题 求职 27 岁的 Android 开发找工作也好难
你又不肯润 在这既要又要
迪士尼影业的人有一年 4 次带朋友玩任意乐园
@moudy 你可以一切都远程 ssh 上去
38 天前
回复了 wtf12138 创建的主题 iPad 请教一下 iPad 的真实使用场景
ios 上有 Termius
38 天前
回复了 zhangcna 创建的主题 问与答 你们手里有多少闲置的手机了
@Seria 现在都激活默认全盘加密
38 天前
回复了 zhangcna 创建的主题 问与答 你们手里有多少闲置的手机了
@zhangcna iPhone 换机倒是一个不差连 esim 都直接传过去 一般换新机当场给小天才抵价
46 天前
回复了 yoshiaki 创建的主题 Android iphone14 转小米 14pro 谈一下感受
贵 亚麻上 Xiaomi 14 12+512GB 卖£870
只装 gameready 驱动 现在 cuda 直接 wsl 里装
50 天前
回复了 beriru 创建的主题 京东 京东自营买到豆腐脑一样的安佳牛奶
我竟然没看懂洋洋洒洒你们一大群人在说啥。。。是 v2er 的问题还是记我的问题
@jurassic2long 很多 v2er 贴子就这样吧 为了以后能卖课
我只是嫌欧洲贵
54 天前
回复了 qinfengge 创建的主题 Windows 微软到底在干什么啊
@frankilla 欧洲也跳这个
58 天前
回复了 SimleCp 创建的主题 问与答 各位洗碗是用洗洁精还是不用?
@sanwang566123 找有洗碗机的房租啊
1  2  3  4  5  6  7  8  9  10 ... 40  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2431 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 40ms · UTC 03:20 · PVG 11:20 · LAX 20:20 · JFK 23:20
Developed with CodeLauncher
♥ Do have faith in what you're doing.