内存泄漏 - 你能帮我找到它们的片段吗? [英] Memory Leaks - Can you help me find them in ths snippet

查看:86
本文介绍了内存泄漏 - 你能帮我找到它们的片段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些与本机C库交互的c ++代码,并且
必须进行一些动态内存分配才能支持它。我得到了内存泄漏的内存泄漏,我认为这段代码是罪魁祸首。可以

有人告诉我我可能做错了吗?


char **栏;

栏=(CHAR ** )malloc(lColumnCount * sizeof(CHAR *));


if(NULL == columns)返回FALSE; //内存分配失败


//为char中的列名分配空间**

int column_index;

for (column_index = 0; column_index< lColumnCount; column_index ++)

{

//为最大owner.table.column表示法分配

列[column_index] =(char *)malloc(SE_QUALIFIED_COLUMN_LEN);


if(NULL == columns [column_index])

{

delete []列; //内存分配失败

返回FALSE;

}

}


//把(** column_index = 0; column_index< lColumnCount; column_index ++)

{

strcpy(columns [column_index]}中的数据,CStringColumnName [column_index]);

}


//用列做一些东西

....... 。


删除[]列;


妮可

解决方案

< blockquote> nm******@gmail.com 写道:


我正在编写一些与本机C库交互的c ++代码,并且
必须进行一些动态内存分配才能支持它。我得到了内存泄漏的内存泄漏,我认为这段代码是罪魁祸首。可以

有人告诉我我可能做错了吗?


char **栏;

栏=(CHAR ** )malloc(lColumnCount * sizeof(CHAR *));

[..]


delete [] columns;



不要混合''malloc / free''和'new / delete''(或''new [] / delete []'' )。

成对出现,成对使用。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


谢谢,我想知道这是不是一个问题。我继续进行改变,但仍然有内存泄漏。任何人都有任何想法

我?我知道它命中了免费(列)在代码的最后

片段。


char **列;

列=(CHAR **)malloc( lColumnCount * sizeof(CHAR *));


if(NULL == columns)返回FALSE; //内存分配失败


//为char中的列名分配空间**

int column_index;

for (column_index = 0; column_index< lColumnCount; column_index ++)

{

//为最大owner.table.column表示法分配

列[column_index] =(char *)malloc(SE_QUALIFIED_COLUMN_LEN);


if(NULL == columns [column_index])

{

免费(列); //内存分配失败

返回FALSE;

}

}

//将数据放入char **

for(column_index = 0; column_index< lColumnCount; column_index ++)

{

strcpy(columns [column_index],CStringColumnName [column_index ]);

}

//用列做一些东西

........

免费(列);


On 2008-01-28 12:35:34 -0500," nm ****** @ gmail .COM" < nm ****** @ gmail.comsaid:


//为最大owner.table.column表示法分配

columns [column_index] =(char *)malloc(SE_QUALIFIED_COLUMN_LEN);



这里分配的内存永远不会被释放。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者;

标准C ++库扩展:教程和参考

www.petebecker.com/tr1book


I am writing some c++ code that interacts with a native C library and
have to do some dynamic memory allocation to support it. I am getting
memory leaks and I think this piece of code is the culprit. Can
anyone tell me what I might be doing wrong?

char **columns;
columns = (CHAR **)malloc (lColumnCount * sizeof (CHAR *));

if (NULL == columns) return FALSE; //memory allocation failed

//allocate space for the column names in the char**
int column_index;
for (column_index = 0; column_index < lColumnCount; column_index++)
{
// Allocate for the maximum owner.table.column notation
columns[column_index] = (char *)malloc(SE_QUALIFIED_COLUMN_LEN);

if (NULL == columns[column_index])
{
delete[] columns; //memory allocation failed
return FALSE;
}
}

//put the data in the char**
for (column_index = 0; column_index < lColumnCount; column_index++)
{
strcpy (columns[column_index], CStringColumnName[column_index]);
}

//do some stuff with columns
........

delete[] columns;

Nicole

解决方案

nm******@gmail.com wrote:

I am writing some c++ code that interacts with a native C library and
have to do some dynamic memory allocation to support it. I am getting
memory leaks and I think this piece of code is the culprit. Can
anyone tell me what I might be doing wrong?

char **columns;
columns = (CHAR **)malloc (lColumnCount * sizeof (CHAR *));
[..]

delete[] columns;

Don''t EVER mix ''malloc/free'' and ''new/delete'' (or ''new[]/delete[]'').
Those come in pairs, use them in pairs.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Thanks, I was wondering if that was a problem. I went ahead and made
the change, but still have memory leaks. Anyone have any ideas for
me? I know it hits the "free(columns)" at the end of the code
snippet.

char **columns;
columns = (CHAR **)malloc (lColumnCount * sizeof (CHAR *));

if (NULL == columns) return FALSE; //memory allocation failed

//allocate space for the column names in the char**
int column_index;
for (column_index = 0; column_index < lColumnCount; column_index++)
{
// Allocate for the maximum owner.table.column notation
columns[column_index] = (char *)malloc(SE_QUALIFIED_COLUMN_LEN);

if (NULL == columns[column_index])
{
free(columns); //memory allocation failed
return FALSE;
}
}
//put the data in the char**
for (column_index = 0; column_index < lColumnCount; column_index++)
{
strcpy (columns[column_index], CStringColumnName[column_index]);
}
//do some stuff with columns
........

free(columns);


On 2008-01-28 12:35:34 -0500, "nm******@gmail.com" <nm******@gmail.comsaid:

// Allocate for the maximum owner.table.column notation
columns[column_index] = (char *)malloc(SE_QUALIFIED_COLUMN_LEN);

The memory allocated here never gets freed.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于内存泄漏 - 你能帮我找到它们的片段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