如何获取实例的“内存位置"?在动作脚本中? [英] How can I get an instance's "memory location" in ActionScript?

查看:15
本文介绍了如何获取实例的“内存位置"?在动作脚本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FlexBuilder 的调试器将向您显示任何范围内实例的内存位置"(或者,我只能假设,大致类似的东西):

但我想在代码中获取这些信息(有点像 Python 的 id 函数),这样我就可以很容易地跟踪对象如何在系统中移动.例如,我可能有:

trace("返回", id(foo));

然后我可以在其他地方使用:

trace("Using", id(foo));

确保两段代码都在处理同一个实例.

现在,我知道许多 AS 类都实现了 IUID 接口......但也有很多类没有实现(例如,普通的数组和对象),所以不会解决我的问题.

我意识到我也可以将对象包装在 ObjectProxy 中,但这也不太理想.

解决方案

实际上,我建议您不要过多使用它……它非常昂贵.Adobe 需要创建一个原生函数来返回给我们.

但是,现在...试试这个:

您需要进行显式强制才能获得它!因为当你进行明确的强制转换时,你会得到这样的错误:

<前>类型错误:错误 #1034:类型强制失败:无法将 Main@1c49d31 转换为 flash.utils.ByteArray.

请注意,在此错误中,您会得到您想要的...@1c49d31.这个哈希就像内存分配中的一个 ID.

我做了很多测试.当您调用new"(在 C 语言中相当于 [[... alloc] init])并且对于静态函数和静态属性时,此哈希值只会发生变化……无论如何……

回到 Flash,问题是我们没有直接的方法来获得这个哈希而不出错.

但这并不是什么大问题.您所需要的只是使用一些尝试"和捕获"像这样:

试试{ByteArray(anyObjectToKnowItAllocationHash);}捕捉(e:错误){痕迹(e);}

瞧!您将获得哈希值而不会导致错误!在此之后,我做了一个更精致的方式......试试这个:

var memoryHash:String;尝试{FakeClass(anyObjectToKnowItAllocationHash);}捕捉(e:错误){memoryHash = String(e).replace(/.*([@|\$].*?) to .*$/gi, '$1');}内部最终类 FakeClass { }

稍微解释一下:fakeClass 是为了确保这会产生一个错误.正则表达式是捕获出现的最后一个@....因为对象和函数在此错误上生成不同的消息.而 $ 用于捕获静态对象、类和函数,因为它们的内存哈希和内存中的不同区域中没有@".

这个小代码对我来说很好用!现在我可以完成一些很棒的引擎,我正在使用内存管理、弱引用和基于内存的 ID.

希望能帮到你.

再见,祝你好运,我的朋友!

FlexBuilder's debugger will show you the "memory location" (or, I can only assume, something roughly analogous) of any in-scope instance:

But I'd like to get this information in code (sort of like Python's id function), so I could very easily trace how objects move through out the system. For example, I might have:

trace("Returning", id(foo));

Then somewhere else I could use:

trace("Using", id(foo));

To make sure both bits of code are dealing with the same instance.

Now, I know that many AS classes implement the IUID interface... But there are also a bunch of classes which don't (plain old arrays and objects, for example), so that wouldn't solve my problem.

I realize that I could also wrap objects in an ObjectProxy, but that would be less than ideal as well.

解决方案

In realy I advise to you don't to use this too much... it is very expensive. Adobe need to create a native function to return this to us.

But, for now... try this:

You will need to cause a explicit coercion to get it! Because when you make and explicit coercion you get an Error like this:

TypeError: Error #1034: 
Type Coercion failed: cannot convert Main@1c49d31 to flash.utils.ByteArray.

Note that in this error you get what you want... the @1c49d31. This hash is like an ID in the memory allocation.

I did a lot of tests. This hash just change when you call a "new" (in C languages is equivalent to [[... alloc] init]) and for static functions and static properties, the allocation occurs a little different... anyway...

Backing to Flash, the problem is we don't have a direct way to get this hash without an Error.

But this is not a realy big problem. All that you need is to use some "try" and "catch" Like this:

try
{
    ByteArray(anyObjectToKnowItAllocationHash);
}
catch (e:Error)
{
    trace(e);
}

And voila! You will get the hash without result in an Error! After this I did a more refinated way... Try this:

var memoryHash:String;

try
{
    FakeClass(anyObjectToKnowItAllocationHash);
}
catch (e:Error)
{
    memoryHash = String(e).replace(/.*([@|\$].*?) to .*$/gi, '$1');
}

internal final class FakeClass { }

A little explain about this: The fakeClass is to be sure about this will generate an Error. The RegularExpression is to capture the last @... that appear. Because Objects and Functions generate different messages on this Error. And the $ is to catch the Static Objects, Class and Functions, bacause they don't have an "@" in its memory hash and different zones in memory.

This little code works so fine to me! Now i can finish some great engines that i'm making that work with memory management, weak references and ID based on memory.

I hope this can help you.

Bye, and good luck, my friend!

这篇关于如何获取实例的“内存位置"?在动作脚本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