Преглед на файлове

Remove WIP code which causes memory leaks.

Ben Hsieh преди 8 години
родител
ревизия
c1fcd8a61f
променени са 5 файла, в които са добавени 47 реда и са изтрити 35 реда
  1. 2
    0
      src/ios/RNFetchBlob.xcodeproj/project.pbxproj
  2. 2
    0
      src/ios/RNFetchBlob/RNFetchBlob.m
  3. 15
    11
      src/ios/RNFetchBlobFS.m
  4. 2
    2
      src/ios/RNFetchBlobNetwork.h
  5. 26
    22
      src/ios/RNFetchBlobNetwork.m

+ 2
- 0
src/ios/RNFetchBlob.xcodeproj/project.pbxproj Целия файл

@@ -233,6 +233,7 @@
233 233
 			isa = XCBuildConfiguration;
234 234
 			buildSettings = {
235 235
 				ALWAYS_SEARCH_USER_PATHS = NO;
236
+				GCC_INPUT_FILETYPE = automatic;
236 237
 				HEADER_SEARCH_PATHS = (
237 238
 					"$(inherited)",
238 239
 					"$(SRCROOT)/Libraries/**",
@@ -253,6 +254,7 @@
253 254
 			isa = XCBuildConfiguration;
254 255
 			buildSettings = {
255 256
 				ALWAYS_SEARCH_USER_PATHS = NO;
257
+				GCC_INPUT_FILETYPE = automatic;
256 258
 				HEADER_SEARCH_PATHS = (
257 259
 					"$(inherited)",
258 260
 					"$(SRCROOT)/Libraries/**",

+ 2
- 0
src/ios/RNFetchBlob/RNFetchBlob.m Целия файл

@@ -130,6 +130,7 @@ RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
130 130
         // send HTTP request
131 131
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132 132
         [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request callback:callback];
133
+        utils = nil;
133 134
     });
134 135
 }
135 136
 
@@ -178,6 +179,7 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
178 179
         // send HTTP request
179 180
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
180 181
         [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request callback:callback];
182
+        utils = nil;
181 183
     });
182 184
 }
183 185
 

+ 15
- 11
src/ios/RNFetchBlobFS.m Целия файл

@@ -258,19 +258,18 @@ void runOnMainQueueWithoutDeadlocking(void (^block)(void))
258 258
             // read stream incoming chunk
259 259
         case NSStreamEventHasBytesAvailable:
260 260
         {
261
-            NSMutableData * chunkData = [[NSMutableData data] init];
261
+            NSMutableData * chunkData = [[NSMutableData alloc] init];
262 262
             NSInteger chunkSize = 4096;
263 263
             if([[self.encoding lowercaseString] isEqualToString:@"base64"])
264 264
                 chunkSize = 4095;
265 265
             if(self.bufferSize > 0)
266 266
                 chunkSize = self.bufferSize;
267
-            uint8_t buf[chunkSize];
267
+            uint8_t * buf = (uint8_t *)malloc(chunkSize);
268 268
             unsigned int len = 0;
269
-            
270 269
             len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
271 270
             // still have data in stream
272 271
             if(len) {
273
-                [chunkData appendBytes:(const void *)buf length:len];
272
+                [chunkData appendBytes:buf length:len];
274 273
                 // dispatch data event
275 274
                 NSString * encodedChunk = [NSString alloc];
276 275
                 if( [[self.encoding lowercaseString] isEqualToString:@"utf8"] ) {
@@ -286,30 +285,32 @@ void runOnMainQueueWithoutDeadlocking(void (^block)(void))
286 285
                         NSInteger byteLen = chunkData.length/sizeof(uint8_t);
287 286
                         for (int i = 0; i < byteLen; i++)
288 287
                         {
289
-                            uint8_t * byteFromArray = chunkData.bytes;
290 288
                             NSInteger val = bytePtr[i];
291 289
                             if(i+1 < byteLen)
292 290
                                 asciiStr = [asciiStr stringByAppendingFormat:@"%d,", val];
293 291
                             else
294 292
                                 asciiStr = [asciiStr stringByAppendingFormat:@"%d", val];
295 293
                         }
296
-                        free(bytePtr);
297 294
                     }
298 295
                     asciiStr = [asciiStr stringByAppendingString:@"]"];
299 296
                     [self.bridge.eventDispatcher
300 297
                      sendDeviceEventWithName:streamEventCode
301
-                     body:@{
302
-                            @"event": FS_EVENT_DATA,
303
-                            @"detail": asciiStr
298
+                     body: @{
299
+                             @"event": FS_EVENT_DATA,
300
+                             @"detail": asciiStr
304 301
                             }
305 302
                      ];
303
+                    free(buf);
304
+                    asciiStr = nil;
305
+                    buf = nil;
306
+                    chunkData = nil;
306 307
                     return;
307 308
                 }
308 309
                 // convert byte array to base64 data chunks
309 310
                 else if ( [[self.encoding lowercaseString] isEqualToString:@"base64"] ) {
310 311
                     encodedChunk = [chunkData base64EncodedStringWithOptions:0];
311 312
                 }
312
-                // unknown encoding, send erro event
313
+                // unknown encoding, send error event
313 314
                 else {
314 315
                     [self.bridge.eventDispatcher
315 316
                      sendDeviceEventWithName:streamEventCode
@@ -328,7 +329,8 @@ void runOnMainQueueWithoutDeadlocking(void (^block)(void))
328 329
                         @"detail": encodedChunk
329 330
                         }
330 331
                  ];
331
-                
332
+                chunkData = nil;
333
+                free(buf);
332 334
             }
333 335
             // end of stream
334 336
             else {
@@ -339,6 +341,8 @@ void runOnMainQueueWithoutDeadlocking(void (^block)(void))
339 341
                         @"detail": @""
340 342
                         }
341 343
                  ];
