Skip to main content
July 18, 2012
Answered

Issues writing to FREByteArray

  • July 18, 2012
  • 1 reply
  • 1379 views

I'm working on a native extension that reads in the contents of a file and writes those contents to a ByteArray for my application to use.

The data is being read fine, I've proven this by returning a string of what I'm reading, but I want this to handle data other than just text.

Here is the native code I'm using to write to the FREByteArray object:

FREObject retVal;

          NSString *errorLevel;

          NSData *fileData;

 

          uint32_t location;

          FREGetObjectAsUint32(argv[2], &location);

          if (location > DSLocation_Temp) {

                    errorLevel = [NSString stringWithFormat:@"Unrecognised location %i", location];

                    FREDispatchStatusEventAsync(g_ctx, (uint8_t *)ARGUMENT_ERROR, (uint8_t *)[errorLevel UTF8String]);

  FRENewObjectFromBool(NO, &retVal);

                    return retVal;

          }

 

          uint32_t pathLength;

          const uint8_t *pathCString;

          NSString *path = nil;

 

          if (FRE_OK == FREGetObjectAsUTF8(argv[1], &pathLength, &pathCString)) {

                    path = [NSString stringWithUTF8String: (char*)pathCString];

          }

 

          BOOL success = [[DataStoreHelper helper] readData:&fileData atPath:path forLocation:location];

 

          if (success) {

  FREByteArray bytes;

                    FREAcquireByteArray(argv[0], &bytes);

                    uint8_t* nativestr = (uint8_t *)fileData.bytes;

                    memcpy(bytes.bytes, nativestr, 12);

  FREReleaseByteArray(argv[0]);

          }

  else {

                    errorLevel = [[[DataStoreHelper helper] currentError] localizedDescription];

                    FREDispatchStatusEventAsync(g_ctx, (uint8_t *)DATA_WRITE_ERROR, (uint8_t *)[errorLevel UTF8String]);

          }

          FRENewObjectFromBool(success, &retVal);

 

          return retVal;

Without fail this always crashes on the memcpy line. I've also tried using this exact code here and it crashes as well:

http://help.adobe.com/en_US/air/extensions/WS460ee381960520ad-866f9c112aa6e1ad46-7ff5.html

Here is the crash info from the device logs (grabbed from Xcode Organizer):

Incident Identifier: 69F45B8A-7123-4EAF-8A37-D4153821B2E5

CrashReporter Key:   26c3b0d6033ccf218b9d8de1aa872f71af680903

Hardware Model:      iPad1,1

Process:         ImaginaryFriends [17092]

Path:            /var/mobile/Applications/3452D48C-C53B-4B3E-9066-AB9191BB2D8F/ImaginaryFriends.app/ImaginaryFriends

Identifier:      ImaginaryFriends

Version:         ??? (???)

Code Type:       ARM (Native)

Parent Process:  launchd [1]

Date/Time:       2012-07-18 16:56:08.390 -0400

OS Version:      iPhone OS 5.1.1 (9B206)

Report Version:  104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)

Exception Codes: KERN_INVALID_ADDRESS at 0x00000000

Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread

Thread 0 Crashed:

0   libsystem_c.dylib                       0x34a710d2 memcpy$VARIANT$CortexA8 + 946

This topic has been closed for replies.
Correct answer

I managed to fix this on my own. You have to write the length of the ByteArray before you can set the bytes:

FREByteArray byteArray;

FREObject bytesLength;

FRENewObjectFromUint32(fileData.length, &bytesLength);

FRESetObjectProperty(argv[0], (uint8_t *)"length", bytesLength, NULL);

FREAcquireByteArray(argv[0], &byteArray);

memcpy(byteArray.bytes, fileData.bytes, fileData.length);

FREReleaseByteArray(argv[0]);

fileData is an NSData object.

1 reply

Correct answer
July 20, 2012

I managed to fix this on my own. You have to write the length of the ByteArray before you can set the bytes:

FREByteArray byteArray;

FREObject bytesLength;

FRENewObjectFromUint32(fileData.length, &bytesLength);

FRESetObjectProperty(argv[0], (uint8_t *)"length", bytesLength, NULL);

FREAcquireByteArray(argv[0], &byteArray);

memcpy(byteArray.bytes, fileData.bytes, fileData.length);

FREReleaseByteArray(argv[0]);

fileData is an NSData object.