Basic on Methods -> Objective-C

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

  1. nacho4d macrumors newbie

    Jul 13, 2008
    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

    [myString retain]
    [oldStrind release];
    oldString = myString;
    it should be fine.

    for a normal type (c types, ex: int, double)
    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)
    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
    (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


    Jan 6, 2004
    Western US
    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:

    (void)setBitmap(RGBColor *)myBitmap {
    	if (bitmap != NULL) {
    	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

    Jul 13, 2008

    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.


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


    Jan 10, 2005
    Dallas, TX
    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:
    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:
    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.


Share This Page