# Xcode Zombies - Huge memory leaks

**URL:** https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760
**Category:** C#
**Created:** [August 28, 2015, 12:45am UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760 "2015-08-28T00:45:06Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [August 28, 2015, 12:45am UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/1 "2015-08-28T00:45:06Z")

</div>

Hello masters 😄

It’s possible to use zombies for instruments from VS? Xcode docs says to enable it in Product\>scheme\>edit scheme\>diagnostic\>Enable Zombie Objects or add NSZombieEnabled=YES to environment variables section. Both are in xcode available by running xcodeproj. So questions is, if I’ll enable it in xcode, compile with VS then it should works? I cannot manage it either do not have idea where to set NSZombieEnabled in VS. It’s even possible?

b.r.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [August 28, 2015, 12:22pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/2 "2015-08-28T12:22:36Z")

</div>

`NSZombieEnabled` isn not currently exposed as option in VS, i’m afraid. Your best option would be to run in Instruments w/ the Zombies template. `NSZombieEnabled` is a simple environment variable, so you can also set that yourself manually when running your app from Terminal, say.

---

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [August 28, 2015, 1:15pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/3 "2015-08-28T13:15:24Z")

</div>

Please can You show me how? I’m not mac/xcode expert. I’m trying to find problem in my app. It crash after 10 min. of activity. I realized that strange crash is related to out of memory. I did some test and I don’t understand why instruments report leaks for this project. I even put code in \_\_autoreleasepool.

[Zombies.zip](https://talk.remobjects.com/uploads/default/original/2X/b/baa1be26fa77157f80ad3f14439f78d95e10e59d.zip) (117.2 KB)

It’s simple loop with create and destroy 10000 object. Instruments report 92415 leaks. In my app instruments alloc 35 GB of ram after 20 sec. of work, so is not possible to trace it, that’s why other test.

 ![](https://talk.remobjects.com/uploads/default/original/2X/6/68a3862d214b054a7f133bcbbf0fa720c44a5dd1.png) 

[EDIT]

I did same project in pure Objective-C and it didn’t leak. Did same iterations and Objective-C persistent mem = 1,5MB, RO = 17,01MB and 3 huge memory leaks.

iOS app: [testapp2.zip](https://talk.remobjects.com/uploads/default/original/2X/2/2c02e23ecc29ad06a7b68225f6efae7d023df86f.zip) (15.5 KB)

Change iterations in RO project to same as in Objective-C to compare results.

b.r.

---

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [August 29, 2015, 12:54am UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/4 "2015-08-29T00:54:54Z")

</div>

Okay, here is best one example. Definitelly is something wrong with no mem release. Simple unlimited loop in thread which create class which alloc array of ~1MB RAM, add to NSMutableArray and right after remove and do null to array. ARC should do rest work. For better safety I wrap code with \_\_autoreleasepool. App will work for few sec. and will close. VS will log “Received memory warning”. Simulator will eat \> 3 GB of RAM. Maybe I’m doing something wrong, but memory shouldn’t be auto freed? Same logic works perefectly in xcode and all app take 11MB. Of course tested on real device and result is same - out of memory and crash after few sec.

[Zombies2.zip](https://talk.remobjects.com/uploads/default/original/2X/b/b7211a07a2986866886d3ead85ccb0d49f153a58.zip) (118.9 KB)

b.r.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [August 29, 2015, 12:16pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/5 "2015-08-29T12:16:15Z")

</div>

I think you’re confusing something. Zombies and Leaks are the opposite. In particular, when you run in Zombie mode you cannot judge your apps memory footprint, _because_ Zombie mode keeps all objects around, so by definition memory usage will keep going up and up and up.

**A memory leak** is an object that never gets released, even though nothing uses it anymore. These commonly happen with Retain Cycles. See [here](http://docs.elementscompiler.com/Platforms/Cocoa/RetainCycles). A leak will manifest in memory usage going up and up. Typically, there fix will be to use weak references to break the retain cycle.

**A Zombie** is an object that _was_ released, bu someone _is_n still holding on to a pointer, and trying to use it after its being released. This usually manifests in an obscure crash (unless running in ZombieEnabled mode, where it will give clean message _because_ the released objects gets kept around.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [August 29, 2015, 12:23pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/6 "2015-08-29T12:23:59Z")

</div>

That said, your project looks fine, that code should definitely not leak, (probably not even without the Auto-release Pools that are in place). We’ll need to debug this, this _could_ be a bug in ARC.

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [August 29, 2015, 12:24pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/7 "2015-08-29T12:24:25Z")

</div>

Thanks, logged as bugs://72900

---

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [August 29, 2015, 12:55pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/8 "2015-08-29T12:55:49Z")

</div>

Project are named zombies because I test it for zombies first 🙂 Don’t take project name as part of issue 🙂 I mix 2 things here to not open 2 cases. I test it (and did screenshot) for memory leaks in instruments. I start from zombies, because I know that something is wrong and I try to find why my apps crash sometimes. I judge that I try to access to released object but true was opposite (out of memory).  
Anyway looks like ARC is not working here. I did other test with only alloc byte array and is same problem. I didn’t find any workaround. Please check it with high priority (if possible), because all apps are affected and depend how much object allocations app do, it will work 1min, 5, 10 or few sec.

I hope You will find solution 🙂 I believe in Your power :)🙂

b.r.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [August 29, 2015, 2:04pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/9 "2015-08-29T14:04:09Z")

</div>

Weird. all the memory goes not into ObjC objects at all, but into 1MB chunks created by.

# Address Category Timestamp Live Size Responsible Library Responsible Caller

0 0x133a16000 Malloc 1.00 MB 00:02.151.466 • 1.00 MB Zombies array\_create

definitely sounds like a compiler issue.

---

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [August 30, 2015, 1:29am UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/10 "2015-08-30T01:29:52Z")

</div>

sounds bad. Fixable?

When byte table is declared in class, then it leak. When created without class, then is destroyed properly. Similar problem is with NSMutableArray.  
Class added to an array will not report mem leak after call removeAllObjects or when is out of scope, but array do not release memory, so it grown and grown till out of memory and crash, but ONLY in case, when string is assigned to class variable which in that class.

```
private class tt
{
	public string str;
}

// repeat it in endless loop
NSMutableArray ar = new NSMutableArray();
for (int b = 0; b <= 1000; b++)
{
	tt tta = new tt();
	tta.str = "some text"; // >>>>> if string is contant, then no memory problem
        tta.str = b.stringValue; // >>>>> if string is variable, then memory is not released
	ar.addObject(tta);
}

```

b.r.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [August 30, 2015, 12:09pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/11 "2015-08-30T12:09:35Z")

</div>

Of course it’s fixable ;). But this is outside of my area of expertise. The compiler guys will need to look at it.

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [September 1, 2015, 6:51am UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/12 "2015-09-01T06:51:04Z")

</div>

Two bugs have been fixed now as part of 72900:

- .intValue used to leak when called directly on an integer.
- [] array doesn’t cleanup on class destruction.

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [September 1, 2015, 6:51am UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/13 "2015-09-01T06:51:21Z")

</div>

bugs://72900 got closed with status fixed.

---

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [September 1, 2015, 1:48pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/14 "2015-09-01T13:48:57Z")

</div>

Great news !!! 🙂 Now left class with string problem (hope is only string).

b.r.

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [September 1, 2015, 2:11pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/15 "2015-09-01T14:11:03Z")

</div>

which one is that? I dont’ see any with a string in a class?

---

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [September 1, 2015, 6:07pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/16 "2015-09-01T18:07:12Z")

</div>

this one. Class with string variable is not released. No matter if added to NSMutableArray or just instance created. But str value MUST be unique for every new instance to reproduce it. With same string all is working properly. Check comments added to `tta.str`

```
private class tt
{
	public string str;
}

// repeat it in endless loop
NSMutableArray ar = new NSMutableArray();
for (int b = 0; b <= 1000; b++)
{
	tt tta = new tt();
	tta.str = "some text"; // >>>>> if string is contant, then no memory problem
        tta.str = b.stringValue; // >>>>> if string is variable, then memory is not released
	ar.addObject(tta);
}
```

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [September 1, 2015, 6:09pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/17 "2015-09-01T18:09:39Z")

</div>

Ah. It’s not really about the string in this case, the boxing call from b to NSNumber(b) wasn’t released. that I already fixed.

---

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [September 1, 2015, 6:12pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/18 "2015-09-01T18:12:30Z")

</div>

Aha, so all is fixed then from this post I assume? 🙂  
Perfect ‼  
b.r.

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [September 1, 2015, 7:08pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/19 "2015-09-01T19:08:03Z")

</div>

I think so.

---

<div class="post-metadata">

### Author: ![Benito\_T](https://talk.remobjects.com/user_avatar/talk.remobjects.com/benito_t/32/1600_2.png) [@Benito\_T](https://talk.remobjects.com/u/Benito_T)
#### Post date: [September 4, 2015, 2:15pm UTC](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760/20 "2015-09-04T14:15:07Z")

</div>

It’s not fixed at all. When \_\_autoreleasepool is used then working properly. Without \_\_autoreleasepool, memory is not released. As sample use same code, just don’t use \_\_autoreleasepool

[EDIT]

Small claryfication what is fixed and what is not:

**Fixed:**

byte array is freed with or without \_\_autoreleasepool

**Not fixed:**

Class with string method where string is unique for every class instance. It’s only freed, when used in with \_\_autoreleasepool

b.r.

[Next page](https://talk.remobjects.com/t/xcode-zombies-huge-memory-leaks/6760.md?page=2)
