Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

c - String literals causing small memory leak?

I have a C function that is called thousands of times and I have noticed that it is the only possible place for a memory leak. I have used Valgrind and Totalview's MemoryScape to check for leaks but they say nothing, yet when the application runs on AIX 6.1 there is a gradual leak.

NOTE: I am using a "leak free" SAP RFC SDK for this, and have spoken to the develops and they say the lib I am using is 100% leak free...so it is not that...

The code is roughly like follows, and is called thousands of times each hour....

int writeMessage(message msg){
...
...
header = ItCreate( "HEADER", HEADER_LEN, 0, 0 );
body = ItCreate( "DATA", DATA_LEN, 0, 0 );
...
...
ItDelete(it_header);
ItDelete(it_body);
...
}

Could those string literals like "HEADER" and "DATA" be causing my small leak? Stupid question but I would rather ask to be safe.

What would be the difference if I declared them as CONSTANTS in this specific case?

Thanks for the help, much appreciated

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No. A string literal has static storage duration and so its storage is reserved prior to the program startup and its lifetime is the entire execution of the program.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...