Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

fix crash

roryclearc3a2d23d5dc3
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.m
index 003e607..42e0a80 100644
--- a/clearcam/FileServer.m
+++ b/clearcam/FileServer.m
@@ -78,17 +78,15 @@
return [paths firstObject];
}
-- (NSArray *)fetchAndProcessSegmentsFromCoreDataForDateParam:(NSString *)dateParam context:(NSManagedObjectContext *)context { //todo, this can cause a crash?
- __block NSMutableArray *tempSegmentDicts = [NSMutableArray array];
+- (NSArray *)fetchAndProcessSegmentsFromCoreDataForDateParam:(NSString *)dateParam context:(NSManagedObjectContext *)context {
+ if (!context) {
+ NSLog(@"Context is nil, skipping fetch.");
+ return @[];
+ }
- [context performBlockAndWait:^{
- if (!context) {
- NSLog(@"Context is nil, skipping fetch.");
- return;
- }
- // Ensure latest changes are processed before fetching
- [context processPendingChanges];
+ __block NSArray *copiedSegments = @[];
+ [context performBlockAndWait:^{
NSError *error = nil;
// Fetch the DayEntity for the given date
@@ -110,68 +108,71 @@
NSManagedObject *dayEntity = fetchedDays.firstObject;
NSOrderedSet *segments = [dayEntity valueForKey:@"segments"];
- for (NSManagedObject *segment in segments) {
- NSString *url = [segment valueForKey:@"url"];
- double timeStamp = [[segment valueForKey:@"timeStamp"] doubleValue];
- double duration = [[segment valueForKey:@"duration"] doubleValue];
-
- NSMutableArray *frameDicts = [NSMutableArray array];
-
- NSOrderedSet *frames = [segment valueForKey:@"frames"];
-
- for (NSManagedObject *frame in frames) {
- double frameTimeStamp = [[frame valueForKey:@"frame_timeStamp"] doubleValue];
- double aspectRatio = [[frame valueForKey:@"aspect_ratio"] doubleValue];
- int res = [[frame valueForKey:@"res"] intValue];
-
- NSMutableArray *squareDicts = [NSMutableArray array];
-
- NSOrderedSet *squares = [frame valueForKey:@"squares"];
-
- for (NSManagedObject *square in squares) {
- double originX = [[square valueForKey:@"originX"] doubleValue];
- double originY = [[square valueForKey:@"originY"] doubleValue];
- double bottomRightX = [[square valueForKey:@"bottomRightX"] doubleValue];
- double bottomRightY = [[square valueForKey:@"bottomRightY"] doubleValue];
- int classIndex = [[square valueForKey:@"classIndex"] intValue];
-
- NSDictionary *squareDict = @{
- @"originX": @(originX),
- @"originY": @(originY),
- @"bottomRightX": @(bottomRightX),
- @"bottomRightY": @(bottomRightY),
- @"classIndex": @(classIndex)
- };
-
- [squareDicts addObject:squareDict];
- }
+ // Create an immutable copy of the segments
+ copiedSegments = [segments array];
+ }];
- NSDictionary *frameDict = @{
- @"frame_timeStamp": @(frameTimeStamp),
- @"aspect_ratio": @(aspectRatio),
- @"res": @(res),
- @"squares": squareDicts
+ // Process outside of performBlockAndWait to avoid blocking Core Data
+ NSMutableArray *tempSegmentDicts = [NSMutableArray array];
+
+ for (NSManagedObject *segment in copiedSegments) {
+ NSString *url = [segment valueForKey:@"url"];
+ double timeStamp = [[segment valueForKey:@"timeStamp"] doubleValue];
+ double duration = [[segment valueForKey:@"duration"] doubleValue];
+
+ NSMutableArray *frameDicts = [NSMutableArray array];
+ NSArray *frames = [[segment valueForKey:@"frames"] array]; // Copy frames
+
+ for (NSManagedObject *frame in frames) {
+ double frameTimeStamp = [[frame valueForKey:@"frame_timeStamp"] doubleValue];
+ double aspectRatio = [[frame valueForKey:@"aspect_ratio"] doubleValue];
+ int res = [[frame valueForKey:@"res"] intValue];
+
+ NSMutableArray *squareDicts = [NSMutableArray array];
+ NSArray *squares = [[frame valueForKey:@"squares"] array]; // Copy squares
+
+ for (NSManagedObject *square in squares) {
+ double originX = [[square valueForKey:@"originX"] doubleValue];
+ double originY = [[square valueForKey:@"originY"] doubleValue];
+ double bottomRightX = [[square valueForKey:@"bottomRightX"] doubleValue];
+ double bottomRightY = [[square valueForKey:@"bottomRightY"] doubleValue];
+ int classIndex = [[square valueForKey:@"classIndex"] intValue];
+
+ NSDictionary *squareDict = @{
+ @"originX": @(originX),
+ @"originY": @(originY),
+ @"bottomRightX": @(bottomRightX),
+ @"bottomRightY": @(bottomRightY),
+ @"classIndex": @(classIndex)
};
- [frameDicts addObject:frameDict];
+ [squareDicts addObject:squareDict];
}
- NSDictionary *segmentDict = @{
- @"url": url,
- @"timeStamp": @(timeStamp),
- @"duration": @(duration),
- @"frames": frameDicts
+ NSDictionary *frameDict = @{
+ @"frame_timeStamp": @(frameTimeStamp),
+ @"aspect_ratio": @(aspectRatio),
+ @"res": @(res),
+ @"squares": squareDicts
};
- [tempSegmentDicts addObject:segmentDict];
+ [frameDicts addObject:frameDict];
}
- NSLog(@"Fetched and processed %lu segments for date %@", (unsigned long)tempSegmentDicts.count, dateParam);
- }];
+ NSDictionary *segmentDict = @{
+ @"url": url,
+ @"timeStamp": @(timeStamp),
+ @"duration": @(duration),
+ @"frames": frameDicts
+ };
+
+ [tempSegmentDicts addObject:segmentDict];
+ }
+
+ NSLog(@"Fetched and processed %lu segments for date %@", (unsigned long)tempSegmentDicts.count, dateParam);
return tempSegmentDicts;
}
-
- (void)startHTTPServerWithBasePath:(NSString *)basePath {
@try {
signal(SIGPIPE, SIG_IGN);
diff --git a/clearcam/SettingsManager.m b/clearcam/SettingsManager.m
index 005a66f..677120e 100644
--- a/clearcam/SettingsManager.m
+++ b/clearcam/SettingsManager.m
@@ -65,7 +65,7 @@
- (NSArray<NSNumber *> *)generateDefaultEvents {
NSMutableArray<NSNumber *> *defaultEvents = [NSMutableArray array];
//[defaultEvents addObject:@0]; // 1 person for now // todo, default will be none, +/- person will be a bool
- //[defaultEvents addObject:@2];
+ [defaultEvents addObject:@2];
[defaultEvents addObject:@63]; //laptop for now
return [defaultEvents copy];
}
diff --git a/clearcam/ViewController.m b/clearcam/ViewController.m
index 9352008..df9344a 100644
--- a/clearcam/ViewController.m
+++ b/clearcam/ViewController.m
@@ -683,10 +683,8 @@ NSMutableDictionary *classColorMap;
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGFloat aspect_ratio = (CGFloat)width / (CGFloat)height;
- NSMutableDictionary *frame = [[NSMutableDictionary alloc] init];//todo, init elsewhere
- NSMutableDictionary *frameSquare = [[NSMutableDictionary alloc] init];;
- NSMutableArray *frameSquares = [[NSMutableArray alloc] init];;
-
+ NSMutableDictionary *frame = [[NSMutableDictionary alloc] init];
+ NSMutableArray *frameSquares = [[NSMutableArray alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
@@ -718,29 +716,29 @@ NSMutableDictionary *classColorMap;
@try {
[weak_self resetSquares];
for (int i = 0; i < output.count; i++) {
+ NSMutableDictionary *frameSquare = [[NSMutableDictionary alloc] init];
+ frameSquare[@"originX"] = output[i][0];
+ frameSquare[@"originY"] = output[i][1];
+ frameSquare[@"bottomRightX"] = output[i][2];
+ frameSquare[@"bottomRightY"] = output[i][3];
+ frameSquare[@"classIndex"] = output[i][4];
+ [frameSquares addObject:frameSquare];
+
[weak_self drawSquareWithTopLeftX:[output[i][0] floatValue]
topLeftY:[output[i][1] floatValue]
bottomRightX:[output[i][2] floatValue]
bottomRightY:[output[i][3] floatValue]
classIndex:[output[i][4] intValue]
aspectRatio:aspect_ratio];
- frameSquare[@"originX"] = output[i][0];
- frameSquare[@"originY"] = output[i][1];
- frameSquare[@"bottomRightX"] = output[i][2];
- frameSquare[@"bottomRightY"] = output[i][3];
- frameSquare[@"classIndex"] = output[i][4];
- [frameSquares addObject:[frameSquare copy]];
}
frame[@"squares"] = frameSquares;
- // Ensure thread safety and check for nil
+ // Ensure thread safety and check for nil values
@synchronized(weak_self) {
- if (frame) {
- [self.segmentLock lock];
+ if (weak_self && weak_self.current_segment_squares && frame) {
[weak_self.current_segment_squares addObject:frame];
- [self.segmentLock unlock];
} else {
- NSLog(@"Warning: Attempted to insert nil frame into array.");
+ NSLog(@"Warning: weak_self, current_segment_squares, or frame is nil.");
}
}
@@ -752,10 +750,9 @@ NSMutableDictionary *classColorMap;
});
});
} @catch (NSException *exception) {
- NSLog(@"Exception occurred: %@, %@", exception, [exception callStackSymbols]);
+ NSLog(@"Exception occurred: %@, %@", exception, [exception callStackSymbols]);
}
}
-
- (CVPixelBufferRef)addColoredRectangleToPixelBuffer:(CVPixelBufferRef)pixelBuffer withColor:(UIColor *)color originX:(CGFloat)originX originY:(CGFloat)originY width:(CGFloat)width height:(CGFloat)height opacity:(CGFloat)opacity {
CVPixelBufferLockBaseAddress(pixelBuffer, 0);