2016-08-28 11 views
0

std :: vectorデータをNSDataに保存して復元することができますが、それは断続的に機能します。ここで働くように見えたので、私はもともと質問を投稿し、答えを受け入れた:std :: vectorデータの保存と復元

Storing and Restoring std::vector from NSData

しかし、時にはそれが動作し、他の時間、それはEXC_BAD_ACCESSエラーが発生します。時にはそれは、テストの繰り返し33、47または例えば105でクラッシュします

http://3DTOPO.com/VectorToData.zip

:私はここからダウンロードすることができる問題を再現する例のXcodeプロジェクトを作成しました。ここで

は、それがクラッシュしたときのためのバックトレースです:

* thread #1: tid = 0x1542ee1, 0x000000010d45e65e libsystem_platform.dylib`_platform_memmove$VARIANT$Nehalem + 254, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x7faf29000000) 
frame #0: 0x000000010d45e65e libsystem_platform.dylib`_platform_memmove$VARIANT$Nehalem + 254 
frame #1: 0x0000000109f46753 Foundation`_NSDataCreateVMDispatchData + 131 
frame #2: 0x0000000109f44cf2 Foundation`-[_NSPlaceholderData initWithBytes:length:copy:deallocator:] + 230 
frame #3: 0x0000000109f50902 Foundation`-[NSData(NSData) initWithBytes:length:] + 37 
* frame #4: 0x0000000109ea2bfe VectorToData`storeContourData() + 574 at ViewController.mm:86 
frame #5: 0x0000000109ea2943 VectorToData`-[ViewController viewDidLoad](self=0x00007faf3a10f410, _cmd="viewDidLoad") + 1443 at ViewController.mm:67 
frame #6: 0x000000010af31984 UIKit`-[UIViewController loadViewIfRequired] + 1198 
frame #7: 0x000000010af31cd3 UIKit`-[UIViewController view] + 27 
frame #8: 0x000000010ae07fb4 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 61 
frame #9: 0x000000010ae0869d UIKit`-[UIWindow _setHidden:forced:] + 282 
frame #10: 0x000000010ae1a180 UIKit`-[UIWindow makeKeyAndVisible] + 42 
frame #11: 0x000000010ad8eed9 UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131 
frame #12: 0x000000010ad95568 UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1769 
frame #13: 0x000000010ad92714 UIKit`-[UIApplication workspaceDidEndTransaction:] + 188 
frame #14: 0x000000010d78a8c8 FrontBoardServices`__FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24 
frame #15: 0x000000010d78a741 FrontBoardServices`-[FBSSerialQueue _performNext] + 178 
frame #16: 0x000000010d78aaca FrontBoardServices`-[FBSSerialQueue _performNextFromRunLoopSource] + 45 
frame #17: 0x000000010a905301 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
frame #18: 0x000000010a8fb22c CoreFoundation`__CFRunLoopDoSources0 + 556 
frame #19: 0x000000010a8fa6e3 CoreFoundation`__CFRunLoopRun + 867 
frame #20: 0x000000010a8fa0f8 CoreFoundation`CFRunLoopRunSpecific + 488 
frame #21: 0x000000010ad91f21 UIKit`-[UIApplication _run] + 402 
frame #22: 0x000000010ad96f09 UIKit`UIApplicationMain + 171 
frame #23: 0x0000000109ea237f VectorToData`main(argc=1, argv=0x00007fff55d5d6e0) + 111 at main.m:14 
frame #24: 0x000000010d12a92d libdyld.dylib`start + 1 

たNSDataと協力が好ましいが、私はC++ソリューションが、例えば(例えばfstreamのとのiostream)を使用することをいとわない、私が発見しました私はiOSで作業することができませんでした。 C++ソリューションが提供されている場合は、データの復元と格納の方法を示してください。

あなたは上記のリンクされたプロジェクトを開くことができない場合は、ここで適切なコードで、名前だけそれViewController.mmをドロップし、新しいシングルビューアプリケーションのXcodeプロジェクトにドロップ:

// 
// ViewController.m 
// VectorToData 
// 
// Created by jeshua on 8/27/16. 
// Copyright © 2016 3DTOPO Inc. All rights reserved. 
// 

#import "ViewController.h" 
#import <vector> 

using namespace std; 

typedef vector<CGPoint> CGContour; 
typedef vector<CGContour> CGContours; 
const size_t cgContourSize = sizeof(CGContour); 
static CGContours contoursVector; 

@interface ViewController() 

@end 

@implementation ViewController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 


    for (int testNumber = 0; testNumber < 1000; testNumber++) { 

     NSLog(@"__________ Test Number: %d __________", testNumber); 

     long numberOfContours = arc4random_uniform(2000) + 1000; 

     contoursVector.clear(); 

     contoursVector = CGContours(numberOfContours); 

     long numberOfBytesGenerated = 0; 

     for(int contourNum = 0; (contourNum < numberOfContours); contourNum++) { 

      long contourPointCount = arc4random_uniform(5000) + 1; 

      CGContour thisContour = CGContour(contourPointCount); 

      for(long pointNum = 0; pointNum < thisContour.size(); pointNum++) 
      { 
       thisContour[pointNum] = CGPointMake(arc4random_uniform(800), arc4random_uniform(1200)); 
      } 

      //This might seem redundant, but in my actual code the data is converted to CGPoints here 
      //and is not already a CGPoint 
      contoursVector[contourNum] = CGContour(contourPointCount); 
      for(long pointNum = 0; pointNum < thisContour.size(); pointNum++) 
      { 
       contoursVector[contourNum][pointNum] = thisContour[pointNum]; 
       numberOfBytesGenerated += cgContourSize; 
      } 

     } 

     NSLog(@" numberContoursGenerated: %lu, numberOfBytesGenerated: %lu", numberOfContours, numberOfBytesGenerated); 

     storeContourData(); 

     NSLog(@"\n\n"); 
    } 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

void storeContourData() { 

    NSMutableArray<NSData *> *contourDataArray = [NSMutableArray new]; 

    long numberContoursStored = 0; 
    long numberOfBytesStored = 0; 

    for(const CGContour &contour : contoursVector) 
    { 

     // Both these data initializations seem to work the same 
     NSData *data = [[NSData alloc] initWithBytes:&contour[0] 
               length:contour.size() * cgContourSize]; 

//  NSData *data = [[NSData alloc] initWithBytes:contour.data() 
//           length:contour.size() * cgContourSize]; 

     [contourDataArray addObject:data]; 

     numberContoursStored++; 
     numberOfBytesStored += contour.size() * cgContourSize; 
    } 

    NSLog(@" numberContoursStored: %lu, numberOfBytesStored: %lu", numberContoursStored, numberOfBytesStored); 
} 

@end 
+0

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

+0

私はデバッガを踏んだ。上に掲示されたバックトレースでクラッシュします。私はこの問題を示す完全なXcodeプロジェクトも含めました。踏み込むことの問題は、断続的な問題なので、クラッシュする前に何十万回も歩き回る必要があるかもしれません。 –

+0

私の勇気からは、あなたは未定義の行動をしていると思います。 –

答えて

0

問題輪郭バイトサイズの誤った計算があり、それは

contour.size() * cgContourSize; // sizeof(vector<CGPoint>) 

ではないのですが、あるべき

contour.size() * sizeof(CGPoint); 

ベクトル(または他のデータコンテナ)の先頭のサイズを計算することはできません。このような

書は避けるべきである。

sizeof(vector<...>); 

結果はベクトルオブジェクトのバイトサイズではなく、ベクトル内のデータのバイトサイズですので。

+0

答えと説明をありがとう。 –

関連する問題