<?xml version="1.0" encoding="UTF-8"?>
	<rss version="2.0"
		xmlns:content="http://purl.org/rss/1.0/modules/content/"
		xmlns:wfw="http://wellformedweb.org/CommentAPI/"
		xmlns:dc="http://purl.org/dc/elements/1.1/"
		xmlns:atom="http://www.w3.org/2005/Atom"

			>

	<channel>
		<title>Cycling 74  &#187;  Topic: defer() and memory allocation / sysmem</title>
		<atom:link href="http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/feed" rel="self" type="application/rss+xml" />
		<link>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/feed</link>
		<description></description>
		<pubDate>Tue, 18 Jun 2013 14:13:01 +0000</pubDate>
		<generator>http://bbpress.org/?v=2.2.4</generator>
		<language></language>

		
														
					
				<item>
					<guid>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-31169</guid>
					<title><![CDATA[defer() and memory allocation / sysmem]]></title>
					<link>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-31169</link>
					<pubDate>Tue, 03 Apr 2007 16:38:16 +0000</pubDate>
					<dc:creator>Olaf Matthes</dc:creator>

					<description>
						<![CDATA[
						<p>Hi everybody,</p>
<p>the WritingExternals PDF states that when using defer() and defer_low <br />
() the following applies:</p>
<p>&#8220;argv &#8211; Array containing a variable number of function<br />
arguments. If this argument is non-zero, defer<br />
allocates memory to make a copy of the arguments<br />
(according to the size passed in argc) and passes the<br />
copied array to the function fun when it executes as<br />
the fourth argument.&#8221;</p>
<p>Since I&#8217;m getting some strange crashes on OS X 10.4.8 with Max 4.6.2  <br />
I was wondering whether I have to manually free this memory in the  <br />
function that gets called by defer()? And if so, which one of the  <br />
memory freeing routines (freebytes() or sysmem_freeptr()) to use?</p>
<p>And another question related to the sysmem API: I&#8217;m currently working  <br />
on a rather simple external (simple in terms of memory allocations)  <br />
and changed it from getbyte()s function to sysmem_newptr() (of  <br />
course, I changed all the memory allocation / freeing / resizing  <br />
functions accordingly). Surprisingly I get now Max crashes with the  <br />
very same code. There is no way to say when it crashes, but it will  <br />
crash sooner or later when I send my external a message that calles  <br />
one of the sysmem functions. The crashlog usually says something like  <br />
this:</p>
<p>Thread 0 Crashed:<br />
0   libSystem.B.dylib              	0x90002aa6 szone_malloc + 308<br />
1   libSystem.B.dylib              	0x9000268f malloc + 597<br />
2   &#8230;ple.CoreServices.CarbonCore 	0x90c9c23a CSMemNewPtr + 39<br />
3   &#8230;ple.CoreServices.CarbonCore 	0x90ca0458 NewPtr + 29<br />
4   com.cycling74.MaxMSP46         	0x00061a43 sysmem_resizeptr + 17  <br />
(sysmem.c:63)<br />
5   com.cycling74.MaxAPI           	0x0180c31e sysmem_resizeptr + 37<br />
6   com.cycling74.at.oracle        	0x159c82e4  <br />
ftmoracle_oracle_add_trans + 117<br />
7   com.cycling74.at.oracle        	0x159c87a1 ftmoracle_int + 234<br />
8   com.cycling74.MaxMSP46         	0x000e6de3 outlet_int + 361  <br />
(inletoutlet.c:753)</p>
<p>What do I learn from this report? It&#8217;s crashing in libSystem when  <br />
allocating (resizing) memory???<br />
This time there are no threads involved in my external&#8230;</p>
<p>Olaf</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-100920</guid>
					<title><![CDATA[Re: defer() and memory allocation / sysmem]]></title>
					<link>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-100920</link>
					<pubDate>Tue, 03 Apr 2007 18:28:14 +0000</pubDate>
					<dc:creator>Jeremy Bernstein</dc:creator>

					<description>
						<![CDATA[
						<p>No, you don&#8217;t need to do anything, or know anything, about this  <br />
memory. It belongs to the Max kernel.</p>
<p>As for your crash, yes, it&#8217;s happening during a memory reallocation.  <br />
How much memory are you trying to allocate? Could you maybe share  <br />
some code so that we can see what you are working with?</p>
<p>jb</p>
<p>Am 03.04.2007 um 18:38 schrieb Olaf Matthes:</p>
<p>> Since I&#8217;m getting some strange crashes on OS X 10.4.8 with Max  <br />
> 4.6.2 I was wondering whether I have to manually free this memory  <br />
> in the function that gets called by defer()? And if so, which one  <br />
> of the memory freeing routines (freebytes() or sysmem_freeptr()) to  <br />
> use?</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-100921</guid>
					<title><![CDATA[Re: defer() and memory allocation / sysmem]]></title>
					<link>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-100921</link>
					<pubDate>Fri, 06 Apr 2007 17:02:05 +0000</pubDate>
					<dc:creator>Olaf Matthes</dc:creator>

					<description>
						<![CDATA[
						<p>Jeremy Bernstein wrote:<br />
> No, you don&#8217;t need to do anything, or know anything, about this memory. <br />
> It belongs to the Max kernel.<br />
> <br />
> As for your crash, yes, it&#8217;s happening during a memory reallocation. How <br />
> much memory are you trying to allocate? Could you maybe share some code <br />
> so that we can see what you are working with?</p>
<p>Hi Jeremy,</p>
<p>thanks for the input regarding defer_low(). It turned our my <br />
sysmem_resizebytes() problem was related to that: in the allocated <br />
memory I tried to resize I was storing pointers to symbols that were <br />
passed trough a defer()ed function (as list of t_atoms of type A_SYM) <br />
and thus probably &#8216;disappeared&#8217; while I was still trying to access them. <br />
After allocating memory for them at the new location and storing a copy <br />
instead of a pointer everything works fine now.</p>
<p>One thing I realised after figuring that out is that some code that <br />
Joshua once send me for thread-safe printing is also wrong. Here&#8217;s what <br />
he suggested to use:</p>
<p>static void post_dostring(t_myobject *x, char *p)<br />
{<br />
     post(&#8220;%s: %s&#8221;, ob_sym(x)->s_name, p);<br />
     sysmem_freeptr(p);<br />
}</p>
<p>static void threadsafe_post(t_myobject *x, char *p)<br />
{<br />
	char *q;<br />
	if((q = sysmem_newptr(strlen(p))))<br />
	{<br />
		q = p;<br />
		defer(x, (method)post_dostring, (t_symbol *)q, 0, 0L);<br />
	}<br />
}</p>
<p>But at closer look the second function should look like this:</p>
<p>static void threadsafe_post(t_myobject *x, char *p)<br />
{<br />
	char *q;<br />
	if((q = sysmem_newptr(strlen(p) + 1)))<br />
	{<br />
		strcpy(q, p);<br />
		defer(x, (method)post_dostring, (t_symbol *)q, 0, 0L);<br />
	}<br />
}</p>
<p>First we have to alloc strlen(p) + 1 and not just strlen(p) bytes <br />
because of the null termination of the incoming string. Second issue is <br />
to make a copy of the string p and not just have q point to it.<br />
This is not really conncted to defer_low() but falls into the same <br />
category of assuming that pointing to something is safe. Usually it&#8217;s not.</p>
<p>Olaf</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-100922</guid>
					<title><![CDATA[Re: defer() and memory allocation / sysmem]]></title>
					<link>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-100922</link>
					<pubDate>Fri, 06 Apr 2007 18:35:57 +0000</pubDate>
					<dc:creator>Joshua Kit Clayton</dc:creator>

					<description>
						<![CDATA[
						<p>
FWIW, here&#8217;s the message I tried to send you, but it didn&#8217;t seem to  <br />
go through given the server issues.</p>
<p>On Apr 3, 2007, at 9:38 AM, Olaf Matthes wrote:</p>
<p>> Since I&#8217;m getting some strange crashes on OS X 10.4.8 with Max  <br />
> 4.6.2 I was wondering whether I have to manually free this memory  <br />
> in the function that gets called by defer()? And if so, which one  <br />
> of the memory freeing routines (freebytes() or sysmem_freeptr()) to  <br />
> use?<br />
></p>
<p>Defer frees the memory after calling into your function. Therefore  <br />
you need to copy this data again to your own memory if you want to  <br />
retain reference to this information. Also, you cannot resize this  <br />
memory.</p>
<p>> And another question related to the sysmem API: I&#8217;m currently  <br />
> working on a rather simple external (simple in terms of memory  <br />
> allocations) and changed it from getbyte()s function to  <br />
> sysmem_newptr() (of course, I changed all the memory allocation /  <br />
> freeing / resizing functions accordingly). Surprisingly I get now  <br />
> Max crashes with the very same code. There is no way to say when it  <br />
> crashes, but it will crash sooner or later when I send my external  <br />
> a message that calles one of the sysmem functions. The crashlog  <br />
> usually says something like this:<br />
><br />
> Thread 0 Crashed:<br />
> 0   libSystem.B.dylib              	0x90002aa6 szone_malloc + 308<br />
></p>
<p>If you&#8217;re passing in valid sysmem (and not getbytes or some other)  <br />
pointers, this just means memory has been corrupted. Probably happens  <br />
shortly before your memory reallocation. These sorts of bugs can be  <br />
tricky to track down. You&#8217;ll want to look at all your memory  <br />
allocation and accessing code (make sure you&#8217;re not assuming  <br />
uninitialized pointers will be NULL). If you&#8217;re trying to access the  <br />
memory passed in from defer beyond the extents of your callback, you  <br />
would see these types of crashes, but it could be something else.</p>
<p>-Joshua</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-100923</guid>
					<title><![CDATA[Re: defer() and memory allocation / sysmem]]></title>
					<link>http://cycling74.com/forums/topic/defer-and-memory-allocation-sysmem/#post-100923</link>
					<pubDate>Fri, 06 Apr 2007 18:39:00 +0000</pubDate>
					<dc:creator>Joshua Kit Clayton</dc:creator>

					<description>
						<![CDATA[
						<p>
On Apr 6, 2007, at 10:02 AM, Olaf Matthes wrote:</p>
<p>><br />
> One thing I realised after figuring that out is that some code that  <br />
> Joshua once send me for thread-safe printing is also wrong.<br />
> First we have to alloc strlen(p) + 1 and not just strlen(p) bytes  <br />
> because of the null termination of the incoming string. Second  <br />
> issue is to make a copy of the string p and not just have q point  <br />
> to it.</p>
<p>You are totally correct. Sorry or the ill guided quick email client  <br />
coding.</p>
<p>-Joshua</p>
						]]>
					</description>

					
					
				</item>

					
		
	</channel>
	</rss>

