释放非托管STL对象

问题描述:

HI,


假设我有一个类(在C ++上),它有一个STRMAP的非托管STL成员,它是

map< string的定义,string&gt ;.


class CL

{

public CL(){mp = new STRMAP()};

public void Dispose(删除mp);

STRMAP * mp; //未损坏的对象

}


成员影响类对象如何足以删除mp删除stl对象表格

内存? GC会收集这样的非管理吗?


谢谢,

Andrey



Say i have a class(on C++) which has an unmanaged STL member of STRMAP which is a define for
map<string, string>.

class CL
{
public CL(){mp = new STRMAP()};
public void Dispose(delete mp);
STRMAP *mp; //unmamaged object
}

member affects how the class objectIs it enough to just "delete mp" to remove the stl object form
memory? Does having such an unmanaged wil be collected by GC?

Thank you,
Andrey

由于它是一个非托管成员,你必须手动清理它,

但是我注意到你没有使用终结器。这意味着如果有人在没有调用Dispose的情况下取消引用你的课程,你就会泄漏地图。

关于完美的方法有一些很大的争论回来的时候,和b $ b我不记得我的方式是否足够好,但是你走了:


ref class CL: IDisisable

{

public CL(){mp = new STRMAP()};


!CL(){

Dispose();

}

public void Dispose(){

delete mp;

SupressFinalize(this);

}


STRMAP * mp; //没有损坏的对象

}


我无法判断你是使用C ++托管扩展,还是使用来自
$ b的C ++ / CLI $ b你的语法,但它对我来说看起来更像C ++ / CLI,所以这就是我使用的语法。


" MuZZy" &LT;乐******* @ yahoo.com&GT;在消息中写道

新闻:SK ******************** @ comcast.com ...
Since it''s an unmanaged member, you will have to clean it up manually,
however I note that you don''t use a finalizer. This means that if someone
dereferences your class without ever calling Dispose, you''ll leak the map.
There was some huge argument on the perfect way to do this a while back, and
I can''t recall if my way''s good enough or not, but here you go:

ref class CL : IDisposable
{
public CL(){mp = new STRMAP()};

!CL() {
Dispose();
}
public void Dispose() {
delete mp;
SupressFinalize(this);
}

STRMAP *mp; //unmamaged object
}

I couldn''t tell if you were using C++ Managed Extension, or C++/CLI from
your syntax, but it looked more C++/CLI to me, so that''s the syntax I used.

"MuZZy" <le*******@yahoo.com> wrote in message
news:SK********************@comcast.com...
HI ,
说我有一个类(在C ++上),它有一个STRMAP的非托管STL成员
,它是map< string,string>的定义。

CL级
公共CL(){mp =新STRMAP()};
public void Dispose(删除mp);
STRMAP * mp; //未损坏的对象


成员影响类对象如何足以删除mp
删除stl对象形式的内存? GC会收集这样一个不受管理的人吗?

谢谢你,
Andrey


Say i have a class(on C++) which has an unmanaged STL member of STRMAP
which is a define for map<string, string>.

class CL
{
public CL(){mp = new STRMAP()};
public void Dispose(delete mp);
STRMAP *mp; //unmamaged object
}

member affects how the class objectIs it enough to just "delete mp" to
remove the stl object form memory? Does having such an unmanaged wil be
collected by GC?

Thank you,
Andrey





" Sean Hederman" &LT;我们*** @ blogentry.com&GT;在消息中写道

news:cu ********** @ ctb-nnrp2.saix.net ...

"Sean Hederman" <us***@blogentry.com> wrote in message
news:cu**********@ctb-nnrp2.saix.net...
因为它是一个不受管理的会员,你必须手动清理它,但是我注意到你没有使用终结器。这意味着,如果有人在没有调用Dispose的情况下取消引用你的课程,那么你就会泄漏地图。
对于一段时间内完成此类工作的完美方法存在一些巨大的争论,
和我不记得我的方式是否足够好,但是你去了:

