Skip to content

Conversation

@finscn
Copy link
Contributor

@finscn finscn commented Jun 8, 2014

fix #398

test - case :

var w = window.innerWidth;
var h = window.innerHeight;
var canvas = document.getElementById('canvas');
canvas.width = w;
canvas.height = h;
var context = canvas.getContext('2d');


function createCanvas(width, height) {
    var canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    canvas.retinaResolutionEnabled = false;
    return canvas;
}

function createTempContexts(count) {
    count = count || 10
    for (var i = 0; i < count; i++) {
        var temp = createCanvas(1000, 1000);
        var ctx = temp.getContext("2d");
    }
}


var globalContextList = [];

function createGlobalCanvases(count) {
    count = count || 10
    for (var i = 0; i < count; i++) {
        var canvas = createCanvas(500, 500);
        var ctx = canvas.getContext("2d");
        globalContextList.push(ctx);
    }
}

createGlobalCanvases(40);
createTempContexts(40);

var pool = [];
setInterval(function() {
    // for eating memory
    var longString = (new Array(1000)).join("X")
    pool.push(longString);

    // Expected Result:
    // When recive memroy warning ,
    // The canvas of ctx be created in createTempContexts should be release (size 1000*1000).
    // BUT the canvas of ctx (in globalContextList) should NOT be release (size 500*500).

}, 20);


@finscn
Copy link
Contributor Author

finscn commented Jun 8, 2014

I read an document( a keynote of WWDC 2013, pdf http://devstreaming.apple.com/videos/wwdc/2013/615xax5xpcdns8jyhaiszkz2p/615/615.pdf?dl=1 , video http://devstreaming.apple.com/videos/wwdc/2013/615xax5xpcdns8jyhaiszkz2p/615/615-SD.mov?dl=1 ) about circular reference and garbage collection of JSBinging in iOS.
It says that if a circular reference is between a Objective-C Object and a JS Object , the situation is too complex .
So I try to move the circular reference bewteen Canvas and Context to JS Context.

The core of this pull request is :

  • Don't protect jsCanvas and jsCanvasContext in EJBingingXXXXXX
  • Create the circular reference bwtween jsObjects via JSObjectSetProperty
- (void)linkCanvasAndContext{
    JSStringRef canvasName = JSStringCreateWithUTF8CString("canvas");
    JSObjectSetProperty(scriptView.jsGlobalContext, jsCanvasContext, canvasName, jsObject, kJSPropertyAttributeReadOnly, NULL);
    JSStringRelease(canvasName);

    JSStringRef contextName = JSStringCreateWithUTF8CString("__context");
    JSObjectSetProperty(scriptView.jsGlobalContext, jsObject, contextName, jsCanvasContext, kJSPropertyAttributeDontEnum, NULL);
    JSStringRelease(contextName);
}

A question :
How to let the attribute named "__context" can't be access ?

.

@finscn
Copy link
Contributor Author

finscn commented Jun 8, 2014

the sytleObject in EJBingingCanvas may also use this way ?

@finscn
Copy link
Contributor Author

finscn commented Jun 29, 2014

@phoboslab , Could you reproduce this issue ?
If you couldn't , I will create a project for this issue.

@phoboslab
Copy link
Owner

I understand the issue, but I'm not very fond of the workaround using JS. I'll see if I can fix it in Obj-C land.

@phoboslab
Copy link
Owner

Closing the PR. I'm leaving the related issue #398 open and will fix it eventually when I find the time.

@phoboslab phoboslab closed this Jan 12, 2015
@finscn
Copy link
Contributor Author

finscn commented Mar 12, 2015

@phoboslab , Could you tell me How to fix it ? If you are busy, I could try to do

@finscn finscn deleted the patch-11 branch April 17, 2015 06:48
@finscn
Copy link
Contributor Author

finscn commented Apr 23, 2015

This is very important for me, please.

darionco pushed a commit to TrompoGames/Ejecta that referenced this pull request May 22, 2015
# By Dominic Szablewski (14) and finscn (1)
# Via Dominic Szablewski
* 'master' of github.com:phoboslab/Ejecta:
  Fix phoboslab#509 - make sure the final view render buffer is bound when resizing
  Fix phoboslab#514 - crash on failed IAP purchase
  Fix phoboslab#502 for WebGL Screen Canvas
  Fix phoboslab#498 - add 'target' property to touches
  Check if file exits before triggering image onload callback
  Cosmetics
  Resolve special paths for resources; close phoboslab#491
  Fix phoboslab#502 - don't resize context when it doesn't yet have a framebuffer
  Silence warning for EJTexture return type
  Fix phoboslab#400 - cyclic reference of Canvas and CanvasContext
  Fix KeyInput delete; see phoboslab#484
  Cosmetics
  Fix phoboslab#473 - Text Middle Baseline Calculation
  Update SRWebSocket.m
  Attempt to fix black screen on context creation; see phoboslab#462
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Offscreen-canvas (not temp one) is released after be created [Update example]

2 participants