ios - 由于流中的优化,值暂时不可用
<p><p>我最近开始使用发布版本测试我的应用程序(管理方案并设置从调试到发布的运行)。我注意到的是,有时我会在流方法中弹出以下错误,我似乎无法找到找到错误的方法。这在 Debug模式下完美运行,但在发布版本中,除了 SIGABRT 消息之外,我没有收到任何通知。我也不知道如何检查模拟器上的崩溃日志以查看问题所在。下面我附上了崩溃的堆栈跟踪:</p>
<pre><code>#00x918749c6 in __pthread_kill ()
#10x916c0f78 in pthread_kill ()
#20x916b1ce3 in __abort ()
#30x916ae64a in __stack_chk_fail ()
#40x00006d0d in - (_cmd=0x9ceb9, msg=0xdfd7004 "Login="User" Pass="Pass" Id="1234" PlayerId="345" Location="12,35" Color="Red" PlayerId="65" Location="180,0" Color="Blue" PlayerId="29" Location="0,200" Color="..., len=333295) at /Users/seb/Desktop/Tutorials/Networking/MainViewController.m:850
#50x000044b3 in - (_cmd=0x17a0410, stream=<value temporarily unavailable, due to optimizations>, eventCode=<value temporarily unavailable, due to optimizations>) at /Users/seb/Desktop/Tutorials/Networking/MainViewController.m:260
#60x01716501 in _inputStreamCallbackFunc ()
#70x016e606d in _signalEventSync ()
#80x016e67ca in _cfstream_solo_signalEventSync ()
#90x016e5e71 in _CFStreamSignalEvent ()
#10 0x016e6727 in CFReadStreamSignalEvent ()
#11 0x020083ad in SocketStream::dispatchSignalFromSocketCallbackUnlocked ()
#12 0x01f64191 in SocketStream::socketCallback ()
#13 0x01f640a1 in SocketStream::_SocketCallBack_stream ()
#14 0x016b3e44 in __CFSocketPerformV0 ()
#15 0x0171997f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#16 0x0167cb73 in __CFRunLoopDoSources0 ()
#17 0x0167c454 in __CFRunLoopRun ()
#18 0x0167bdb4 in CFRunLoopRunSpecific ()
#19 0x0167bccb in CFRunLoopRunInMode ()
#20 0x0226f879 in GSEventRunModal ()
#21 0x0226f93e in GSEventRun ()
#22 0x0066aa9b in UIApplicationMain ()
#23 0x0000201b in main (argc=1, argv=0xbffff5e0) at /Users/seb/Desktop/Tutorials/Networking/main.m:14
</code></pre>
<p>stream:handleevent 中发生崩溃的案例语句:</p>
<pre><code>case NSStreamEventHasBytesAvailable:
{
if (stream == inputStream)
{
NSLog(@"NSStreamEventHasBytesAvailable");
uint8_t buffer;
unsigned int len = 0;
NSInputStream* inputstream = (NSInputStream*)stream;
len = ;
// Check if our stream is still valid
if( != nil)
{
//We lost our connection to the host
NSError *theError = ;
[self setConnectError:[NSString stringWithFormat:@"Error %i: %@",
, ]];
break;
}
;
int processedBytes = 0;
while (true)
{
if ( >= processedBytes + sizeof(uint32_t))
{
const char* bufferstart = ((const char*)) + processedBytes;
uint32_t sz_n;
memcpy(&sz_n, bufferstart, sizeof(sz_n));
uint32_t sz_h = htonl(sz_n);
if ( >= processedBytes + sz_h + sizeof(uint32_t))
{
;
processedBytes += sz_h + sizeof(uint32_t);
}
else
{
break;
}
}
else
{
break;
}
}
if (processedBytes)
{
if (processedBytes < )
{
NSMutableData *newdata = + processedBytes length: - processedBytes];
;
}
else
{
;
}
}
}
break;
}
</code></pre>
<p>processMessage 方法:</p>
<pre><code>- (void)processMessage:(const char*)msg len:(int)len
{
NSLog(@"processMessage start (%d)", len);
{
char buffer;
memcpy(buffer, msg, len);
buffer = '\0';
NSLog(@"%s", buffer);
}
NSLog(@"processMessage end");
}
</code></pre>
<p>非常感谢任何建议。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您已通过在 <code>len + 1</code> 边界之外分配空终止符而使 <code>缓冲区</code> 溢出而损坏了堆栈。正确的代码是:</p>
<pre><code> char buffer;
memcpy(buffer, msg, len);
buffer = '\0';
NSLog(@"%s", buffer);
</code></pre>
<p>您所做的是未定义的行为,并且将未定义的行为与针对 Release模式进行的优化相结合很可能是您仅在 Release模式下看到此问题的原因。</p></p>
<p style="font-size: 20px;">关于ios - 由于流中的优化,值暂时不可用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/10109517/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/10109517/
</a>
</p>
页:
[1]