Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Malloc RangeError: The specified range is invalid #38

Open
trungnq97 opened this issue Jan 4, 2014 · 5 comments
Open

Malloc RangeError: The specified range is invalid #38

trungnq97 opened this issue Jan 4, 2014 · 5 comments

Comments

@trungnq97
Copy link

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
Copy link
Contributor

cbakgly commented Jan 6, 2014

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 [email protected] 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
Copy link
Author

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
Copy link
Contributor

cbakgly commented Jan 7, 2014

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
Copy link
Author

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
Copy link
Contributor

cbakgly commented Feb 8, 2014

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

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

No branches or pull requests

2 participants