• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSAutoreleasePool and static data member constructors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSAutoreleasePool and static data member constructors


  • Subject: NSAutoreleasePool and static data member constructors
  • From: Erik Buck <email@hidden>
  • Date: Wed, 10 Oct 2007 09:20:20 -0400

I have a C++/Objective-C application which has some constructors for
static data members accessing autorelased Objective-C objects and I
get messages like this in the console:
* _NSAutoreleaseNoPool(): Object 0xd06e70 of class NSCFString
autoreleased with no pool in place - just leaking

Obviously, I need to setup an autorelase pool before all the static
member construction. Is there a way to do this?

No.
Constructors for C++ static data members are all called before main (). You could create an NSAutorleasePool within a static data member's constructor, but the ANSI/C++ standard does not define any order for static data member constructors to be called, and in fact, every time you link your application, the order will likely change.


My advice to you is to not mix Objective-C and C++ in the way you are attempting. Keep the separation clear. If possible, do not use any non-pointer instances of C++ classes as instance variables of Objective-C classes or visa versa. The implementation of C++ classes should access Objective-C objects by pointer only (obviously), and the reverse is also true. Objective-C methods should access C++ instances by pointer only.

GIVEN THE FOLLOWING:
@class    Bar;

class Foo
{
   Bar		*bar;

Foo() {this->bar = nil;};
virtual ~Foo() {[bar release];};
virtual GetBar() {return bar;};
virtual SetBar(Bar *aBar) {[aBar retain];[bar release]; bar = aBar;};
};


GOODE:
@interface Bar : NSObject
{
   Foo         *foo;
}

@end


BAD: @interface Bar : NSObject { Foo foo; // constructor will be called }

@end


GIVEN THE FOLLOWING: @class Bar;

class Foo
{
   Bar		*bar;

Foo() {this->bar = [[Bar alloc] init];};
virtual ~Foo() {[bar release];};
virtual GetBar() {return bar;};
virtual SetBar(Bar *aBar) {[aBar retain];[bar release]; bar = aBar;};
};


GOODE:
static Foo	*foo = NULL;

int main(int argc, const char *argv[])
{
   NSAutoreleasePool	*outerPool = [[NSAutoreleasePool alloc] init];
   foo = new Foo;
   [outerPool release];
}


BAD:
static Foo foo; // Note: this is generally bad when constructors have side effects in straight C++ too



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSAutoreleasePool and static data member constructors
      • From: "Clark Cox" <email@hidden>
    • Re: NSAutoreleasePool and static data member constructors
      • From: Eyal Redler <email@hidden>
  • Prev by Date: NSAutoreleasePool and static data member constructors
  • Next by Date: Using NSValueTransformer with a double
  • Previous by thread: NSAutoreleasePool and static data member constructors
  • Next by thread: Re: NSAutoreleasePool and static data member constructors
  • Index(es):
    • Date
    • Thread