GC and malloc question

  • Hi,

    My program has to build a tree structure, the number of nodes, in
    which can run into many millions.
    So I began to make changes to allocate a node using malloc. Earlier I
    was using a object for a node.

    I have GC enabled (required). And basically I'm making a linked list
    of nodes to store children nodes.
    The root node pointer is a member of a class and children nodes
    pointer are stored in root node.

    Immediately after malloc'ing a node I'm using Garbage Collector method
    - disableCollectorForPointer, just to make sure, although GC is
    supposed to work only on objects, if I'm right.

    My question is: will GC affect the node allocations in anyway and,
    also (GC or No GC) will the tree starting from a root node able to
    stay in memory until I free it.
    Because I'm getting EXC_BAD_ACCESS/KERN_PROTECTION_FAILURE, one after
    the other.

    Will using - disableCollectorForPointer have any effect on this.

    Best,
    Nick
  • On May 31, 2012, at 8:29 AM, Nick Rogers wrote:

    > Immediately after malloc'ing a node I'm using Garbage Collector method - disableCollectorForPointer, just to make sure, although GC is supposed to work only on objects, if I'm right.

    You don't need to do this. GC only applies to Objective-C objects, not malloc or any other memory allocator.

    > My question is: will GC affect the node allocations in anyway and, also (GC or No GC) will the tree starting from a root node able to stay in memory until I free it.

    GC won't affect this. Nothing is going to make a malloc block go away other than your explicitly calling free() on it. Your crashes must be due to something else.

    —Jens
  • On Thu, 31 May 2012 20:59:07 +0530, Nick Rogers said:

    > My question is: will GC affect the node allocations in anyway and,
    > also (GC or No GC) will the tree starting from a root node able to
    > stay in memory until I free it.
    > Because I'm getting EXC_BAD_ACCESS/KERN_PROTECTION_FAILURE, one after
    > the other.

    Are you storing Obj-C objects in your malloc'ed memory?  Because the collector does not track malloc'ed memory and may think your objects are not-rooted and thus get collected before you want.  If so, you could use NSAllocateCollectable with NSScannedOption instead of malloc.

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
previous month may 2012 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