Pthread_create导致分段错误(C ++,Kubutnu 15)

时间:2015-06-13 20:27:46

标签: c++ linux pthreads

老实说,我感到震惊和困惑。在阅读了几篇关于“pthread_create导致分段错误”的主题后,我仍然遇到这个问题。我做了所有订购的,结果为零:在调试阶段,程序在线创建过程中给出了分段错误。

我已经什么都不懂了。我很感激你的帮助。提前谢谢。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <unistd.h>
#include <pthread.h>
#include <interfacemanager.h>
using namespace std;

InterfaceManager IFACE;



void* watch_for_interfaces(void*)
{
    IFACE.monitoring();
}



int main()
{

        sigset_t   *set;
        sigemptyset(&(*set));
        sigaddset(&(*set), SIGINT);
        sigaddset(&(*set), SIGTERM);

        pthread_t th1, th2;
        int i=1;
        if (pthread_create(&th2, NULL, watch_for_interfaces, (void*)&i) != 0) {
          perror("th2 error\n");
          exit(1);
        }




        printf ("Terminating\nPress eny key\n");
        getchar();

        exit(EXIT_SUCCESS);

        return 0;
    }

1 个答案:

答案 0 :(得分:0)

问题不在于 sigset_t *set; sigemptyset(&(*set)); sigaddset(&(*set), SIGINT); sigaddset(&(*set), SIGTERM); ,而在于您的信号功能。

set

在这里,您使用的是未初始化的变量malloc,并将其传递给 undefined 的信号函数。你可以 sigset_t set; sigemptyset(&set); sigaddset(&set, SIGINT); sigaddset(&set, SIGTERM); 但是你必须把它写成

- (IBAction)ShareItem:(id)sender {

NSString *textToShare = @"This is just some random text I put here";
NSURL *myWebsite = [NSURL URLWithString:@"http://www.martylavender.com/"];

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
fetchRequest.resultType = NSDictionaryResultType;

NSError *error      = nil;
NSArray *results    = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSMutableArray *itemNames = [results valueForKey:@"itemname"];
NSString *names = [itemNames componentsJoinedByString:@" \n"];
NSLog(@"%@",names);



NSArray *objectsToShare = @[textToShare, myWebsite, names];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                               UIActivityTypePrint,
                               UIActivityTypePostToTwitter,
                               UIActivityTypeAssignToContact,
                               UIActivityTypeSaveToCameraRoll,
                               UIActivityTypeAddToReadingList,
                               UIActivityTypePostToFlickr,
                               UIActivityTypePostToVimeo];

activityVC.excludedActivityTypes = excludeActivities;

[self presentViewController:activityVC animated:YES completion:nil];
}