344
+                chunkData = nil;
345
+                free(buf);
342 346
             }
343 347
             break;
344 348
         }

+ 2
- 2
src/ios/RNFetchBlobNetwork.h Целия файл

@@ -25,8 +25,8 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
25 25
 @property (nullable, nonatomic) RCTBridge * bridge;
26 26
 @property (nullable, nonatomic) NSDictionary * options;
27 27
 @property (nullable, nonatomic) RNFetchBlobFS * fileStream;
28
-@property (strong, nonatomic) CompletionHander fileTaskCompletionHandler;
29
-@property (strong, nonatomic) DataTaskCompletionHander dataTaskCompletionHandler;
28
+//@property (strong, nonatomic) CompletionHander fileTaskCompletionHandler;
29
+//@property (strong, nonatomic) DataTaskCompletionHander dataTaskCompletionHandler;
30 30
 @property (nullable, nonatomic) NSError * error;
31 31
 
32 32
 

+ 26
- 22
src/ios/RNFetchBlobNetwork.m Целия файл

@@ -32,8 +32,8 @@ NSOperationQueue *taskQueue;
32 32
 @synthesize callback;
33 33
 @synthesize bridge;
34 34
 @synthesize options;
35
-@synthesize fileTaskCompletionHandler;
36
-@synthesize dataTaskCompletionHandler;
35
+//@synthesize fileTaskCompletionHandler;
36
+//@synthesize dataTaskCompletionHandler;
37 37
 @synthesize error;
38 38
 
39 39
 
@@ -88,7 +88,8 @@ NSOperationQueue *taskQueue;
88 88
     // file will be stored at a specific path
89 89
     if( path != nil) {
90 90
         
91
-        self.fileTaskCompletionHandler = ^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
91
+//        self.fileTaskCompletionHandler = ;
92
+        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
92 93
             if(error != nil) {
93 94
                 callback(@[[error localizedDescription]]);
94 95
                 return;
@@ -102,14 +103,16 @@ NSOperationQueue *taskQueue;
102 103
                 return;
103 104
             }
104 105
             callback(@[[NSNull null], path]);
105
-        };
106
-        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
106
+            // prevent memory leaks
107
+            self.respData = nil;
108
+        }];
107 109
         [task resume];
108 110
     }
109 111
     // file will be stored at tmp path
110 112
     else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
111 113
         
112
-        self.fileTaskCompletionHandler = ^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
114
+//        self.fileTaskCompletionHandler;
115
+        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
113 116
             if(error != nil) {
114 117
                 callback(@[[error localizedDescription]]);
115 118
                 return;
@@ -124,13 +127,15 @@ NSOperationQueue *taskQueue;
124 127
                 return;
125 128
             }
126 129
             callback(@[[NSNull null], tmpPath]);
127
-        };
128
-        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
130
+            // prevent memory leaks
131
+            self.respData = nil;
132
+        }];
129 133
         [task resume];
130 134
     }
131 135
     // base64 response
132 136
     else {
133
-        self.dataTaskCompletionHandler = ^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
137
+//        self.dataTaskCompletionHandler = ;
138
+        NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
134 139
             if(error != nil) {
135 140
                 callback(@[[error localizedDescription]]);
136 141
                 return;
@@ -138,8 +143,7 @@ NSOperationQueue *taskQueue;
138 143
             else {
139 144
                 callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
140 145
             }
141
-        };
142
-        NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:dataTaskCompletionHandler];
146
+        }];
143 147
         [task resume];
144 148
     }
145 149
 }
@@ -205,17 +209,17 @@ NSOperationQueue *taskQueue;
205 209
     
206 210
 }
207 211
 
208
-- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
209
-{
210
-    if(self.dataTaskCompletionHandler != nil)
211
-    {
212
-        dataTaskCompletionHandler(self.respData, nil, error);
213
-    }
214
-    else if(self.fileTaskCompletionHandler != nil)
215
-    {
216
-        fileTaskCompletionHandler(nil, nil, self.error);
217
-    }
218
-}
212
+//- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
213
+//{
214
+//    if(self.dataTaskCompletionHandler != nil)
215
+//    {
216
+//        dataTaskCompletionHandler(self.respData, nil, error);
217
+//    }
218
+//    else if(self.fileTaskCompletionHandler != nil)
219
+//    {
220
+//        fileTaskCompletionHandler(nil, nil, self.error);
221
+//    }
222
+//}
219 223
 
220 224
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
221 225
 {