New, delete, Objective-C ++ and leaks

I am adding functionality to an existing Cocoa application, written primarily in Objective-C. I have to use the existing C ++ class in the class I am writing, so the new Objective-C ++ class. Also, I had to write a method in a C ++ class that temporarily uses a buffer. So my C ++ method is similar to

(void) myMethod{ int8_t* bffr = new int8_t[length]; // use the buffer delete [] bffr; } 

I am instantiating a C ++ class in a .mm file and trying to continue. The problem is that the application crashes. However, if I comment on delete , the application does not crash, but the tools report a leak associated with this method. I assume Objective-C memory management is getting bolluxed up. How to enable this catch-22?

By the way, I get the same result using malloc and get_temporary_buffer.

+4
source share
2 answers

You must free the allocated memory and do it at the right time - only after no other code tries to access this part of the memory.

+1
source

Have you tried to enable memory debugging features in Xcode to track the source of the failure?

(Edit Scheme> Debug> Memory Mgmt, check all fields)

Cheers, Jay

0
source

Source: https://habr.com/ru/post/1385547/


All Articles