无法修复 iPad 应用程序中的内存泄漏

发布于 2024-11-03 10:32:36 字数 1467 浏览 6 评论 0原文

我已经使用 Instruments 来定位应用程序中的内存泄漏,但我似乎无法修复它或找到解决方法;这是我的第一个应用程序,也是我第一次涉足 Objective-C,所以我可能正在做一些相当愚蠢的事情。有人可以帮忙吗?我猜 NSString 对象“str”没有被释放,但是我如何释放它然后返回它?

这是源代码:

- (NSString *)MakeMsg:(uint8_t)slaveAddr FunctionCode:(uint8_t)functionCode StartReg: (uint16_t)startReg Range:(uint16_t)range {

NSString *str;

uint8_t CRCbyte1;
uint8_t CRCbyte2;
uint8_t buf[MODBUS_MSG_LEN_BYTES+LRC_BYTE+ASCII_WRAPPER];
uint8_t ASCIIbuf[(MODBUS_MSG_LEN_BYTES*2)+LRC_BYTE+ASCII_WRAPPER];  

buf[0] = slaveAddr;
buf[1] = functionCode;
buf[2] = (uint8_t)(startReg >> 8);
buf[3] = (uint8_t)(startReg & 0xFF);
buf[4] = (uint8_t)(range >> 8);
buf[5] = (uint8_t)(range & 0xFF);

if (RTUMode==YES){
    // calculate the CRC bytes 
    [self GenerateCRC16:buf CRC1:&CRCbyte1 CRC2:&CRCbyte2];
    buf[6] = CRCbyte1;
    buf[7] = CRCbyte2;
}

if (ASCIIMode==YES){
    // calculate the LRC byte
    [self GenerateLRC:buf Length:(uint8_t)MODBUS_MSG_LEN_BYTES ASCIIBuffer:ASCIIbuf];

    // convert the buffer to ASCII
    [self BufToASCII:buf ASCIIBuffer:ASCIIbuf];

    // add the ASCII wrapper ':',buf,'CR','LF'
    [self AddASCIIWrapper:buf ASCIIBuffer:ASCIIbuf];
}

str = [NSString stringWithUTF8String:(const char *)ASCIIbuf]; // <-- Memory leak identified as this line right here by Instruments
return str;// Return the string value of our command so we can use it in a comms log display.   

}

任何帮助将不胜感激! 谢谢 马修

I've used Instruments to locate the memory leak in my app but I just can't seem to fix it or find a workaround; it's my first app and first foray into Objective-C so I'm probably doing something fairly dumb. Can anyone help? I'm guessing the NSString object "str" isn't being released but how do I release it and then return it?

Here's the source code:

- (NSString *)MakeMsg:(uint8_t)slaveAddr FunctionCode:(uint8_t)functionCode StartReg: (uint16_t)startReg Range:(uint16_t)range {

NSString *str;

uint8_t CRCbyte1;
uint8_t CRCbyte2;
uint8_t buf[MODBUS_MSG_LEN_BYTES+LRC_BYTE+ASCII_WRAPPER];
uint8_t ASCIIbuf[(MODBUS_MSG_LEN_BYTES*2)+LRC_BYTE+ASCII_WRAPPER];  

buf[0] = slaveAddr;
buf[1] = functionCode;
buf[2] = (uint8_t)(startReg >> 8);
buf[3] = (uint8_t)(startReg & 0xFF);
buf[4] = (uint8_t)(range >> 8);
buf[5] = (uint8_t)(range & 0xFF);

if (RTUMode==YES){
    // calculate the CRC bytes 
    [self GenerateCRC16:buf CRC1:&CRCbyte1 CRC2:&CRCbyte2];
    buf[6] = CRCbyte1;
    buf[7] = CRCbyte2;
}

if (ASCIIMode==YES){
    // calculate the LRC byte
    [self GenerateLRC:buf Length:(uint8_t)MODBUS_MSG_LEN_BYTES ASCIIBuffer:ASCIIbuf];

    // convert the buffer to ASCII
    [self BufToASCII:buf ASCIIBuffer:ASCIIbuf];

    // add the ASCII wrapper ':',buf,'CR','LF'
    [self AddASCIIWrapper:buf ASCIIBuffer:ASCIIbuf];
}

str = [NSString stringWithUTF8String:(const char *)ASCIIbuf]; // <-- Memory leak identified as this line right here by Instruments
return str;// Return the string value of our command so we can use it in a comms log display.   

}

Any help would be greatly appreciated!
Thanks
Matthew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一人独醉 2024-11-10 10:32:36

Instruments 只会告诉您泄漏对象是在哪里创建的。它无法告诉您在哪里未能释放它(这就是泄漏的原因),因为那可能是在任何地方。

最有可能的是,您将字符串分配给某处对象的属性,但无法在该对象的 -dealloc 中释放它。

Instruments only tells you where the leaked object was created. It can't tell you where you failed to release it (which is why the leak) because that could be anywhere.

Most likely you assign the string to a property of an object somewhere and fail to release it in that object's -dealloc.

终止放荡 2024-11-10 10:32:36

str 不是泄漏,或者至少它没有在您发布的代码中泄漏。由于您使用 +stringWithUTF8String: 创建字符串,因此您将返回一个自动释放的对象。返回该对象是安全的,并且调用者将能够使用它,但它最终会为您释放。如果调用者要保留对从 -MakeMSG 接收到的字符串的引用,则应该保留它。

str isn't a leak, or at least it's not being leaked in the code you posted. Since you create the string with +stringWithUTF8String:, you get back an autoreleased object. It's safe to return that object, and the caller will be able to use it, but it will eventually be released for you. If the caller is going to keep a reference to the string it receives from -MakeMSG, it should retain it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文