新版主问题之1:BCB的内存管理

yesry 2004-04-12 10:46:11
出于“内存泄漏,内存自动回收”等目的考虑,对于BCB的内存管理作了一番研究,但是效果甚微。大家说一下应该如何实现像JAVA一样的内存自动回收?如何保证读写内存不超界?



//全局定义
TMemoryManager MyMemMMM,OldMem;

//我的内存管理
void * __fastcall MyGetMem(int iSize);//分配新内存
int __fastcall MyFreeMem(void *p);//释放
void * __fastcall MyReallocMem(void *p, int iSize);//从新分配


__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
GetMemoryManager(OldMem);
MyMemMMM.GetMem=MyGetMem;
MyMemMMM.FreeMem=MyFreeMem;
MyMemMMM.ReallocMem=MyReallocMem;
SetMemoryManager(MyMemMMM);
}
//---------------------------------------------------------------------------

void * __fastcall MyGetMem(int iSize)//分配新内存
{
//return LocalAlloc(LPTR,iSize);
return OldMem.GetMem(iSize);
}
int __fastcall MyFreeMem(void *p)//释放
{
//LocalFree(p);return 0;
return OldMem.FreeMem(p);
}
void * __fastcall MyReallocMem(void *p, int iSize)//从新分配
{
//MyFreeMem(p);return MyGetMem(iSize);
return OldMem.ReallocMem(p,iSize);
}
...全文
52 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rurama 2004-04-13
  • 打赏
  • 举报
回复
同意 jiangchun_xn(再回头·灯火依旧·人不见·潸然泪下) 的看法。:)

1. AllocMem

  在队中分配指定字节的内存块,并将分配的每一个字节初始化为 0.函数原型如下:

  void * __fastcall AllocMem(Cardinal Size);



2. SysFreeMem

  释放所指定的内存块.函数原型如下:

  int __fastcall SysFreeMem(void * P);



3. SysReallocMem

  要求重新分配参数Size所指定的内存.函数原型如下:

  void * __fastcall SysReallocMem(void * P , int Size);
jiangchun_xn 2004-04-12
  • 打赏
  • 举报
回复
估计用void *是不行的。至少要把指针包装成java的handle概念。
jiangchun_xn 2004-04-12
  • 打赏
  • 举报
回复
java的gc纯粹是个笑话在某种意义上,当java在复杂任务的时候,根本没有调度gc的时候,这时候内存被疯狂的吞噬。然而c++有了Smart Pointer,几乎可以保证没有Memory leak.

我不懂电脑 2004-04-12
  • 打赏
  • 举报
回复
再c++里可以使用auto_ptr避免忘记释放内存
Summary

A simple, smart pointer class.

Synopsis

#include <memory>
template <class X> class auto_ptr;

Description

The template class auto_ptr holds onto a pointer obtained via new and deletes that object when the auto_ptr object itself is destroyed (such as when leaving block scope). auto_ptr can be used to make calls to operator new exception-safe. The auto_ptr class has semantics of strict ownership: an object may be safely pointed to by only one auto_ptr, so copying an auto_ptr copies the pointer and transfers ownership to the destination if the source had already had ownership.

Interface

template <class X> class auto_ptr {
template <class Y> class auto_ptr_ref {
public:
const auto_ptr<Y>& p;
auto_ptr_ref (const auto_ptr<Y>&);
};
public:
typedef X element_type;
// constructor/copy/destroy
explicit auto_ptr (X* = 0) throw();
auto_ptr (const auto_ptr<X>&) throw ();
template <class Y>
auto_ptr (const auto_ptr<Y>&) throw();
void operator=(const auto_ptr<X>&) throw():
template <class Y>
void operator= (const auto_ptr<Y>&) throw();

~auto_ptr ();
// members
X& operator* () const throw();
X* operator-> () const throw();
X* get () const throw();
X* release () throw();
void reset (X*=0) throw();
auto_ptr(auto_ptr_ref<X>) throw();
template <class Y>
operator auto_ptr_ref<Y>() throw();
template <class Y>
operator auto_ptr<Y>() throw();
};

Types

template <class Y>
class auto_ptr_ref;

A private class template that holds a reference to an auto_ptr. It can only be constructed within an auto_ptr using a reference to an auto_ptr. It prevents unsafe copying.

Constructors

explicit
auto_ptr (X* p = 0);

Constructs an object of class auto_ptr<X>, initializing the held pointer to p, and acquiring ownership of that pointer. p must point to an object of class X or a class derived from X for which delete p is defined and accessible, or p must be a null pointer.

auto_ptr (const auto_ptr<X>& a);
template <class Y>
auto_ptr (const auto_ptr<Y>& a);

Constructs an object of class auto_ptr<X>, and copies the argument a to *this. If a owned the underlying pointer, then *this becomes the new owner of that pointer.

auto_ptr (const auto_ptr_ref<X> r);

Constructs an auto_ptr from an auto_ptr_ref.

Destructors

~auto_ptr ();

Deletes the underlying pointer.

Operators

void operator= (const auto_ptr<X>& a);
template <class Y>
void operator= (const auto_ptr<Y>& a);

Copies the argument a to *this. If a owned the underlying pointer, then *this becomes the new owner of that pointer. If *this already owned a pointer, then that pointer is deleted first.

X&
operator* () const;

Returns a reference to the object to which the underlying pointer points.

X*
operator-> () const;

Returns the underlying pointer.

template <class Y>
operator auto_ptr_ref<Y> ();

Constructs an auto_ptr_ref from *this and returns it.

template <class Y>
operator auto_ptr<Y> ();

Constructs a new auto_ptr using the underlying pointer held by *this. Calls release() on *this, so *this no longer possesses the pointer. Returns the new auto_ptr.

Member Functions

X*
get () const;

Returns the underlying pointer.

X*
release();

Releases ownership of the underlying pointer. Returns that pointer.

void
reset(X* p)

Sets the underlying pointer to p. If non-null, deletes the old underlying pointer.

Example

//
// auto_ptr.cpp
//
#include <iostream>
#include <memory>
using namespace std;
//
// A simple structure.
//
struct X
{
X (int i = 0) : m_i(i) { }
int get() const { return m_i; }
int m_i;
};
int main ()
{
//
// b will hold a pointer to an X.
//
auto_ptr<X> b(new X(12345));
//
// a will now be the owner of the underlying pointer.
//
auto_ptr<X> a = b;

//
// Output the value contained by
// the underlying pointer.
//
cout << a->get() << endl;
//
// The pointer will be deleted when a is destroyed on
// leaving scope.
//
return 0;
}

Program Output

12345
thp 2004-04-12
  • 打赏
  • 举报
回复
..
kpld8888 2004-04-12
  • 打赏
  • 举报
回复
多用C++标准的容器类
zhaoxinghan 2004-04-12
  • 打赏
  • 举报
回复
看不明白,俺现在还没到这个高度。
yunuo2010000 2004-04-12
  • 打赏
  • 举报
回复
学习
yanjing01 2004-04-12
  • 打赏
  • 举报
回复
一切尽在学习中,^_^
JetKingLau 2004-04-12
  • 打赏
  • 举报
回复
标记,便于将来学习。
gqxs 2004-04-12
  • 打赏
  • 举报
回复
关注
constantine 2004-04-12
  • 打赏
  • 举报
回复
进来学习

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