My question is about AnsiString in Borland C++Builder 6.0
As you know, VCL's AnsiString uses reference counting.
Is that reference counting thread safe?
For example, suppose we have std::queue<AnsiString> asq;
We push an AnsiString value in that queue in thread#1, and then we do asq.pop() in thread#2. (The access to asq itself is synchronized.) Strings are not modified. Is it possible I will have a memory leak when AnsiString's destructor is called when using AnsiString(s) that way?
Yes, the refcount is thread-safe. The RTL places a lock on the refcount whenever it is being incremented and decremented. There will not be any memory leak in the scenario you describe.
Related
I am trying to Run the code but its reporting the memory leaks when using static analyzer. on this line as Potential leak of an object stored into 'encodedData'
return encodedData;
use __bridge_transfer
Using __bridge_transfer ensures that ARC will release the object for you. Without __bridge_transfer, you must release the returned object manually.
__bridge,__bridge_transfer keywords are used to tell to ARC system how to handle your non-objective-c pointers. In essence, if you use __bridge, you are telling to ARC not to deal with the ownership of the converted pointer because you will free it from non-objective-c code, most likely with a free() or a CFRelease... type function. __bridge_transfer, on the other hand, transfers the ownership to ARC and ARC will free your objective-c (and thus also the original non-objective-c) object via the standard release mechanism when the references to that object hits zero.
Reference
The problem is that you create your string using CoreFoundation methods. And by default ARC doesn't know what to do with it. So, you're responsible for either manually managing the memory for the created object (using CFRelease for example), or handing it over to ARC.
The later is, I believe, the way to go in your case. You can do it, as others have already noted, using __bridge_transfer.
According to Using Swift with Cocoa and Objective-C you can use takeUnretainedValue() and takeRetainedValue()to tell Swift how to manage the memory of an object for a function like this:
func StringByAddingTwoStrings(CFString!, CFString!) -> Unmanaged<CFString>!
When do I have to use takeUnretainedValue() or takeRetainedValue()?
When I use ARC is it then always takeUnretainedValue()?
You use takeRetainedValue when the unmanaged object has a +1 retain count and you want ARC to take care of releasing the object when you're done. For example, if you call a Core Foundation function with Create or Copy in the name (see Create Rule in the Memory Management Programming Guide for Core Foundation) which returns an unmanaged object for which you are responsible for releasing, you generally use takeRetainedValue so that it is released for you (or, if you don't do this, you have to manually release it yourself with CFRelease or similar function). You use takeUnretainedValue when ownership of the object has not been transferred to you and you therefore do not want ARC releasing the object for you when it falls out of scope.
So, as to when you call takeUnretainedValue vs takeRetainedValue, it simply depends upon what sort of object the called function returns. As a general rule of thumb, if the object was returned from a Core Foundation function with Create or Copy in the name, use takeRetainedValue. Otherwise use takeUnretainedValue.
In terms of what happens if you call the wrong method, if you call takeUnretainedValue when you're passed a +1 object (e.g. an object returned from Core Foundation function with Create or Copy in the name), your app will leak unless you explicitly CFRelease it. You may not immediately notice the occasional leak when running the app, but it can be observed by watching your app's memory usage (e.g. if you profile your app with Instruments). But if you leave these leaks unresolved, your app may eventually receive memory warnings.
On the other hand, if you call takeRetainedValue on an object which has not been retained for you (returned by a function that did not have Create or Copy in its name), the app will likely crash when the object is released. Sometimes this won't manifest itself immediately (not until the last strong reference is resolved), but it will generally result in a catastrophic failure of the app.
So judicious selection of takeUnretainedValue vs takeRetainedValue is very important.
Quoting from NSHipster:
https://nshipster.com/unmanaged/
An Unmanaged instance wraps a CoreFoundation type T, preserving a reference to the underlying object as long as the Unmanaged instance itself is in scope. There are two ways to get a Swift-managed value out of an Unmanaged instance:
takeRetainedValue() returns a Swift-managed reference to the wrapped instance, decrementing the reference count while doing so—use with the return value of a Create Rule function.
takeUnretainedValue() returns a Swift-managed reference to the wrapped instance without decrementing the reference count—use with the return value of a Get Rule function.
I am using iOS 7 and I am getting memory warnings in this part of code, but I'am not able to understand how the retain count increases as I release imageToSave variable too.
Static analyser not always write about memory leak but its assuming that when you are calling finalOutput its returning you an allocated object mean +1 retain count object which is never used and its treating this waring as memory leak!
You have not allocated memory to this object yourself, so you don't own it. And you are still releasing it.You can't release objects you don't own.
Unless you use alloc method to allocate memory, you can't just release them........
I need to ReAllocMem a block, but the extension should be zero'ed.
For this reason ReAllocMem is no good:
From the helpfile:
ReallocMem reallocates a memory block.
[...]
The content of the newly allocated memory is not set to zero.
I've looked at ReAllocMemory, but the help does not state anything about zeroing the new allocation, it only states:
Note: ReallocMemory is the version of ReallocMem compatible with C++.
Is there an alternative that does zero the newly allocated memory?
The simple answer is no. The only raw memory reallocator is ReallocMem. There is nothing else. The design of a re-allocator is always to preserve the contents that were there before. You will have to write your own routine.
David is right but using WinAp it's possible: you can use Global Alloc and GlobalReAlloc using the GMEM_ZEROINIT flag.
I'm having trouble understanding what's happening here. I am debugging an app because I think it's retaining a class 'DownloadController' after its been used. Using ARC, it should automatically release the object when the Reference Count is zero.
So in instruments it looks like
From that I assume, that 48 bytes have are still 'live' and being used, correct?
So I try to find what is causing this object to be retained, when its no longer needed, by using reference count traces.
At the bottom it says, RefCt is zero; so why has the object not been released? How can I debug further, why it hasn't been released?
These ARC anomilies were being caused by NSZombieEnabled, after removing this argument it started freeing objects.