ref class CL:IDisposable
{
public CL(){mp =新的STRMAP()};

!CL(){
Dispose();
}
public void Dispose(){
删除mp;
SupressFinalize(this);
}

STRMAP * mp; //没有损坏的对象

我无法判断你是使用C ++ Managed Extension,还是从你的语法中使用C ++ / CLI,但它看起来更像是C ++ / CLI对我来说,这就是我使用的语法。
Since it''s an unmanaged member, you will have to clean it up manually,
however I note that you don''t use a finalizer. This means that if someone
dereferences your class without ever calling Dispose, you''ll leak the map.
There was some huge argument on the perfect way to do this a while back,
and I can''t recall if my way''s good enough or not, but here you go:

ref class CL : IDisposable
{
public CL(){mp = new STRMAP()};

!CL() {
Dispose();
}
public void Dispose() {
delete mp;
SupressFinalize(this);
}

STRMAP *mp; //unmamaged object
}

I couldn''t tell if you were using C++ Managed Extension, or C++/CLI from
your syntax, but it looked more C++/CLI to me, so that''s the syntax I
used.




IMO OP并没有说它是用托管C ++编写的,例如你是* b $ b显示是基于(未发布的)C ++ / CLI语言修订版,OP''

应用程序不是使用C ++ / CLI编写的(我想)。 />

Willy。



IMO OP didn''t say it was written using managed C++, the example you are
showing is based on the (unreleased) C++/CLI language revision, OP''s
application is not written using C++/CLI (I suppose).

Willy.


看起来对我来说,课前没有__gc,访问权限

修饰符是内联的。不可否认OP也没有使用ref类,所以只是

以防万一,这里是托管扩展版本(我希望...,我有点生疏)


__gc class CL:IDisposable

{

public:

CL(){mp = new STRMAP( )};


void Dispose(){

删除mp;

SupressFinalize(this);

}


受保护:

虚拟无效Finalize(){

Dispose();

}


私人:

STRMAP * mp; //未受损害的对象

}


Willy Denoyette [MVP]" &LT;无线************* @ pandora.be&GT;在消息中写道

新闻:ey ************** @ TK2MSFTNGP10.phx.gbl ...
Looks like it to me, there''s no __gc in front of class, and the access
modifiers are inline. Admittedly the OP also isn''t using ref class, so just
in case, here''s the managed extensions version (I hope..., I''m a bit rusty)

__gc class CL : IDisposable
{
public:
CL(){mp = new STRMAP()};

void Dispose() {
delete mp;
SupressFinalize(this);
}

protected:
virtual void Finalize(){
Dispose();
}

private:
STRMAP *mp; //unmamaged object
}

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...

" ; Sean Hederman &LT;我们*** @ blogentry.com&GT;在消息中写道
新闻:cu ********** @ ctb-nnrp2.saix.net ...

"Sean Hederman" <us***@blogentry.com> wrote in message
news:cu**********@ctb-nnrp2.saix.net...
由于它是一个无管理的成员,你将必须手动清理它,但是我注意到你没有使用终结器。这意味着如果某人在没有调用Dispose的情况下取消引用您的课程,您将泄露
地图。关于完美做这件事的方法有一些很大的争论
回来了,我不记得我的方式是否足够好,但是你走了:
> ref class CL:IDisposable
公共CL(){mp =新STRMAP()};

!CL(){
Dispose();
}
public void Dispose(){
删除mp;
SupressFinalize(this);
}

STRMAP * mp; //没有损坏的对象

我无法判断你是使用C ++ Managed Extension,还是从你的语法中使用C ++ / CLI,但它看起来更像是C ++ / CLI对我来说,这就是我使用的语法。
Since it''s an unmanaged member, you will have to clean it up manually,
however I note that you don''t use a finalizer. This means that if someone
dereferences your class without ever calling Dispose, you''ll leak the
map. There was some huge argument on the perfect way to do this a while
back, and I can''t recall if my way''s good enough or not, but here you go:

ref class CL : IDisposable
{
public CL(){mp = new STRMAP()};

!CL() {
Dispose();
}
public void Dispose() {
delete mp;
SupressFinalize(this);
}

STRMAP *mp; //unmamaged object
}

I couldn''t tell if you were using C++ Managed Extension, or C++/CLI from
your syntax, but it looked more C++/CLI to me, so that''s the syntax I
used.



IMO OP并没有说它是用托管C ++编写的,你的例子是
显示基于(未发布的)C ++ / CLI语言版本,OP'的应用程序不是使用C ++ / CLI编写的(我想)。

Willy。



IMO OP didn''t say it was written using managed C++, the example you are
showing is based on the (unreleased) C++/CLI language revision, OP''s
application is not written using C++/CLI (I suppose).

Willy.