Examples of NSAutoreleasePool


Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

        // If you are making Cocoa calls outside of the Application Kit’s main thread—for example
        // if you create a Foundation-only application or if you detach a thread—you need to create
        // your own autorelease pool.
        Thread thread = new Thread("test") {
            public void run() {
                NSAutoreleasePool second = NSAutoreleasePool.new_();
                second.drain();
            }
        };
        thread.start();
        thread.join();
        RococoaTestCase.assertRetainCount(2, idString);
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

        int expectedInitialRetainCount = expectedAutorelease ? 2 : 1;
        // that will decrease the count IF it was pooled
        int expectedFinalRetainCount = expectedAutorelease ?
          expectedInitialRetainCount - 1 : expectedInitialRetainCount;  
       
        NSAutoreleasePool pool = NSAutoreleasePool.new_();
       
        NSObject object = factory.create();       
        assertRetainCount(expectedInitialRetainCount, object);
       
        // aliasing should increase the retain count, as the alias also owns it
        NSObject alias = Rococoa.cast(object, NSObject.class);
        assertSame(object.id(), alias.id());
        assertRetainCount(expectedInitialRetainCount + 1, object);
        assertRetainCount(expectedInitialRetainCount + 1, alias);
       
        // wait until object has been GC'd
        WeakReference<Object> reference = new WeakReference<Object>(object);
        object = null;
        while (reference.get() != null) {
            gc();
        }
        gc();
       
        // it should now have been release'd
        assertRetainCount(expectedInitialRetainCount, alias);
       
        // now let the pool go
        pool.drain();

        assertRetainCount(expectedFinalRetainCount, alias);
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

     @param <R> the type returned when <code>callable</code> is run
     *  @param callable the Callable code to run with a pool in place
     *  @return the result of running the Callable
     */
    private static <R> R doWithAutoreleasePool(Callable<R> callable) {
        NSAutoreleasePool pool = null;
        try {
            pool = NSAutoreleasePool.new_();
            return callable.call();
        } catch (Exception e) {
            throw e instanceof RuntimeException ? ((RuntimeException)e) : new RuntimeException(e);
        } finally {
            if ( pool != null ) pool.drain();
        }
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

    // This is the test that is made pass by marking NSAutoreleasePool as ReleaseInFinalize(false)
    @Test
    public void garbageCollectDrainedPool() throws InterruptedException {
        Thread thread = new Thread("test") {
            public void run() {
                NSAutoreleasePool pool = NSAutoreleasePool.new_();
                pool.drain();
            }
        };
        thread.start();
        thread.join();
        RococoaTestCase.gc();
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

    }


    @Test
    public void drainPoolAndFinalize() {
        NSAutoreleasePool pool = NSAutoreleasePool.new_();
        pool.drain();
        WeakReference<Object> reference = new WeakReference<Object>(pool);
        pool = null;
        while(reference.get() != null) {
            RococoaTestCase.gc();
        }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

        Foundation.cfRelease(idOfString);
        // causes count to go to 0 and dispose will happen
    }   
   
    @Test public void testAutorelease() {
        NSAutoreleasePool pool = NSAutoreleasePool.new_();
       
        ID idOfString = Foundation.cfString("Hello world");
        assertRetainCount(1, idOfString);
       
        Foundation.sendReturnsVoid(idOfString, "autorelease");
        assertRetainCount(1, idOfString); // autorelease does not increase the count
       
        Foundation.sendReturnsVoid(idOfString, "retain");
        assertRetainCount(2, idOfString);
       
        pool.drain();
        assertRetainCount(1, idOfString);

        Foundation.cfRelease(idOfString);
        // causes count to go to 0 and dispose will happen
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

        Foundation.cfRelease(idOfString);
        // causes count to go to 0 and dispose will happen
    }
   
    @Test public void testInitedObject() {
        NSAutoreleasePool pool = NSAutoreleasePool.new_();

        ID idOfClass = Foundation.getClass("NSString");
        ID idOfString = Foundation.sendReturnsID(idOfClass, "alloc");
        idOfString = Foundation.sendReturnsID(idOfString, "initWithCString:", "Hello world");
        assertRetainCount(1, idOfString);

        // show that it wasn't in the pool
        pool.drain();
        assertRetainCount(1, idOfString);
        Foundation.cfRelease(idOfString);
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

        public void callback(ObjCObjectByReference reference);
    }

    @Test
    public void testArgument() {
        NSAutoreleasePool pool = NSAutoreleasePool.new_();
        TestShunt shunt = Rococoa.create("TestShunt", TestShunt.class);
        ObjCObjectByReference reference = new ObjCObjectByReference();
        shunt.testNSNumberByReference_with(reference, 42);
        NSNumber value = reference.getValueAs(NSNumber.class);
        assertEquals(42, value.intValue());

        // we better have retained the result by the time it gets back
        assertEquals(3, value.retainCount());
        pool.drain();
        assertEquals(2, value.retainCount());
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

        assertEquals(2, value.retainCount());
    }

    @Test
    public void testDelegate() {
        NSAutoreleasePool pool = NSAutoreleasePool.new_();
        TestShunt shunt = Rococoa.create("TestShunt", TestShunt.class);
        final CountDownLatch count = new CountDownLatch(1);
        final ObjCObject callback = Rococoa.proxy(new TestShuntDelegate() {
            public void callback(ObjCObjectByReference reference) {
                // Success
                count.countDown();
            }
        });
        final ID delegate = callback.id();
        shunt.testCallbackWithReference(delegate);
        assertEquals("Callback to delegate failed", 0, count.getCount());
        pool.drain();
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

    }


    @Override
    public BufferedImage getScreenCapture() {
        final NSAutoreleasePool pool = NSAutoreleasePool.new_();
        try {

            if (_windowId > 0) {
                // We already know the ID of the program window.
                BufferedImage image = getWindowImage(_windowId);
                if (image == null) {
                    // We seem to have lost the window?
                    debugLog.debug("    Window not found, resetting _windowId and _pid to 0");
                    _windowId = 0;
                    _pid = 0;
                    return null;
                } else {
                    return image;
                }

            } else {
                // We don't know the ID of the program window, so look for it now.
                _windowId = findWindow(_pid);
                if (_windowId == 0) {
                    // The window couldn't be found, so maybe the program has been closed. Reset the pid to force it to start from the start
                    debugLog.debug("    Window not found, resetting _pid to 0");
                    _pid = 0;

                } else {
                    // The program was found, so take an image
                    debugLog.debug("    Window found, setting _windowId to {}", _windowId);
                    BufferedImage image = getWindowImage(_windowId);
                    return image;
                }
            }

        } catch (Throwable ex) {
            ex.printStackTrace(System.err);
            throw new RuntimeException("Unable to create screen capture for  " + _bundleIdentifier + " due to exception", ex);
        } finally {
            pool.drain();
        }

        return null;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.