realm / realm-cocoa-converter

A library that provides the ability to import/export Realm files from a variety of data container formats.
Apache License 2.0
219 stars 25 forks source link

Importing CSVs in a loop causes EXC_BAD_ACCESS in RLMRegisterClassLocalNames #43

Open jondewoo opened 7 years ago

jondewoo commented 7 years ago

Hi,

I'm exporting multiple CSV files with the same schema to Realm dbs in different directories, in a loop:

for srcFilePath in srcFilePathArray {
  let generator =  ImportSchemaGenerator(files: [ srcFilePath! ])
  let schema = try! generator.generate()

  let dataImporter = CSVDataImporter(files: [ srcFilePath! ])
  let dstDirectoryPath = NSURL.fileURLWithPath(srcFilePath).URLByAppendingPathComponent("Realm")?.path

  try! dataImporter.importToPath(dstDirectoryPath!, schema: schema)
}

Upon the second call to dataImporter.importToPath(), an EXC_BAD_ACCESS exception is raised from RLMRegisterClassLocalNames. The console can read:

objc[20294]: objc_disposeClassPair: class 'Class' still has subclasses, including 'RLMAccessor_0_Class'!
objc[20294]: objc_disposeClassPair: class 'Class' still has subclasses, including 'RLMAccessor_0_Class'!

The break is at (RLMSchema.mm:106, objc_retain): if (Class existingClass = s_localNameToClass[className]) {

Here's the stacktrace:

0   ???                                 0x000000010ceeda15 0x0 + 4511947285,
1   MyApp                               0x0000000100005a60 main + 0,
2   Realm                               0x0000000100374ef1 _ZL26RLMRegisterClassLocalNamesPP10objc_classm + 417,
3   Realm                               0x0000000100374384 +[RLMSchema schemaWithObjectClasses:] + 932,
4   Realm                               0x00000001003698d5 -[RLMRealmConfiguration setObjectClasses:] + 85,
5   RealmConverter                      0x0000000100b0b3d6 _TFC14RealmConverter12DataImporter18createNewRealmFilefzTSS6schemaCS_12ImportSchema_CSo8RLMRealm + 4294,
6   RealmConverter                      0x0000000100b0689b _TFC14RealmConverter15CSVDataImporter12importToPathfzTSS6schemaCS_12ImportSchema_CSo8RLMRealm + 235,
7   MyApp                               0x0000000100003c34 _TFC19MyApp14ViewController7convertfCSo8NSButtonT_ + 7092,
8   MyApp                               0x00000001000045ba _TToFC19MyApp14ViewController7convertfCSo8NSButtonT_ + 58,
9   libsystem_trace.dylib               0x00007fffb47dcc3d _os_activity_initiate + 61,
10  AppKit                              0x00007fff9d300150 -[NSApplication(NSResponder) sendAction:to:from:] + 456,
11  AppKit                              0x00007fff9cde5988 -[NSControl sendAction:to:] + 86,
12  AppKit                              0x00007fff9cde58b0 __26-[NSCell _sendActionFrom:]_block_invoke + 136,
13  libsystem_trace.dylib               0x00007fffb47dcc3d _os_activity_initiate + 61,
14  AppKit                              0x00007fff9cde5808 -[NSCell _sendActionFrom:] + 128,
15  AppKit                              0x00007fff9ce28201 -[NSButtonCell _sendActionFrom:] + 98,
16  libsystem_trace.dylib               0x00007fffb47dcc3d _os_activity_initiate + 61,
17  AppKit                              0x00007fff9cde40ee -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2481,
18  AppKit                              0x00007fff9ce27f3a -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 798,
19  AppKit                              0x00007fff9cde2aa3 -[NSControl mouseDown:] + 832,
20  AppKit                              0x00007fff9d477ab7 -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341,
21  AppKit                              0x00007fff9d4742d4 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942,
22  AppKit                              0x00007fff9d473772 -[NSWindow(NSEventRouting) sendEvent:] + 541,
23  AppKit                              0x00007fff9d2fc0a9 -[NSApplication(NSEvent) sendEvent:] + 1145,
24  AppKit                              0x00007fff9cb784b1 -[NSApplication run] + 1002,
25  AppKit                              0x00007fff9cb42d80 NSApplicationMain + 1237,
26  MyApp                               0x0000000100005ab4 main + 84,
27  libdyld.dylib                       0x00007fffb45aa255 start + 1,
28  ???                                 0x0000000000000003 0x0 + 3
)

A similar crash happens in the Realm Browser when importing two CSVs with the same schema in a row.

Best regards, Jonathan

stel commented 7 years ago

Thank you for reporting this issue @jderrough! Will need to take a close look on it. Realm Browser uses RealmConverter internally, that's why you can see this issue there as well.

jondewoo commented 7 years ago

Great, thanks for picking this up, @stel.