IPhone / Simulator devices using Objective-C ++

I port the project to iPhone (from Windows Mobile) and share most of the common code in C and C ++ using Objective-C ++. However, during testing, I came across a curious and unpleasant problem that manifests itself only when working on the device. I redid the problem code in a new project to prove reproducibility and facilitate sharing.

// MemoryTestAppDelegate.mm
#include "MemoryTestAppDelegate.h"
#include "Widget.h"

@implementation MemoryTestAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching: (UIApplication*)application {
    Widget widget;
    const wchar_t wideHello[] = L"Hello, world!";
    const char narrowHello[] = "Hello, world!";
    widget.Go();
    widget.Go(wideHello);

    [window makeKeyAndVisible];
}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end

 

// MemoryTestAppDelegate.h
#import <UIKit/UIKit.h>

@interface MemoryTestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow* window;
}

@property (nonatomic, retain) IBOutlet UIWindow* window;

@end

 

// Widget.h
#include <iostream>

class Widget {
public:
    Widget() { };
    ~Widget() { };

    void Go() const { std::wcout << L"Widget is GO." << std::endl; };
    void Go(const wchar_t* message) const { std::wcout << message << std::endl; };
};

 

// main.mm
#import <UIKit/UIKit.h>

int main(int argc, char* argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"MemoryTestAppDelegate");
    [pool release];
    return retVal;
}

 

The remaining project files, as well as settings, are provided by default when creating a new application based on the iPhone Window. (I removed the Builder interface by removing .xib, deleting the "Primary nib file name" from MemoryTest-Info.plist and specifying @ "MemoryTestAppDelegate" as the 4th parameter for UIApplicationMain in main.mm.)

, , : applicationDidFinishLaunching, Widget , . , wideHello narrowHello Locals. (wideHello , , narrowHello "` K3\x10f\x11".) , - 64 of WideHello narrowHello.

, Widget:: Go (const wchar_t *) const, wideHello std:: wcout, applicationDidFinishLaunch ! , wcscpy/memcpy "pre" 64 , . * , , . , ++, , .

, .

+3
1

, .

, , , Mac-, , Intel, i386, , armv6 .

, , , i386, , , .

+1

Source: https://habr.com/ru/post/1722399/


All Articles