Application crashes while appending data on string - c++

I have a cross platform console application. The purpose of it is to transfer data between two peers. Sometimes packets received are not complete so we append the data into a string while the complete packet has been arrived. But it get crashed in appending data in string after sometime. Here is the back tarce -
1 libsystem_c.dylib 0x93b77acf pthread_kill + 101
2 libsystem_c.dylib 0x93bae4f8 abort + 168
3 libc++abi.dylib 0x9698180c abort_message + 151
4 libc++abi.dylib 0x9697f275 default_terminate() + 34
5 libc++abi.dylib 0x9697f2b5 safe_handler_caller(void (*)()) + 13
6 libc++abi.dylib 0x9697f31d std::terminate() + 23
7 libc++abi.dylib 0x96980412 __cxa_throw + 110
8 libstdc++.6.dylib 0x90e23d6c std::__throw_length_error(char const*) + 104
9 libstdc++.6.dylib 0x90e4f3a9 std::string::append(char const*, unsigned long) + 175
10 libConnector.dylib 0x13905228 ConnectionSocket::AdjustPartialData(char const*, int)
14 libConnector.dylib 0x1383e0b6 ConnectionChannel::ProcessData(int, void const*, int, char*, int) + 7886
15 libConnector.dylib 0x13861ecb ConnectionManager::BaseThreadImpl() + 1185
16 libConnector.dylib 0x13861a23 ConnectionManager::BaseThread(void*) + 17
17 libsystem_c.dylib 0x93b76557 _pthread_start + 344
18 libsystem_c.dylib 0x93b60cee thread_start + 34
Can someone give any idea about this issue?
EXAMPLE CODE:
AdjustPartialData(char* const pData, int dataLen)
{
if (true == packetIncomplete)
{
partialDataBuffer.append(pData, dataLen); // crash occurs in this line
}
}

