4/7/2011 2:18 PM | |
Posts: 12 Rating:
|
Hello, Some WinCC functions (for example, GetTagChar) return a pointer to a string (for example, char* or LPCSTR as returned value). P.S. I didn't found an answer to the question either in https://support.automation.siemens.com/WW/llisapi.dll?func=cslib.csinfo&lang=en&objid=26728674&caller=viewor any other topics on the forum. |
Last edited by: Alexey_Spb at: 4/7/2011 2:21 PM |
|
This contribution was helpful to
1 thankful Users |
4/20/2011 1:56 PM | |
Posts: 12 Rating:
|
Does anybody know the answer? |
4/21/2011 8:41 AM | |
Joined: 1/17/2006 Last visit: 2/18/2025 Posts: 800 Rating:
|
I would do it like this, to prevent a leak |
Last edited by: Jaapie at: 4/21/2011 8:41 AM |
|
This contribution was helpful to
1 thankful Users |
4/24/2011 8:31 AM | |
Posts: 12 Rating:
|
Thank you for your reply, Jaapie! Your code is a good way to handle pointers, which are returned by the internal WinCC functions. But even in your example GetTagChar () may also allocate memory for the values of the tag 'ProcessValue_1, but the pointer transferred into strcpy() as argument.. Do you think the memory is released later? The most likely it seems that the memory inside GetTagChar () function is allocated by SysMalloc() and is automatically released upon completion of the action by SysFree () I gonna make some tests to ensure do that functions leak or not and post results... |
Last edited by: Alexey_Spb at: 4/24/2011 8:31 AM |
|
4/26/2011 12:51 AM | |
Posts: 55 Rating:
|
Hi Alexey there is no leak in the code after the function ends, because you are not allocating memory dynamically for the array. Your array is static (not dynamic). You reserve memory when you define the array, but it will be freed when the function ends. Lets have a look at your code. char*pszTagValue; Here you are declaring a pointer to a char, that means that pszTagValue will contain the address of the memory where the char is allocated. This pointer in memory occupies 32 bits (or 64 depending on the computer architecture). This is only a declaration, that means that the value of the pointer is what the memory had in that moment which is random number. charszData[STR_SIZE+1]; This is the declaration of the array. You are saying to the compiler that reserves (STR_SIZE + 1) chars in memory. pszTagValue=GetTagChar(TAG_PV); This declaration places the memory address returned by the function GetTagChar into the pointer variable pszTagValue. This address in memory contains the Tag Value. strncpy(szData,pszTagValue,STR_SIZE); The Tag Value is copied in the array. After the function ends, the pointer is released and the array is destroyed since they are out of the scope of the function. You would need to free the array if it would have been defined with malloc (dynamic array). But for static arrays there is no need. Static arrays are created on the stack, and the have a fixed size and you don't need to deal with the memory, they will be destroyed when the function ends. And the pointer contains only an address, and once the function ends will have no sense, since the array is destroyed. Regards, Marta |
This contribution was helpful to
2 thankful Users |
4/26/2011 9:14 AM | |
Posts: 12 Rating:
|
Thank you for your reply, Marta! You are completely right about static (in our case, more correctly, automatic allocated) arrays. They do not cause leaks. But the problem is that we do not know how GetTagChar() and other same functions allocate memory for returning value. Let's imagine an example of a function that returns a pointer to a string: char *IntToStr(int Argument) { char* pResult = malloc(...); // The memory for output string must be allocated first. // Then we place the data to the address pointed to by pResult. .... return pResult; // And return the pointer. } So this code [u]will[/u] cause leaks: void Foo { char* pszReturned; char szString[255]; pszString = IntToStr(12345); strncpy(szString, pszReturned, sizeof szString - 1); // Doing something with szString } szString [u]is released[/u] after Foo() is completed. But the leak is caused by IntToStr() call. I see three ways to solve that problem: 1) Place 'free(pszReturned);' after strncpy call. 2) Replace malloc() in IntToStr() by SysMalloc(). So the memory will be released after the action ends. 3) Replace IntToStr() definition with: void IntToStr(int Argument, char* OutputBuffer) . In this case, memory is allocated by the caller and is released after the function ends. GetTagChar () looks like the function described in this example. I think it uses an algorithm, described in case 2 (SysMalloc), to avoid leaks, but I'm not sure. You wrote:
As far as I know, Ansi C compilers do not automatically release memory associated with the pointer at the end of the function. In WinCC, it is implemented differently? Regards, Alexey. |
Last edited by: Alexey_Spb at: 4/26/2011 9:18 AM |
|
4/26/2011 10:47 AM | |
Posts: 55 Rating:
|
Hi Alexey, I see what you mean. If the function is defined like you said:
you are right, there will be memory leaks. Therefore you would need to free the pointer, unless the developer do it later. Lets think that the developers of the library have solved the memory leaks for this functions, at least I think that is how it should be.
I don't know how the C Compiler of WinCC works, and maybe the term "release" in the sentence is not correct. What I was trying to say is that you don't have to worry about this pointer. I have done a test (using Visual C compiler). void basic_pointer(){ int * pointer; int a = 10: pointer = &a; } int main(int argc, _TCHAR* argv[]) { basic_pointer(); //call the function first time basic_pointer(); //call the function second time return 0; } Both times the function basic_pointer() is called, when the pointer is declared, the compiler assigns the same address for the pointer. That means that the compiler can reuse that address memory again. The compiler does not assign a different address each time, he doesn't think that address is not free (or "released"). One question: What happends when you use free? do you get an error? Have you tried to use a memory leak detection function? (like _CrtDumpMemoryLeaks()) Regards, Marta |
4/28/2011 8:10 AM | |
Posts: 12 Rating:
|
Hi Marta! Thank you very much!
When I said "to release memory", I was not referring to its cleansing, but the opportunity for the compiler to use it in the future. And your example is fully compatible with this. At the beginning of the function program automatically allocate memory at a specific address for the variable, after the completion of the function that memory is released (I mean the program may use it again). The second call repeats all over again. The same address mabe be assigned to the same variables in different function calls, but in general, this is not guaranteed (this is just my suggestion).
Yes, I tried it.. and it didn't cause an error! So I suggested that GetTagChar allocates memory using SysMalloc()
Unfortunately not, since I'm new to WinCC (and ANSI C programming). What library contains declaration of that function? Regards, |