Basic on Methods -> Objective-C

Discussion in 'Mac Programming' started by nacho4d, Jul 14, 2008.

  1. nacho4d macrumors newbie

    Joined:
    Jul 13, 2008
    #1
    I have a question about methods and memory allocation in Objective-C
    for example.

    for setting an object in a class that has a NSString object as attribute

    Code:
    -(void)setString:(NSString*)myString{
    [myString retain]
    [oldStrind release];
    oldString = myString;
    }
    
    it should be fine.

    for a normal type (c types, ex: int, double)
    Code:
    -(void)setInt:(int)myint{
    oldInt = myint;
    }
    
    It should work;

    but how about pointers?

    I mean:

    lets suppose I have a pointer to CF's structure RGBColor (which is the bitmap of a pic)
    Code:
    RGBColor *myBitmap = (RGBColor *)malloc (width*height*sizeof(RGBColor));
    
    and then I set some values in the structure.

    and then Try to pass is as an argument
    Code:
    (void)setBitmap(RGBColor *)myBitmap{
    oldBitmap = myBitmap;
    }
    
    is this ok?
    or do I need to initialize oldBitmap also? (lets suppose is the first time I need to use it)

    I am trying to pass a pointer to structure and then try to create a CGImageRef in order to show it using IKImageView. but the image shown is different from what i am trying to create.

    help please.
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    I think that would be fine as long as you remember to free the data when you're done with it. Maybe you could add that free into the accessor method with a null check, something like:

    Code:
    (void)setBitmap(RGBColor *)myBitmap {
    	if (bitmap != NULL) {
    		free(bitmap);
    	}
    	bitmap = myBitmap;
    }
    You could also stuff it into an NSData object and then use retain/release, and then use NSData's bytes: method to obtain the raw data when needed.

    Also it seems odd that you are calling the variable you're assigning to "old<whatever>", since as soon as you assign it, it's no longer "old", it's "new". I know that's nitpicky and you were just throwing out test accessors.
     
  3. nacho4d thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #3
    thanks

    Thanks but I am having trouble with memory allocation.
    If i would use a statically allocated array (RGBColor bitmap[][]) there would be no problem.
    But When i try to use is dynamically the colors shown in the image are different, Actually, is the same problem when you don't make "static" the RGBColor array in the definition.

    ie->

    static RGBColor bitmap[256][256]; -> OK
    RGBColor bitmap[256][256]; ->NG
    RGBColor *bitmap = (RGBColor *)malloc(x*y*sizeof(RGBColor));-> It is not working well!.
    :confused:
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    What are you passing bitmap to? Do you need to pass it an RGBColor **? An RGBColor *? An RGBColor struct?

    It seems like it would be better if you did something like:
    Code:
    RGBColor **bitmap = NULL;
    bitmap = (RGBColor **)malloc(x*sizeof(RGBColor *));
    bitmap[0] = (RGBColor *)malloc(x*y*sizeof(RGBColor));
    for(int dimA=1; dimA < x; dimA++) {
      bitmap[dimA] = bitmap[dimA - 1] + y;
    }
    
    if it needs to be dynamic, then to free:
    Code:
    free(bitmap[0]);
    free(bitmap);
    
    This is somewhat involved, and should not really be necessary. You cannot depend on local storage once it is out of scope, but if bitmap is a member of an Object, as long as that Object instance is retained you should be able to get to bitmap through an accessor method and its memory should be accessible from outside of the class.

    -Lee
     

Share This Page