error when calling function in another C file

  • Hi,

    In my sample app, I'm calling a function from another C file as follows:

    MyCFile.h:-



    Int Func(CFMutableArrayRef response);



    SampleApp.h:-



    #include "MyCFile.h"

    @interface SampleApp: NSObject

    {

                NSMutableArray *response;

    }

    SampleApp.m:-

    #import "SampleApp.h"

    @implementation SampleApp



    -(id)init

    {

                [super init];

                MyCFunc(response);

    return self;

    }



    Build succeeds without any warning or error.

    Documents say that NSMutableArray * and CFArrayRef are toll-free bridged.
    And can be supplied as arguments to function interchangeably.

    Running the program opens the assembly like code in debugger, with
    objc_msgSend_rtp at the top of the stack and something related to _CFArray
    downwards.

    What I may be doing wrong?



    Thanks in advance.



    Navneet
  • >
    >
    > Build succeeds without any warning or error.
    >
    > Documents say that NSMutableArray * and CFArrayRef are toll-free
    > bridged.
    > And can be supplied as arguments to function interchangeably.

    'response' is not initialized (and so is nil). CF functions crash if
    you call them with NULL as first argument. Unlike with obj-c you have
    to check for nullity before calling Core Foundation function.
  • >
    > -(id)init
    >
    > {
    >
    > [super init];
    >
    > MyCFunc(response);
    The root cause of your problem is here.  The member variable
    response is not at all allocated and you seem to access it in your C
    function.
    Allocate it first before using it through zero bridging. The CF calls
    may end up crashing
    or have undefined behavior if the pointer is nil or is junk.

    Regards
    Shripada
  • Hi, thnks for the reply.
    Actually I'm doing-

    -(id)init

    {

                [super init];
      Response = [[NSMutableArray alloc] init];
                MyCFunc(response);
      return self;

    }

    Still the error...

    -----Original Message-----
    From: cocoa-dev-bounces+navneet=<diskdoctors.com...>
    [mailto:cocoa-dev-bounces+navneet=<diskdoctors.com...>] On Behalf
    Of Navneet Kumar
    Sent: Wednesday, January 02, 2008 3:35 PM
    To: Cocoa-dev
    Subject: error when calling function in another C file

    Hi,

    In my sample app, I'm calling a function from another C file as follows:

    MyCFile.h:-



    Int Func(CFMutableArrayRef response);



    SampleApp.h:-



    #include "MyCFile.h"

    @interface SampleApp: NSObject

    {

                NSMutableArray *response;

    }

    SampleApp.m:-

    #import "SampleApp.h"

    @implementation SampleApp



    -(id)init

    {

                [super init];

                MyCFunc(response);

    return self;

    }



    Build succeeds without any warning or error.

    Documents say that NSMutableArray * and CFArrayRef are toll-free bridged.
    And can be supplied as arguments to function interchangeably.

    Running the program opens the assembly like code in debugger, with
    objc_msgSend_rtp at the top of the stack and something related to _CFArray
    downwards.

    What I may be doing wrong?



    Thanks in advance.



    Navneet
  • On Wednesday, January 02, 2008, at 06:11AM, "Navneet Kumar" <navneet...> wrote:
    > Hi, thnks for the reply.
    > Actually I'm doing-
    >
    > -(id)init
    >
    > {
    >
    > [super init];
    > Response = [[NSMutableArray alloc] init];
    > MyCFunc(response);
    > return self;
    >
    > }
    >
    > Still the error...

    You really need to post all relevant code.  Your first e-mail didn't contain all lines of your init and thus the answers you got ultimately didn't apply.

    Post the relevant sections of the stack trace too.  Simply stating that there was an error related to "_CFArray" isn't helpful.

    Also, you may want to consider doing checks in your init and only doing work appropriately.  The pattern I use is:

    - (id)init
    {
        if ((self = [super init]) != nil)
            {
            // initialize all necessary ivars here
            }

        return self;
    }

    --
    Rick Sharp
    Instant Interactive(tm)
  • Your prototype is
    Int Func(CFMutableArrayRef response);

    And you're calling
    MyCFunc

    Did you quick write your code in the email or is it copy-paste? I
    suspect not the latter because you do manage to compile the code, right?

    Cheers

    On 2008/01/02, at 12:10, Navneet Kumar wrote:

    > Hi, thnks for the reply.
    > Actually I'm doing-
    >
    > -(id)init
    >
    > {
    >
    > [super init];
    > Response = [[NSMutableArray alloc] init];
    > MyCFunc(response);
    > return self;
    >
    > }
    >
    > Still the error...
    >
    > -----Original Message-----
    > From: cocoa-dev-bounces+navneet=<diskdoctors.com...>
    > [mailto:cocoa-dev-bounces+navneet=<diskdoctors.com...>]
    > On Behalf
    > Of Navneet Kumar
    > Sent: Wednesday, January 02, 2008 3:35 PM
    > To: Cocoa-dev
    > Subject: error when calling function in another C file
    >
    > Hi,
    >
    > In my sample app, I'm calling a function from another C file as
    > follows:
    >
    > MyCFile.h:-
    >
    >
    >
    > Int Func(CFMutableArrayRef response);
    >
    >
    >
    > SampleApp.h:-
    >
    >
    >
    > #include "MyCFile.h"
    >
    > @interface SampleApp: NSObject
    >
    > {
    >
    > NSMutableArray *response;
    >
    > }
    >
    > SampleApp.m:-
    >
    > #import "SampleApp.h"
    >
    > @implementation SampleApp
    >
    >
    >
    > -(id)init
    >
    > {
    >
    > [super init];
    >
    > MyCFunc(response);
    >
    > return self;
    >
    > }
    >
    >
    >
    > Build succeeds without any warning or error.
    >
    > Documents say that NSMutableArray * and CFArrayRef are toll-free
    > bridged.
    > And can be supplied as arguments to function interchangeably.
    >
    > Running the program opens the assembly like code in debugger, with
    > objc_msgSend_rtp at the top of the stack and something related to
    > _CFArray
    > downwards.
    >
    > What I may be doing wrong?
    >
    >
    >
    > Thanks in advance.
    >
    >
    >
    > Navneet

previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today