if partialDataBuffer.size() + dataLen > partialDataBuffer.max_size() then append throws a length_error (http://en.cppreference.com/w/cpp/string/basic_string/append)
Either partialDataBuffer or dataLen are too big.

Finally I have resolved the issue by discarding the extra data which causes the overflow. Previously I thought this will not be feasible solution as it could lead to possible data loss. But I found that TCP stream size shouldn't be greater than 65535 which is 16 bit. But found another issue when the following condition added -
if(partialDataBuffer.size() + dataLen >= partialDataBuffer.max_size())
{
// do not append data
}
else partialDataBuffer.append(pData, dataLen);
The issue was in windows platform it always crashes even though size of partialDataBuffer is much less than partialDataBuffer.max_size(). So, for resolving this issue I have done the following -
try{
partialDataBuffer.append(pData, dataLen);
}
catch(...)
{
// got exception, return
}
And it worked fine for me.
N.B: I have posted my answer because it will be helpful for others who are facing similar issue.

Related

MAC audioUnit AudioComponentInstanceDispose crash

the audio unit subType is kAudioUnitSubType_VoiceProcessingIO,
and use kAudioOutputUnitProperty_CurrentDevice to set Device ID for the audio unit
OSStatus result = AudioOutputUnitStop(audio_unit);
result = AudioUnitUninitialize(audio_unit);
result = AudioComponentInstanceDispose(audio_unit);
Thread trace:
3 ExceptionHandling 0x00007fff3c64ef31 -[NSExceptionHandler _handleException:mask:] + 364
4 ExceptionHandling 0x00007fff3c64ecac NSExceptionHandlerUncaughtSignalHandler + 35
5 libsystem_platform.dylib 0x00007fff204d3d7d _sigtramp + 29
6 CoreFoundation 0x00007fff2050f381 CFStringGetLength + 11
7 CoreFoundation 0x00007fff205225c7 CFStringCompare + 24
8 CoreAudio 0x00007fff221e83f5 _ZN9HALDevice4DuckEfPK14AudioTimeStampf + 921
9 CoreAudio 0x00007fff21d3ed48 AudioDeviceDuck + 843
10 AudioDSP 0x00000001390d7d4a _Z14DuckOtherAudiojff + 51
11 AudioDSP 0x0000000139213e06 _ZN16AUVoiceProcessor22DestroyAggregateDeviceEv + 974
12 AudioDSP 0x0000000139215459 _ZN16AUVoiceProcessorD2Ev + 417
13 AudioDSP 0x00000001392efdec _ZN13ComponentBase8AP_CloseEPv + 30
14 AudioToolboxCore 0x00007fff219e4b6a AudioComponentInstanceDispose + 55
You are disposing of an Audio Instance before it is stopped.
Note that AudioOutputUnitStop may not stop an audio unit immediately, since the audio thread runs asynchronously. If you delay for a half second after stopping the audio unit, that other thread is likely stopped by then and then you can more safely dispose of its resources.

opencv c++ VideoCapture open() error

I'm trying to read a webcam stream in opencv c++ on OS X but when I attempt to open the feed I produce the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
Stacktrace:
*** First throw call stack:
(
0 CoreFoundation 0x00007fff86cfee7b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff9b8e8cad objc_exception_throw + 48
2 CoreFoundation 0x00007fff86c181dc -[__NSArrayM objectAtIndex:] + 204
3 libopencv_highgui.2.4.dylib 0x0000000104b09280 _ZN13CvCaptureFileC2EPKc + 350
4 libopencv_highgui.2.4.dylib 0x0000000104b07cf2 _Z32cvCreateFileCapture_AVFoundationPKc + 34
5 libopencv_highgui.2.4.dylib 0x0000000104afb7ee cvCreateFileCapture + 14
6 libopencv_highgui.2.4.dylib 0x0000000104afba9e _ZN2cv12VideoCapture4openERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 64
7 classification.bin 0x0000000104031842 main + 370
8 libdyld.dylib 0x00007fff9c1cc255 start + 1
9 ??? 0x0000000000000005 0x0 + 5
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
Code:
VideoCapture cap;
printf("vidcap created\n"); // This message is printed
cap.open("http://192.168.1.155:80/video.cgi?dummy=.mjpg");
printf("feed opened\n"); // this one is not
What could be the reason for this error?

Segfault taking element from QList

I get a SEGFAULT at the first line in the if-block, takeFirst().
if (!sendQueue.isEmpty()) {
QString nxtcmd = sendQueue.takeFirst();
port->write(nxtcmd.toLatin1());
port->flush();
}
I have code pushing 96 command strings into the queue with
if (completionHandlerQueue.empty()) {
…
} else {
sendQueue.append(cmd_str);
}
The latter happens in my main GUI code. The first bit is called by a signal by QExtSerialPort (when data is there to be read).
sendQueue is a member and declared as such:
class SB::ModuleCommunicator : public QObject
{
…
private:
…
QStringList sendQueue;
Now I'm not using threads and I assumed the Qt Event Loop would make everything work smoothly, but the full backtrace does tell me that there are in fact 3 threads.
According to documentation, Qt Containers are only thread-safe for read only access.
But the Queue is already full with all 96 strings when it crashes. So I don't think anything is appending while the list is being modified.
Is this a thread issue? How can I find out?
Here is the stacktrace:
0 _int_malloc malloc.c 3530 0x7ffff61c7410
1 __GI___libc_malloc malloc.c 2924 0x7ffff61c9f95
2 QListData::detach(int) /usr/lib/x86_64-linux-gnu/libQtCore.so.4 0 0x7ffff6cd397b
3 QList<QString>::detach_helper qlist.h 709 0x412169
4 QList<QString>::detach_helper qlist.h 725 0x411cf2
5 QList<QString>::detach qlist.h 139 0x411e72
6 QList<QString>::begin qlist.h 267 0x423610
7 QList<QString>::first qlist.h 282 0x422dda
8 QList<QString>::takeFirst qlist.h 490 0x422885
9 SB::ModuleCommunicator::handleMsg ModuleCommunicator.cpp 116 0x41f9be
10 SB::ModuleCommunicator::onReadyRead ModuleCommunicator.cpp 393 0x421f4d
11 SB::ModuleCommunicator::qt_static_metacall moc_ModuleCommunicator.cpp 52 0x435e1e
12 QMetaObject::activate(QObject*, QMetaObject const*, int, void**) /usr/lib/x86_64-linux-gnu/libQtCore.so.4 0 0x7ffff6dc9281
13 QextSerialPortPrivate::_q_canRead qextserialport.cpp 313 0x40c505
14 QextSerialPort::qt_static_metacall moc_qextserialport.cpp 97 0x40dbfa
15 QMetaObject::activate(QObject*, QMetaObject const*, int, void**) /usr/lib/x86_64-linux-gnu/libQtCore.so.4 0 0x7ffff6dc9281
16 QSocketNotifier::activated(int) /usr/lib/x86_64-linux-gnu/libQtCore.so.4 0 0x7ffff6e162fe
17 QSocketNotifier::event(QEvent*) /usr/lib/x86_64-linux-gnu/libQtCore.so.4 0 0x7ffff6dd260b
18 QApplicationPrivate::notify_helper(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/libQtGui.so.4 0 0x7ffff72d7894
19 QApplication::notify(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/libQtGui.so.4 0 0x7ffff72dc713
20 QCoreApplication::notifyInternal(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/libQtCore.so.4 0 0x7ffff6db4e9c
... <More>
UDPATE:
So it seems I have a transient error here. Now it crashed at
for (int i = 0; i < msgBuffer->size(); i++) {
with msgBuffer being yet another QStringList member of my Class. This piece of code is in the 'onReadyRead()' Slot that gets called when QExtSerialPort has data for me.
So I think the error is related to that package.
I'm using Qt 4.8 on Ubuntu 12.04 with QExtSerialPort v1.2rc from https://code.google.com/p/qextserialport

Unpredictable EXC_BAD_ACCESS on FMOD OpenCallback

I'm getting a strange crash, manifesting itself as an EXC_BAD_ACCESS that always happens somewhere on the call stack of a thread opened by FMOD calling it's OpenCallback (the function that is called when FMOD can't find a sound in memory so needs to load it from the disk). The crash comes at a number of different places and I can't seem to find any reason why this changes, since the execution path is essentially the same. One thing I've noticed though is that there are two calls to the same non-recursive constructor adjacent to each other in the call stack. That is, the debugger (Xcode/LLDB, but I've tried it with Xcode/GDB) thinks that this constructor is calling itself, but it isn't.
Another thing I noticed is that the offsets for these calls are different, even though they're referring to the same function. Does anyone know what might be going on? I'm pretty stuck since I don't even know what this kind of problem is called (and therefore can't Google it).
Here's a call stack for one of the crashes (some things are renamed for anonymity, but I tried to keep the naming convention consistent). Notice that the CString and PIFilePath constructors are called twice each, adjacently.
* thread #49: tid = 0x6003, 0x009d3220 MyProject`SpinLock::Lock() + 64 at spinlock.h:73, stop reason = EXC_BAD_ACCESS (code=2, address=0xb01f7ffc)
frame #0: 0x009d3220 MyProject`SpinLock::Lock() + 64 at spinlock.h:73
frame #1: 0x0099d8e0 MyProject`tcmalloc::CentralFreeList::RemoveRange(void**, void**, int) + 160 at central_freelist.cc:211
frame #2: 0x009b4989 MyProject`tcmalloc::ThreadCache::FetchFromCentralCache(unsigned long, unsigned long) + 281 at thread_cache.cc:149
frame #3: 0x009d66fc MyProject`tcmalloc::ThreadCache::Allocate(unsigned long, unsigned long) + 332 at thread_cache.h:330
frame #4: 0x009ce123 MyProject`do_malloc + 227 at tcmalloc.cc:960
frame #5: 0x009ce1e5 MyProject`do_malloc_or_cpp_alloc + 85 at tcmalloc.cc:897
frame #6: 0x009d1285 MyProject`MallocBlock::Allocate(unsigned long, int) + 517 at debugallocation.cc:534
frame #7: 0x009ce460 MyProject`DebugAllocate + 48 at debugallocation.cc:968
frame #8: 0x011cc942 MyProject`malloc + 50
frame #9: 0x00a80bbf MyProject`CSystemUtilities::CSAllocate(long, unsigned long, void*) + 47 at CSystemUtilities.cpp:2358
frame #10: 0x96a08d17 CoreFoundation`CFAllocatorAllocate + 343
frame #11: 0x96a0de4b CoreFoundation`__CFStringChangeSizeMultiple + 1179
frame #12: 0x96a219d6 CoreFoundation`CFStringCreateMutableCopy + 454
frame #13: 0x00a9bae2 MyProject`CString::SetCFString(__CFString const*, bool) + 114 at CString.cpp:527
frame #14: 0x00a9bcb0 MyProject`CString::CString(CString const&) + 112 at CString.cpp:189
frame #15: 0x00a9bc1f MyProject`CString::CString(CString const&) + 47 at CString.cpp:190
frame #16: 0x00aa3a9e MyProject`PIFilePath::NormalisePath(CString const&, long, bool, unsigned short) + 2142 at PIFilePath.cpp:2021
frame #17: 0x00aa2dc9 MyProject`PIFilePath::FindPathInList(CString const&, std::vector<CString, std::allocator<CString> > const&) + 89 at PIFilePath.cpp:1716
frame #18: 0x00aa3d26 MyProject`PIFilePath::IsAbsolutePath(CString const&) + 134 at PIFilePath.cpp:465
frame #19: 0x00aa62d1 MyProject`PIFilePath::ApplyMappingsToPath(CString const&, CString const&, bool) + 1041 at PIFilePath.cpp:1323
frame #20: 0x00aa292d MyProject`PIFilePath::TranslatePath(CString const&) const + 4157 at PIFilePath.cpp:1645
frame #21: 0x00aa15ad MyProject`PIFilePath::SetPath(CString const&, PIFilePath::ConvertPathMode) + 253 at PIFilePath.cpp:175
frame #22: 0x00aa1460 MyProject`PIFilePath::PIFilePath(CString const&, PIFilePath::ConvertPathMode) + 96 at PIFilePath.cpp:122
frame #23: 0x00aa13dd MyProject`PIFilePath::PIFilePath(CString const&, PIFilePath::ConvertPathMode) + 61 at PIFilePath.cpp:123
frame #24: 0x00becad8 MyProject`FileAttributes + 88 at pi_files.cpp:3341
frame #25: 0x0090702b MyProject`SystemStuff::CWinFS::Exists(char const*) + 219 at fsWin.cpp:627
frame #26: 0x008e2892 MyProject`SystemStuff::CMultiFS::FindFS(char const*) + 162 at fsMulti.cpp:390
frame #27: 0x008e1466 MyProject`SystemStuff::CMultiFS::Open(SystemStuff::RCPtr<SystemStuff::IBlock>&, char const*, unsigned long) + 134 at fsMulti.cpp:223
frame #28: 0x008db911 MyProject`SystemStuff::CDispatchFS::Open(SystemStuff::RCPtr<SystemStuff::IBlock>&, char const*, unsigned long) + 321 at fsDispatch.cpp:158
frame #29: 0x008cbb89 MyProject`SystemStuff::FS::Open(SystemStuff::RCPtr<SystemStuff::IBlock>&, char const*, unsigned long) + 233 at fs.cpp:135
frame #30: 0x00985611 MyProject`OpenCallback(char const*, int, unsigned int*, void**, void**) + 177 at sndFMODEx.cpp:324
frame #31: 0x029ecf59 libfmodex.dylib`FMOD::DSP::disconnectFrom(FMOD::DSP*) + 25999
frame #32: 0x029ee7cb libfmodex.dylib`FMOD_File_SetDiskBusy + 4861
frame #33: 0x02a0c15b libfmodex.dylib`FMOD::SystemI::createSoundInternal(char const*, unsigned int, unsigned int, unsigned int, FMOD_CREATESOUNDEXINFO*, FMOD::File**, bool, FMOD::SoundI**) + 2413
frame #34: 0x0298b6aa libfmodex.dylib
frame #35: 0x02a11d2c libfmodex.dylib`FMOD::SystemI::createSoundInternal(char const*, unsigned int, unsigned int, unsigned int, FMOD_CREATESOUNDEXINFO*, FMOD::File**, bool, FMOD::SoundI**) + 25918
frame #36: 0x91b6d557 libsystem_c.dylib`_pthread_start + 344
This is the crash I get about 40% of the time, and the other 60% of the time it fails at NormalisePath (but the callstack above that is the same).

Bool method to check user input

Hi everybody i am trying to make a boolean method that will return value depending upon the user input for the field when submit button is clicked.
- (BOOL)checkuser:(NSString*)input
{
bool result = NO;
if ([input isEqualToString:#"test"])
{
result = YES;
}
else
{
result = NO;
}
return result;
}
And i am refrencing this method from my program as
else if ([Username checkuser] == YES)
{ // do something
}
with following warning
'NSString' may not respond to '-checkuser'
and i am not getting why my program crashing at this point with following error.
2011-03-22 22:57:25.942 Assignment 2[824:207] -[NSCFString checkuser]: unrecognized selector sent to instance 0x4b7c050
2011-03-22 22:57:25.944 Assignment 2[824:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString checkuser]: unrecognized selector sent to instance 0x4b7c050'
* Call stack at first throw:
(
0 CoreFoundation 0x00db8be9 exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f0d5c2 objc_exception_throw + 47
2 CoreFoundation 0x00dba6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d2a366 __forwarding + 966
4 CoreFoundation 0x00d29f22 _CF_forwarding_prep_0 + 50
5 Assignment 2 0x00002958 -[Assignment_2ViewController SubmitbuttonPressed:] + 727
6 UIKit 0x002c1a6e -[UIApplication sendAction:to:from:forEvent:] + 119
7 UIKit 0x003501b5 -[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x00352647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
9 UIKit 0x003511f4 -[UIControl touchesEnded:withEvent:] + 458
10 UIKit 0x002e60d1 -[UIWindow _sendTouchesForEvent:] + 567
11 UIKit 0x002c737a -[UIApplication sendEvent:] + 447
12 UIKit 0x002cc732 _UIApplicationHandleEvent + 7576
13 GraphicsServices 0x016eea36 PurpleEventCallback + 1550
14 CoreFoundation 0x00d9a064 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52
15 CoreFoundation 0x00cfa6f7 __CFRunLoopDoSource1 + 215
16 CoreFoundation 0x00cf7983 __CFRunLoopRun + 979
17 CoreFoundation 0x00cf7240 CFRunLoopRunSpecific + 208
18 CoreFoundation 0x00cf7161 CFRunLoopRunInMode + 97
19 GraphicsServices 0x016ed268 GSEventRunModal + 217
20 GraphicsServices 0x016ed32d GSEventRun + 115
21 UIKit 0x002d042e UIApplicationMain + 1160
22 Assignment 2 0x00001c04 main + 102
23 Assignment 2 0x00001b95 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
(gdb)
You're over-complicating a very simple problem:
- (BOOL) checkuser:(NSString*)input {
return [input isEqualToString:#"test"];
}
and then to call it:
if ([Username checkUser:#"DONT FORGET YOUR PARAM HERE"]) {
// Do something...
}
In your example, you're not passing any parameter to your method... That's a misstake hence your error.
Also, if you create a method returning a 'BOOL', make sure you're returning a 'BOOL' and not a 'bool'. You have to always keep in mind that the underlying types can be different, even if a bool can be evaluated to a BOOL.
else if ([Username checkuser] == YES)
checkuser is supposed to receive a NSString*, which you are not sending though and is the reason for error messages. So, it should be -
Assuming someNSString is initialized and is of type NSString*.
else if( ([Username checkuser:someNSString] ) == YES )
// ^^^^^^^^^^^^ Needs to be passed as the method prototype mentions so.
{
// ....
}
NSString* someNSString = #"Forgot to pass this" ;
else if( ([Username checkuser:someNSString] ) == YES )
// ^^^^^^^^^^^^ Needs to be passed as the method prototype mentions so.
{
// ....
}