Can any body suggest me how can I free the dynamic memory in the given code

int fun(CString qry, CStringList *List,int n)
{
    using namespace std;
	string sqlStmt = qry;
	int i;
	this->stmt = this->conn->createStatement (sqlStmt);
	ResultSet *rset = this->stmt->executeQuery ();
	string **oraRecordsStr=NULL;
	try 
	{	
		while (rset->next())
		{
			if(oraRecordsStr == NULL)
			{
				oraRecordsStr=new string*[n];
				for(i=0;i<n;i++)
					oraRecordsStr[i] = new string;
			}
		
			for(i=0;i<n;i++)
			{
				*oraRecordsStr[i]= rset->getString (i+1);
				List->AddTail(oraRecordsStr[i]->c_str());
			}

			for(i=0;i<n;i++)
				oraRecordsStr[i] = NULL;

			oraRecordsStr=NULL;
		}
		
	}
}

i tried

for(i=0;i<n;i++)
       delete []oraRecordsStr[i];
       delete []oraRecordsStr;

but it didn't work

Thanks in advance

Dani AI

Generated

Short summary and practical notes based on the posted thread: the crash/leak came from a mix of two mistakes (mismatched delete/new forms, and nulling pointers before freeing). gave the right diagnosis and the corrected deletion sequence fixed the immediate problem for , but a few important clarifications and safer patterns are worth adding.

The delete operator must match how the memory was allocated: objects created with new T must be freed with delete, while arrays created with new T[n] must be freed with delete[]. Using the wrong form is undefined behavior and can crash or corrupt memory; deleting a null pointer is safe, but assigning NULL before calling delete simply leaks the original allocation. See the language reference on the delete operator for the formal rules: operator delete — cppreference.

A second, subtle problem to check: handing c_str() pointers into another container. The pointer returned by std::string::c_str() remains valid only while the original std::string exists and is not modified; if the receiving container stores only that raw pointer and the std::string is later destroyed, the container will hold a dangling pointer. Confirm that CStringList::AddTail makes a copy of the string data (or explicitly pass a copy) so stored data does not become invalid when the temporary string is deleted. See std::string::c_str() lifetime rules: basic_string::c_str — cppreference.

For more robust, exception-safe code, prefer RAII containers and smart pointers (for example, std::vector<std::string> or std::unique_ptr wrappers) instead of manual new/delete. These eliminate manual cleanup logic, prevent leaks on exceptions, and make intent much clearer: std::vector — cppreference.

Recommended Answers

All 6 Replies

How about

for(i=0;i<n;i++)
    delete oraRecordsStr[i];
delete [] oraRecordsStr;

?

How about

for(i=0;i<n;i++)
    delete oraRecordsStr[i];
delete [] oraRecordsStr;

?

sorry ...but it didn't work

Well, "didn't work" is not very meaningful.

Btw.: In lines 26-29 you set all pointers to NULL. That will of course make it impossible to delete them anymore.

Well, "didn't work" is not very meaningful.

Caligulaminus you rock!!!
Your solution worked like a charm.I misplaced the code you gave previously.But after replacing the code with highlighted lines it worked absolutely fine. If you could explain why it was not working previously i.e. the code in my original post, I will be highly thankful.

int fun(CString qry, CStringList *List,int n)
{
    using namespace std;
	string sqlStmt = qry;
	int i;
	this->stmt = this->conn->createStatement (sqlStmt);
	ResultSet *rset = this->stmt->executeQuery ();
	string **oraRecordsStr=NULL;
	try 
	{	
		while (rset->next())
		{
			if(oraRecordsStr == NULL)
			{
				oraRecordsStr=new string*[n];
				for(i=0;i<n;i++)
					oraRecordsStr[i] = new string;
			}
 
			for(i=0;i<n;i++)
			{
				*oraRecordsStr[i]= rset->getString (i+1);
				List->AddTail(oraRecordsStr[i]->c_str());
			}
 
			for(i=0;i<n;i++)
			delete oraRecordsStr[i];
			delete [] oraRecordsStr;
			oraRecordsStr=NULL;
		}
 
	}
}

You must use delete [] only for new type[]. But you trieed to use it to free each individual string.

delete []oraRecordsStr[i]; // <- wrong

And you must not set the pointer to NULL before deleting it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.