0

i've a stupid questiona about passing pointer.

I've this:

@interface MyClass : NSObject
myobj* foo;
-(void)doSomething:(myobj*)aObj;
@end

@implementation MyClass
-(void)doSomething:(myobj*)aObj
{
  cFuncCall(&aObj); //alloc memory and init the object
}

-(id)init
{
  //init stuff...
  [self doSomething:foo]; // foo retun 0x0!!!
}
@end

why foo return nil??? It should be initialized by cFuncCall! [update]

Yes the problem is that foo is not changin outside doSomething. I've tried with myobj** but foo is still nil... This is the real code:

I declare FMOD::DSP *lowpassDSP; in the class interface. No property is set.

This is the function that sould init lowpassDSP. -(void)configFilter:(FMOD::DSP**)dsp { system->createDSPByType(FMOD_DSP_TYPE_LOWPASS, dsp); baseChannel->addDSP(*dsp, 0); } and this is the call: [self configFilter:&lowpassDSP];

in configFilter the dsp variable is correctly initiated but lowpassDSP is nil after calling configFilter.

2
  • please show the code of cFuncCall Commented Jun 11, 2010 at 10:06
  • cFuncCall is a private API and i don't have the code but the function allocates memory and init the object. In doSomthing, aObj is correctly allocated and it works fine. Commented Jun 11, 2010 at 10:08

2 Answers 2

2

The problem is that your object variable foo is not changed outside of doSomething:. Try the following:

-(void)doSomething:(myobj**)aObjP
{
    cFuncCall(aObjP);
}

-(id)init
{
    //...
    [self doSomething:&foo];
}
Sign up to request clarification or add additional context in comments.

3 Comments

The other problem is that foo is not declared in an instance-variables block, so it's a global variable, not an instance variable of each MyClass instance. This will cause wrong behavior and probably memory leaks if the questioner goes on to create more than one MyClass instance.
Yes the problem is that foo is not changin outside doSomething. I've tried with myobj** but foo is still nil...
Ok it's my fault. In createDSPByType the function return NULL because the system isn't initialized. Sorry! :)
1

First of all your -init method isn't defined correctly, perhaps this causes the problem (the superclass of your class isn't properly initialized). Your -init methods should be defined like this in objC:

- (id)init {
  if (self = [super init]) {
     // do custom initialization here, perhaps [self doSomething:foo] if required ...
  }

  return self;
}

-Edit:

Well, in objective-C I've often seen pointers passed to objects like this (notice the pointer-to-pointer notation), I'm not sure if this will work fine in ANSI C as well for passing pointers to methods, but you could check it out:

- (void)callFunction {
   NSError *error = nil;

   [self doSomethingWithError:&error];

   if (error != nil) {
      NSLog(@"error: %@", error];
   }
}

- (void)doSomethingWithError:(NSError **)error {
    // do something ...
}

1 Comment

I know, sorry. I write the method just for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.