NeuroLens Discussions  

Go Back   NeuroLens Discussions > Developer Forums > NeuroLens Plugins
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-16-2008
rhoge rhoge is offline
Administrator
 
Join Date: Jun 2005
Posts: 556
Default Garbage Collection

Recent versions of NeuroLens have been built using garbage collection, which is just a compiler flag. As you know, if this option is selected then all retain/release invocations are ignored and dealloc is never called (finalize is used instead).

Older versions of NeuroLens suffered from retain cycle problems that originated in the relationship between the dataset window, the document and the window controller (some people think that this kind of thing is why Apple introduced GC in the first place). The retain cycle problem is the reason for the checkbox in NeuroLens under the "Dev" tab in preferences to force a cleanup of image data when a datasset is closed. Under GC this should no longer be needed.

Although GC sounds (and is) nice, there are a number of programming practices that used to be ok but which can now cause problems (mostly crashes, but also memory leaks). I'm starting this thread as a place to list such issues.

Rick
Reply With Quote
  #2  
Old 01-16-2008
rhoge rhoge is offline
Administrator
 
Join Date: Jun 2005
Posts: 556
Default Methods that return pointers

On thing that caused crashes in NeuroLens after the switch to GC was methods (or functions) that return a pointer to memory allocated using an autoreleased NSMutableData:

Code:
-(float*)badMethodForGC {

  float *buffer = (float*)[[NSMutableData dataWithLength:100] mutableBytes]; // Would be autoreleased in non-GC

  return buffer; // In non-GC, buffer could be used until end of current event loop, when autorelease pool is released

}
Under GC, the collector does not know that 'buffer' is a reference to some bytes in the NSMutableData object. It will free the data at the bottom of the above method (thinking the scope where the data can be used is finished), and any subsequent use of the pointer 'buffer' (which used to be ok) will cause a crash.

Under GC, the correct way to do this is now:

Code:
-(__strong float*)goodMethodForGC {

  __strong float *buffer = (float*)NSAllocateCollectable(100,0); // Like malloc for GC

  return buffer;

}
Note that the second parameter in NSAllocateCollectable is a flag indicating whether the GC should scan that memory for pointers referencing other collectable data. As far as I can tell, if you are just going to use the chunk of data to contain arbitrary data (pixel values, etc.) the flag can be zero. An example where you would set the flag to a value of NSScannedOption is when you will use the data to contain an array of pointers (e.g. the return value will be float**). This was causing the crash in the Volume Registration action after the port to Leopard.

Rick
Reply With Quote
  #3  
Old 10-07-2009
Arnaud Arnaud is offline
Senior Member
 
Join Date: Jun 2005
Location: Durham, NC USA
Posts: 182
Default NSMutableData and large number of elements

Hi Rick,

Thanks for posting the details in the forum on how to do implement GC correctly. I had that exact same error that you described while trying to allocate an NSMutableData in the plugin "run" method.
It seems like large datasets (>~700Mb) aren't any longer supported by NSMutableData. The new syntax using NSAllocateCollective seems to work fine for me at least for the allocation and processing parts. However, this causes a problem with the current implementation of the plugin framework since the MyVolume:configWithData method takes an NSMutableData as first argument.
I've been looking for a more elegant workaround than reimplementing the configWithData method. Any thought would be greatly appreciated.
Cheers,
Arnaud

Last edited by Arnaud : 10-08-2009 at 06:54 AM.
Reply With Quote
  #4  
Old 10-12-2009
Arnaud Arnaud is offline
Senior Member
 
Join Date: Jun 2005
Location: Durham, NC USA
Posts: 182
Default

Building against older versions of the NLPlugin and NLMatrix frameworks seems to be working fine. It will do the trick for the time being.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 02:31 PM.