adobe-flash / crossbridge

Welcome to visit the homepage!
http://www.crossbridge.io
542 stars 194 forks source link

Malloc RangeError: The specified range is invalid #38

Open trungnq97 opened 10 years ago

trungnq97 commented 10 years ago

Hi,

In side a loop, I use malloc to allocate some domain memory, and free it at the end of a loop. It works well for a while, then I received lots of RangeError.

RangeError: Error #1506: The specified range is invalid.

var pcmPtr:int = CModule.malloc(FRAME_SIZE_BYTE * CHANNEL);

I've seen this https://github.com/adobe-flash/crossbridge/wiki/Memory-Fragmentation, but I don't understand why malloc and free at the same time in init function.

protected function init(e:Event):void { var p:int=CModule.malloc(1024_1024_256);//pre-allocate a block domain memory, the size should be according to your project if (!p) throw(new Error("You have opened too many pages, close some of them or restart your browser!")); CModule.malloc(1);//take up the domain memory CModule.free(p);//release the pre-allocated memory so that it can be used for new C/C++ Object ... }

Can preallocated memory fix RangeError issue?

Many thanks, Trung

cbakgly commented 10 years ago

Surely you can. The code you see just want to make sure a big memory is available. Image that domain memory is where your c++ code data lives in, just like the OS mem you have for stack and heap.

On Jan 5, 2014, at 12:39 AM, trungnq97 notifications@github.com wrote:

Hi,

In side a loop, I use malloc to allocate some domain memory, and free it at the end of a loop. It works well for a while, then I received lots of RangeError.

RangeError: Error #1506: The specified range is invalid.

var pcmPtr:int = CModule.malloc(FRAME_SIZE_BYTE * CHANNEL);

I've seen this https://github.com/adobe-flash/crossbridge/wiki/Memory-Fragmentation, but I don't understand why malloc and free at the same time in init function.

protected function init(e:Event):void { var p:int=CModule.malloc(1024_1024_256);//pre-allocate a block domain memory, the size should be according to your project if (!p) throw(new Error("You have opened too many pages, close some of them or restart your browser!")); CModule.malloc(1);//take up the domain memory CModule.free(p);//release the pre-allocated memory so that it can be used for new C/C++ Object ... }

Can preallocated memory fix RangeError issue?

Many thanks, Trung

— Reply to this email directly or view it on GitHub.

trungnq97 commented 10 years ago

This init function can guarantee that there is enough memory at the time it runs, but it cannot guarantee when this function exits. Is it correct?

Can I put the pre-allocated memory code here (inside event ADDED_TO_STAGE)?

addEventListener(Event.ADDED_TO_STAGE, init);

and free it here (inside event REMOVED_FROM_STAGE)?

addEventListener(Event.REMOVED_FROM_STAGE, cleanup);

Many thanks, Trung

cbakgly commented 10 years ago

Don't understand what you are trying to say. You just call init() once to make sure you have a big memory area available for later allocation.

trungnq97 commented 10 years ago

I thought that when this CModule.free(p); gets executed, the memory is free for other flash instance to take. So after that, there will be no guarantee that we can malloc the same amount of memory as in this example CModule.malloc(1024*1024*256); again. I am not 100% sure that it is correct or not.

If I CModule.malloc an amount of memory to preserve and I don't CModule.free it later, will this amount of memory be automatically released when user closes the page that contains my swf file?

cbakgly commented 10 years ago

See that one byte allocation at the end? This will do the trick.