Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7428629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:49:58+00:00 2026-05-29T08:49:58+00:00

We are working on a Qt project, and there is some Mac specific code

  • 0

We are working on a Qt project, and there is some Mac specific code that we need to add. We need to register for an event, in a sample program we did that by using:

[[NSNotificationCenter defaultCenter] addObserver:self
                                   selector:@selector(notificationHandler:) 
                                   name:NSWorkspaceDidDeactivateApplicationNotification
                                   object:nil];

Since we can use that directly in our mm file on Qt, we are taking the approach of doing something like:

MyClass::MyClass() : {
    // do other setup ...

    CFNotificationCenterAddObserver
    (
        CFNotificationCenterGetLocalCenter(),
        this,
        &notificationHandler,
        CFSTR("???"),
        NULL,
        CFNotificationSuspensionBehaviorDeliverImmediately
    );
}

whats the string for “NSWorkspaceDidDeactivateApplicationNotification”?? Or how do we attatch ourselves to this particular notification?

We tried NSGod’s approach, but since no Objective-C code can be added in a .h with Qt, then we added a private member which its class is defined in the mm file, that containes the actual logic. like this:

SelectedStuffManager.h

class MacWrap;

class SelectedStuffManager
{
  public:
   ....
    doSomething();

    MacWrap* d;

  private:
   ....
};

SelectedStuffManager.mm

@class MDWorkspaceWatcher;

class MacWrap
{
    public:
        MacWrap();
        ~MacWrap();

        void  applicationDeactivated(NSNotification * notification);

        SystemEventsApplication *systemApplication;
        NSRunningApplication *runApp;

        private:
           MDWorkspaceWatcher *workspaceWatcher;
};
MacWrap::MacWrap() {
      this->workspaceWatcher = [[MDWorkspaceWatcher alloc] initWithMyClass:this];
}

MacWrap::~MacWrap() {
      [this->workspaceWatcher release];
}

void  MacWrap::applicationDeactivated(NSNotification* notification)
{
    // guardar el id del proceso para utilizarlo luego
    runApp = [[notification userInfo] valueForKey:@"NSWorkspaceApplicationKey"];
    NSString *systemEventsASppName = [runApp bundleIdentifier];
    if( [ systemEventsASppName isNotEqualTo:@"com.yo.SelectedText"])
    {
        systemApplication = [SBApplication applicationWithBundleIdentifier:systemEventsASppName];
        NSLog(@"Launched. %@",systemEventsASppName);
    }

}

@interface MDWorkspaceWatcher : NSObject {

     MacWrap  *manager;
}

- (id)initWithMyClass:(MacWrap*)obj;
- (void)didDeactivateApp:(NSNotification *)notification; @end

@implementation MDWorkspaceWatcher
- (id)initWithMyClass:(MacWrap*)obj {
    if ((self = [super init])) {
       manager = obj;

       [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                selector:@selector(didDeactivateApp:)
                name:NSWorkspaceDidDeactivateApplicationNotification
                object:nil];
    }
    return self;
}

- (void)dealloc {
    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
    [super dealloc];
}

- (void)didDeactivateApp:(NSNotification *)notification {
   manager->applicationDeactivated(notification);
}
@end

SelectedStuffManager::SelectedStuffManager()
{
    d = new MacWrap();
}
SelectedStuffManager::doSomething()
{
    if ([[d->runApp localizedName] isEqualTo: @"something"]) --> here it fails, bad memory access
    {
       ...
    }
}

It seems like someone is freeing both runApp and systemApplication, so we get a null pointer or bad memory. How or why could this be happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:49:59+00:00Added an answer on May 29, 2026 at 8:49 am

    I don’t believe you can just do like you’re hoping to. First of all, NSWorkspace uses its own NSNotificationCenter, which is different than the default NSNotificationCenter returned with +defaultCenter.

    I don’t believe there’s a strict CF-equivalent to those NSWorkspace calls. There are likely high-level Carbon-based equivalents, but they’re not available in 64-bit, and should likely be avoided.

    You should be able to accomplish what you want using a small Objective-C helper class to receive the notifications and forward them to your C++ class like in the following code:

    EDIT: updated to remove any Objective-C from header files. Just use generic void * pointers, which you can cast inside the .mm file.

    .h:

    //@class MDWorkspaceWatcher;
    
    class MyClass {
       private:
          // MDWorkspaceWatcher *workspaceWatcher;
          void *workspaceWatcher;
       public:
           MyClass();
           ~MyClass();
    
           // const void didActivateApp(NSNotification *notification) const;
           // const void didDeactivateApp(NSNotification *notification) const;
           const void didActivateApp(void *anNSnotification) const;
           const void didDeactivateApp(void *anNSnotification) const;
    
    };
    

    .mm:

    Objective-C part:

    @interface MDWorkspaceWatcher : NSObject {
        MyClass    *myClass;
    }
    - (id)initWithMyClass:(MyClass *)aMyClass;
    @end
    
    @implementation MDWorkspaceWatcher
    - (id)initWithMyClass:(MyClass *)aMyClass {
        if ((self = [super init])) {
           myClass = aMyClass;
           [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                    selector:@selector(didActivateApp:)
                    name:NSWorkspaceDidActivateApplicationNotification
                    object:nil];
           [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                    selector:@selector(didDeactivateApp:)
                    name:NSWorkspaceDidDeactivateApplicationNotification
                    object:nil];
        }
        return self;
    }
    // very important:
    - (void)dealloc {
        [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
        [super dealloc];
    }
    - (void)didActivateApp:(NSNotification *)notification {
       myClass->didActivateApp(notification);
    }
    - (void)didDeactivateApp:(NSNotification *)notification {
       myClass->didDeactivateApp(notification);
    }
    @end
    

    C++ part:

    MyClass::MyClass() {
          this->workspaceWatcher = [[MDWorkspaceWatcher alloc] initWithMyClass:this];
    }
    
    MyClass::~MyClass() {
          [(MDWorkspaceWatcher *)this->workspaceWatcher release];
    }
    
    MyClass::didActivateApp(void *anNSnotification) {
         NSDictionary *appInfo = [(NSNotification *)anNSnotification userInfo];          
         NSLog(@"appInfo == %@", appInfo);
    
    }
    
    MyClass::didDeactivateApp(void *anNSnotification) {
         NSDictionary *appInfo = [(NSNotification *)anNSnotification userInfo];          
         NSLog(@"appInfo == %@", appInfo);
    
    }
    

    Note that an NSDictionary is toll-free-bridged with CFDictionaryRef, so you can simply cast the appInfo NSDictionary to a CFDictionaryRef and then call the CF functions to get at the contents of the dictionary if you prefer C over Objective-C.

    Note that the notification center owns the appInfo dictionary (in other words, it will be autoreleased), so you shouldn’t call CFRelease() on it like you might with CFCreate*/CFCopy*-related code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a project that I'm working, there's a table with a on update trigger,
I am working on a project that includes a Mac application and an iPad
I'm currently working on a small project: there's a protocol for sending some strings
I'm working on a project where I need a mature crawler to do some
I'm working on a project and there is some battle between how some JS
I'm looking through some existing code in a project I'm working on, and I
I am working in a project where there are configuration files, one in number
I'm working on a project where there is a lot of external service messaging.
I'm working on a project where there is data visualization. My ultimate goal is
I am working on a project currently where there are SQL strings in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.