[vtkusers] vtkCocoaRenderWindow bug?
Sean McBride
sean at rogue-research.com
Tue Apr 8 10:30:13 EDT 2008
On 4/5/08 7:32 AM, Antoine Rosset said:
>Ok, thanks. You are right.
>
>Yes, I'm having problems with this cocoamanager variable and memory
>management:
Hi Antoine,
Sorry for my delayed reply....
>To create your own NSWindow, you create a subclass of vtkCocoaGLView
>and you initialize it with:
NSWindow? You mean NSView, right?
>-(id)initWithFrame:(NSRect)frame {
>
> if ( self = [super initWithFrame:frame])
> {
> _renderer = vtkRenderer::New();
> _cocoaRenderWindow = vtkCocoaRenderWindow::New();
> _cocoaRenderWindow->SetWindowId( [self window]);
> _cocoaRenderWindow->SetDisplayId( self);
>
> _cocoaRenderWindow->AddRenderer(_renderer);
> _interactor = vtkCocoaRenderWindowInteractor::New();
> _interactor->SetRenderWindow(_cocoaRenderWindow);
>
>and a destructor
>
>-(void)dealloc
>{
> _renderer->Delete();
> _cocoaRenderWindow->Delete();
> _interactor->Delete();
> [super dealloc];
>}
>
>By calling _cocoaRenderWindow->SetDisplayId( self);, you increase the
>retain count of the NSView, because you add it to the cocoamanager
>NSDictionary
>
>Hence, the NSView will not be released automatically, because you will
>remove this NSView from the cocoamanager in the destructor of
>vtkCocoaRenderWindow AND the destructor of vtkCocoaRenderWindow will
>be called from the dealloc of the NSView, that cannot be called
>because the cocoamanager retains the NSView. (I hope it's not too
>confuse... :-))
>
>To avoid this memory leak, I had to create a 'prepareForRelease'
>function, that I call from my NSWindowController dealloc : [myVTKView
>prepareForRelease]
>
>- (void) prepareForRelease
>{
> _cocoaRenderWindow->SetWindowId( 0L);
> _cocoaRenderWindow->SetDisplayId( 0L);
>}
>
>Then, the NSView will be released.
>
>To identify the problem, put an NSLog in your dealloc of your VTKView
>subclass, to see if it is called.
>
>The solution would be to put the 'displayId' in a variable, instead of
>adding it to the NSDictionary.
It used to be a variable but we moved it into a dictionary to facilitate
supporting Garbage Collection. With Obj-C++ GC support is a bit
trickier, as the collector does not scan malloc'ed/new'ed memory.
>What do you think? Is there another way of subclassing the VTKView ?
I think I'll review the changes we made... there may be a nicer way of
doing all this... Could you help me test changes?
Thanks,
--
____________________________________________________________
Sean McBride, B. Eng sean at rogue-research.com
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
More information about the vtkusers
mailing list