正確なコードがない:
malloc と free の間の何かがスローされているため、この問題が発生していることを確認してください (そして、おそらくすでにそれをキャッチしているので、ループを終了しません)。これが C (または Objective-C) または C++ コードで発生しているかどうかに応じて、解決方法がわずかに異なります。
C++ では、malloc/free を RAII パターンでラップして、スタックが巻き戻されたときに free が呼び出されるようにします。
class MyData {
public:
A(size_t numShorts) : dataPtr(0) { dataPtr = malloc(numShorts * sizeof(short)); }
~A() { free(dataPtr); }
operator short*() { return dataPtr; }
private:
short* dataPtr;
}
MyData data(numShorts);
// do your stuff, you can still use data as you were before due the 'operator short*'
// allow the dtor to be called when you go out of scope
Objective-C では、finally ブロックを使用する必要があります。
void* myPtr = 0;
@try { myPtr = malloc(...); }
@catch {}
@finally { free(myPtr); }