正确处置 SharePoint 对象?

发布于 2024-07-06 19:29:20 字数 554 浏览 8 评论 0原文

我希望这里有一位 SharePoint 专家可以帮助解决这个问题。

问题就在这里。 我的 SharePoint 日志多次包含此行:

在此线程结束之前未释放 SPRequest 对象。 为了避免浪费系统资源,请在使用完该对象或其父对象(例如 SPSite 或 SPWeb)后立即对其进行处理。 现在将处理该对象。 分配 ID:{8D090AD2-5D55-42C2-9873-2D5486FE257C} 要确定此对象的分配位置,请在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings 中创建一个注册表项。 然后在该键下创建一个名为 SPRequestStackTrace 的新 DWORD,其值为 1。

我编辑了注册表并添加了该键,但找不到堆栈跟踪。 它不在 SharePoint 日志或事件查看器中。

我确实需要找到这些 SPSite/SPWeb 泄漏的根源并修复它们,但我不能只是开始编辑可能是也可能不是问题根源的代码。 有没有人有什么建议?

I hope there's a SharePoint expert here on SO who can help with this.

Here's the problem. My SharePoint logs contain this line, several times:

An SPRequest object was not disposed before the end of this thread. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. This object will now be disposed. Allocation Id: {8D090AD2-5D55-42C2-9873-2D5486FE257C} To determine where this object was allocated, create a registry key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key.

I edited the registry and added the key, but the stacktrace is nowhere to be found. It's not in the SharePoint logs or in the Event Viewer.

I really need to find the source of these SPSite/SPWeb leaks and fix them, but I can't just start editing code that may or may not be the source of the problem. Does anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼波传意 2024-07-13 19:29:20

到目前为止,检查处置警告的最佳位置是:

http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns -by-example.aspx

在您的情况下,OpenWeb() 需要包装在 using 中。 如果您将其放置在您的 fianlly 块中,那么我建议您显示更多代码以查看您是否正在调用任何其他“陷阱”实例。 此外,如果某些 SPSite 和 SPWeb 对象是从 SPContext 类获取的,则无需处理它们。

如果您想跟踪对象的处置,您可以继承它们并重写 onload 和 ondispose 方法,以详细的消息传递方式记录它们。

阅读您的代码表明 SPWeb 对象是在 RunWithElevatedPrivileges 委托之外声明的。 这可能会影响 SharePoint 处理它们的方式。 通常建议对委托内的对象执行您需要执行的操作。

By far the best location for checking Disposal caveats is:

http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx

In your case OpenWeb() will need to be wrapped in a using. If you are disposing it in your fianlly block then I would suggest showing more code to see if you are calling any other "gotcha" instances. Also, it is unneccessary to dispose of certain SPSite and SPWeb objects if they are obtained from the SPContext class.

If you want to track disposals of the object you could inherit them and override the onload and ondispose methods to log them in a verbose messaging way.

Reading your code suggests that the SPWeb object is declared outside of the RunWithElevatedPriviledges delegate. This may have an effect on the way SharePoint disposes of them. It is generally suggested to do what you need to do to the object inside the delegate.

红衣飘飘貌似仙 2024-07-13 19:29:20

您需要重新启动受影响的进程(如果是 w3wp.exe 重新启动 IIS)以捕获注册表更改。

You need to restart the affected processes (if it is w3wp.exe restart IIS) to catch the registry change.

总攻大人 2024-07-13 19:29:20

来自 http://msdn.microsoft.com/en-us/library/前面提到的aa973248.aspx链接:

调用Response.Redirect不会执行finally块。 所以,
在发生任何重定向或转移处理之前,您必须处置
对象的数量。

鉴于您的示例代码,您仍然可能生成未释放的对象,因为 Dispose() 调用位于 finally 块中。

我的建议是将您的代码重新配置为以下内容:

try 
{
    //instantiate the SPSite and SPWeb with elevated privileges:    
    SPSecurity.RunWithElevatedPrivileges(delegate() 
    {
        using (SPSite mySite = new SPSite(url)) 
        {
            using (myWeb = mySite.OpenWeb()) 
            {
                //do stuff here
            }
        }
    });
}

如果您有多层Using语句,您可以像这样“堆叠”它们并减少代码缩进量(类似于if语句执行下一行或堵塞):

try 
{
    //instantiate the SPSite and SPWeb with elevated privileges:    
    SPSecurity.RunWithElevatedPrivileges(delegate() 
    {
        using (SPSite mySite = new SPSite(url)) 
        using (myWeb = mySite.OpenWeb()) 
        {
            //do stuff here
        }
    });
}

From the http://msdn.microsoft.com/en-us/library/aa973248.aspx link that was previously mentioned:

Calling Response.Redirect WILL NOT execute the finally block. Therefore,
before any redirection or transfer of processing can occur, you must dispose
of the objects.

Given your example code, you could still be generating objects that do not get disposed because the Dispose() call is in the finally block.

My suggestion would be to reconfigure your code to the following:

try 
{
    //instantiate the SPSite and SPWeb with elevated privileges:    
    SPSecurity.RunWithElevatedPrivileges(delegate() 
    {
        using (SPSite mySite = new SPSite(url)) 
        {
            using (myWeb = mySite.OpenWeb()) 
            {
                //do stuff here
            }
        }
    });
}

If you have multiple layers of Using statements, you can 'stack' them like this and reduce the amount the code gets indented (similar to the way an if statment executes the next line or block):

try 
{
    //instantiate the SPSite and SPWeb with elevated privileges:    
    SPSecurity.RunWithElevatedPrivileges(delegate() 
    {
        using (SPSite mySite = new SPSite(url)) 
        using (myWeb = mySite.OpenWeb()) 
        {
            //do stuff here
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文