Memory leak in managed class!!!
-
Hello; I'm in trouble with memory leaks. Everthing going good until try to delete cphostname and cpclient at destructor. System has losing reference of objects that makes memory leak and annoying. How can i delete these variables from memory? Source code below.
public __gc class CSample { private: char* cphostname; char* cpclient; // Defines static const int HOST_NAME_SIZE = 17; static const int CLIENT_SIZE = 4; public: // Constructor & destructor CSapR3Rfc() { // Initialize member variables try { cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; } catch(...) { TRACE("CSapR3Rfc::CSapR3Rfc() - Memory allocation error"); } mode = trace = 0; } CSapR3Rfc(RFC_OPTIONS* m_opt) { // Initialize member variables try { cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; } catch(...) { TRACE("CSapR3Rfc::CSapR3Rfc() - Memory allocation error"); } CSapR3Rfc(CSapR3Rfc& __sapr3rfc) { // Initialize member variables cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; mode = trace = 0; } ~CSapR3Rfc(void) { // Release heap area delete [] cphostname; delete [] cpclient; }
System generate folowing message when processing the delete operator. ------------------------------------------------------------------ The thread 'Win32 Thread' (0x56c) has exited with code 2 (0x2). The thread 'Win32 Thread' (0x964) has exited with code 2 (0x2). Detected memory leaks! Dumping objects -> {74} normal block at 0x003457B8, 4 bytes long. Data: < > CD CD CD CD {73} normal block at 0x00345768, 17 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. The program '[2416] smc_server.exe' has exited with code 0 (0x0). The program '[2416] smc_server.exe: Native' has exited with code 2 (0x2). Ahmet Orkun GEDiK Technical Consultant ASTRON Project Office -
Hello; I'm in trouble with memory leaks. Everthing going good until try to delete cphostname and cpclient at destructor. System has losing reference of objects that makes memory leak and annoying. How can i delete these variables from memory? Source code below.
public __gc class CSample { private: char* cphostname; char* cpclient; // Defines static const int HOST_NAME_SIZE = 17; static const int CLIENT_SIZE = 4; public: // Constructor & destructor CSapR3Rfc() { // Initialize member variables try { cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; } catch(...) { TRACE("CSapR3Rfc::CSapR3Rfc() - Memory allocation error"); } mode = trace = 0; } CSapR3Rfc(RFC_OPTIONS* m_opt) { // Initialize member variables try { cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; } catch(...) { TRACE("CSapR3Rfc::CSapR3Rfc() - Memory allocation error"); } CSapR3Rfc(CSapR3Rfc& __sapr3rfc) { // Initialize member variables cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; mode = trace = 0; } ~CSapR3Rfc(void) { // Release heap area delete [] cphostname; delete [] cpclient; }
System generate folowing message when processing the delete operator. ------------------------------------------------------------------ The thread 'Win32 Thread' (0x56c) has exited with code 2 (0x2). The thread 'Win32 Thread' (0x964) has exited with code 2 (0x2). Detected memory leaks! Dumping objects -> {74} normal block at 0x003457B8, 4 bytes long. Data: < > CD CD CD CD {73} normal block at 0x00345768, 17 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. The program '[2416] smc_server.exe' has exited with code 0 (0x0). The program '[2416] smc_server.exe: Native' has exited with code 2 (0x2). Ahmet Orkun GEDiK Technical Consultant ASTRON Project OfficeBecause your class is garbage collected (__gc) you don't need to call the delete functions on your
char*
's the framework should automatically delete them for you. I think. - monrobot13 -
Hello; I'm in trouble with memory leaks. Everthing going good until try to delete cphostname and cpclient at destructor. System has losing reference of objects that makes memory leak and annoying. How can i delete these variables from memory? Source code below.
public __gc class CSample { private: char* cphostname; char* cpclient; // Defines static const int HOST_NAME_SIZE = 17; static const int CLIENT_SIZE = 4; public: // Constructor & destructor CSapR3Rfc() { // Initialize member variables try { cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; } catch(...) { TRACE("CSapR3Rfc::CSapR3Rfc() - Memory allocation error"); } mode = trace = 0; } CSapR3Rfc(RFC_OPTIONS* m_opt) { // Initialize member variables try { cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; } catch(...) { TRACE("CSapR3Rfc::CSapR3Rfc() - Memory allocation error"); } CSapR3Rfc(CSapR3Rfc& __sapr3rfc) { // Initialize member variables cphostname = new char[HOST_NAME_SIZE]; cpclient = new char[CLIENT_SIZE]; mode = trace = 0; } ~CSapR3Rfc(void) { // Release heap area delete [] cphostname; delete [] cpclient; }
System generate folowing message when processing the delete operator. ------------------------------------------------------------------ The thread 'Win32 Thread' (0x56c) has exited with code 2 (0x2). The thread 'Win32 Thread' (0x964) has exited with code 2 (0x2). Detected memory leaks! Dumping objects -> {74} normal block at 0x003457B8, 4 bytes long. Data: < > CD CD CD CD {73} normal block at 0x00345768, 17 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. The program '[2416] smc_server.exe' has exited with code 0 (0x0). The program '[2416] smc_server.exe: Native' has exited with code 2 (0x2). Ahmet Orkun GEDiK Technical Consultant ASTRON Project OfficeI feel your pain, since there is so little info on how __gc and __nogc stuff interacts in MC++. One thing is that any __gc class you create, should inherit from IDisposable when there are any heap (gc heap or regular heap) members to deallocate. Regular C++ destructors are not treated quite the same in __gc classes. For one thing, they are considered a last resort, and synonymous with Finalize methods defined in .Net. They are supposed to be declared protected to hide them from users, and should themselves just call the Dispose method that IDisposable descendants are supposed to implement. It is actually in the Dispose method where deallocation is supposed to occur (this all seems complicated, I know). Here is a simplified snippet from one of my __gc classes that implements IDisposable. I left in some comments from the .Net docs:
public __gc class GcClass : public IDisposable { char *m_pBuf; public: GcClass() { m_pBuf = new char __nogc[BUF_SIZE]; } void Dispose() { //required by IDisposable to be made avail to users Dispose(true); //true indicates user-called // Take ourselves off of the Finalization queue to prevent // finalization code for this object from executing a second time. GC::SuppressFinalize(this); } protected: ~GcClass() { //CLR dtors are supposed to be protected Dispose(false); //DO NOT delete[] m_pBuf here! } // Dispose(bool disposing) executes in two distinct scenarios. // If disposing equals true, the method has been called directly // or indirectly by a user's code. Managed and unmanaged resources // can be disposed. // If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. virtual void Dispose(bool bWeAreDisposing) { if(this->m_pBuf) { //if Dispose has not been called yet if(bWeAreDisposing) { //If disposing, dispose all gc resources. //only release managed resources here } //Release unmanaged resources. If bWeAreDisposing is false (called by runtime), // only the following code is executed. //Note that this is not thread safe. Another thread could start // disposing the object after the managed resources are disposed, // but before the disposed flag is set to true (same as m_pBuf set NULL in this class). dele