Invalid pointer dequeued from free proxy listlist 内存错误怎么解决

& 升级后,常常遇到的遇到的警告、错误,解决方法
从sdk3.2.5升级到sdk 5.1中间废弃了很多的方法,还有一些逻辑关系更加严谨了。
1,警告:“xoxoxoxo”
is deprecated
解决办法:查看xoxoxoxo的这个方法的文档,替换掉这个方法即可。
2,警告:Declaration of “struct sockaddr” will not be visible outside of this function
解决办法:在你的开源.m文件中添加 #import &netinet/in.h&
3,警告:Implicit conversion from enumeration type ‘UIInterfaceOrientation’ to different enumeration type ‘UIDeviceOrientation’
解决办法:类型不匹配。跳到出错的那一行,UIInterfaceOrientation强制转换为UIDeviceOrientation就行了。
4,警告:incompatible pointer types assigning to ‘MyArrayList*’from ‘NSMutableArray’
解决办法:加入强制转换(MyArrayList*)
5,警告:‘&&’ within ‘||’
问题出处:
if (exists && !isDirectory || !exists)………
解决办法: if ((exists && !isDirectory) || !exists)………
6,警告:Warning:The Copy Bundle Resources build phase contains this target’s Info.plist file
解决办法:将Info.plist文件移到Resources目录下,而不要直接放在target下。
7,警告:在使用ASIHttp…第三方库的,运行报错。
解决办法:看你的项目中是否添加CFNetwork.framework、SystemConfiguration.framework, MobileCoreServices.framework,
CoreGraphics.framework和libz.1.2.3.dylib,如果是sdk5.0以上,改添加libz.1.2.5.dylib
8,警告:xxxooo,missing required architecture i386 in file
解决办法:如果是错误信息的话:Target-&Build Settings-&Search Paths, 删除FrameworkSearch Paths 里面内容就可以了。
要只是一个警告的话,真机调试可以过。具体解决方法待大神出现。
clang: error: no such file or directory: ‘/demo2/控件代码/13/Recorder/Recorder_Prefix.pch’
clang: error: no input files
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/clang failed with exit code 1
解决办法: 在你的主工程文件 target搜素,pch ,找到Prefix Header
把它后面的值,都删除,再运行就解决了。
10,警告:
“ARC forbids synthesizing a property of an Objective-C object with unspecified ownership or storage attribute
解决办法:如果定义了ARC有效,那么必须要有所有者属性的定义;所以代码改成下面这样
@property (nonatomic, strong, readonly) NSString *
11,警告:
io6一下的xib系统均没有自动选择Use Autolayout, Supporting iOS 5 and below with xib of iOS 6
解 决办法:Just un-select “Use Autolayout” in the file inspector of the xib’s view and we are back to the familiar autosizing in size inspector and boom, it supports iOS 5 and below.
12,警告:
Warning: Multiple build commands for output file xxx.png
解决办法:找到项目里xxx.png重复,删除重复的资源。
错误信息:
“_OBJC_CLASS_$
“, referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
解决方法:
查看工程,看是不是没有导入相关的框架。或者工程里添加的有相同”.m”,”.h” 文件
错误信息:
Couldn’t register dy.CKRiLiText with the bootstrap server. Error: unknown error code.
This generally means that another instance of this process was already running or is hung in the debugger.Current language: currently objective-c
解决方法: 可能是电脑内存问题引起,重启电脑即可解决。如果重启解决不了问题,那就是你刚刚改动的代码引起的问题。
13 、 错误信息:
ios 5是调试正常的,ios 6真机调试的时候,出现如下错误:ld: file is universal (3 slices) but does not contain a(n) armv7s slice: /Users/mac4/Desktop/my desktop/My app/MyApp name 20:09:12
/MyApp name/ZBarSDK/libzbar.a for architecture armv7serror: linker command failed with exit code 1 (use -v to see invocation)
解决方法:在Xcode里,点击相应的Target,然后点Build Settings,找到VALID_ARCHS,看里面的是不是arvm7s,如果不是改成arvm7s就可以了。
14 、 错误信息:
error: receiver type ‘ViewController’ for instance message does not declare a method with selector ‘hideSearchBar:’ [4]
ViewController 中没有声明一个方法选择’hideSearchBar:
解决方法:
在ViewController .h 中声明一下这个方法 “ hideSearchBar ”
15、 错误信息:当json从服务端请求时得到的字符串,如果这样写的话,会报错,‘;’ after top level declarator
NSString *ss= @”{“recommend”:”世界末日”,”dogname“:”机器人”}”;
解决方法:
即可。NSString *ss= @”{ \”recommend \”: \”世界末日 \”, \”dogname \”: \”机器人 \”}”;
31,警告:
warning: Semantic Issue: Incompatible integer to pointer conversion assigning to ‘BOOL *’ (aka ‘signed char *’) from ‘BOOL’ (aka ‘signed char’)
解决办法: 检查 BOOL *换为BOOL就可以了,多写一个
Xcode升级到4.4后,出现了一堆的Warning.网上搜了一些办法,总结一下。
32:Jsonkit中的
Direct access to objective-c’s isa is deprecated in favor of object_setClass() and object_getClass()
object-&isa 替换为 object_getClass(object)
keyObject-&isa 替换为 object_getClass(keyObject)
(id)keys[idx]-&isa 替换为 object_getClass((id)keys[idx])
format specifies type ‘unsigned long’ but the argument has type ‘nsuinteger’ (aka ‘unsigned int’)
给变量增加(unsigned long)进行类型转换
33:md5加密(iOS SDK中自带了CommonCrypto
Implicit declaration of function ‘CC_MD5′ is invalid in C99
view plaincopy
#define CC_MD5_DIGEST_LENGTH 16
+(NSString *)MD5HashForString:(NSString *)input {
const char *cStr = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, strlen(cStr), result);
return [NSString stringWithFormat: @&#x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x”,
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]];
引入函数定义的头文件
#import &CommonCrypto/CommonDigest.h&
34:ASIDataDecompressor中的警告
format specifies type
‘short’ but the
argument has type ‘int’
在+ (NSError *)deflateErrorWithCode:(int)code 和 +(NSError *)inflateErrorWithCode:(int)code中
[NSString stringWithFormat:@”Compression of data failed with code %hi”,code] 中
将code改为 (short)code,类型转换
35:Reachability中警告
Using ‘stringWithString:’ with a literal is redundant
statusString = [NSString stringWithString: @”Not Reachable”];
改为:statusString = @”Not Reachable”;
36.format specifies type ‘id’ but the argument has type ‘const char *’
NSCAssert(NO, @”Unhandled error encountered during SAX parse. msg is %@”, msg);
改为:NSCAssert(NO, @”Unhandled error encountered during SAX parse. msg is %@”, [NSString stringWithUTF8String:msg]);
118:65: Using ‘stringWithString:’ with a literal is redundant
改为:self.locationInput.text = @”captured change”;
38 在项目中设置控件的layer属性时,会发生错误,”Property ‘c’ cannot be found in forward class object ‘CALayer *”,
这时需要引入
#import &QuartzCore/CALayer.h&。
Error launching remote program: failed to get the task for process
解决方法:
把真机上的软件,删除,然后,clean 一下,重新运行就可以了。
42,真机调试的时候,出现
这正常,但是不识别机器的。
解决方法:
设置为以上相对应的版本就可以了。
43,真机调试的时候,出现
ios Broken pipe
解决方法:
:推出xcode
:断开机器(iphone,ipad,ipod)链接
:重启iPhone在联接xcode,就可以了。
本文出自 蓝天笔记,转载时请注明出处及相应链接。本文永久链接: /archives/288
Ɣ回顶部A Simple Moving Average Algorithm - CodeProject
Sign in using
466.2K views2.5K downloads74 bookmarked
&4.63 (40 votes)
4.63/5 - 40 votes4 removedμ 4.20, σa 2.20 []
Rate this:
Introduction
I needed a running average to monitor CPU utilization and ended writing a small class for doing the calculation. In writing the code, I realized that even something as simple as a running average algorithm requires some thought to writing it well. So I have written this article for beginners, to illustrate how even simple algorithms need some thought.
What Is A Running Average?
A running average (also called a moving average) can be implemented in different ways. For an in-depth description, refer to .
Simply Moving Average
A simple moving average is the unweighted mean (the sum of all items in a list divided by the number of items in the list) of the previous n data points. This is the easiest running average to implement and is the one I'm illustrating in this article.
Weighted Moving Average
A weighted moving average is an average in which the data points in the list are given different multiplying factors. This has the affect of making some items in the list more important (given more weight) than others. For example, you may wish to have older values to have more weight than newer ones, or vice-versa.
Brute Force Implementation
A simple moving average takes the sample values and divides them by the sample count. A basic implementation might look something like this:
using System.Collections.G
using System.T
namespace AveragerTest
public class BruteForce
protected float[]
protected int
public float Average
float total=0;
for (int i=0; i&samples.L i++)
total+=samples[i];
return total/samples.L
public BruteForce(int numSamples)
if (numSamples &= 0)
throw new ArgumentOutOfRangeException(
"numSamples can't be negative or 0.");
samples = new float[numSamples];
public void AddSample(float val)
samples[idx] =
if (++idx == samples.Length)
In fact, this reminds me a lot of an algorithm I wrote when I was first starting programming in Commodore BASIC! There are several problems with this algorithm:
Until the sample array is completely loaded, the Average calculation includes unloaded (and zero) sample values. This results in the wrong average
The algorithm is very inefficient for large sample sizes, having to total the samples every time the average is taken
The algorithm implements a circular list. Wouldn't it be nice to extract the implementation of the circular list from the implementation of the running average algorithm?
There is no good way to replace the simple running average with, say, a weighted running average, because the class does not implement an interface that promotes use-case flexibility.
A Better Implementation
Separating Out The Collection and Iteration Logic
The first step, in my opinion, would be to separate out the iteration logic. We have to be careful here because the circular list that we need isn't just an iterator--we actually have to set the values in the list as well. An iterator does not allow this. From MSDN:
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException.
Changing the management of the sample list to a
eliminates two problems:
The list management is decoupled from the algorithm
We can use the Count method of the circular list to obtain the count of loaded items, not just the total number of items in the list
In addition, it leverages code re-use and helps debugging--we can isolate testing of the circular list from problems in the running average class. In other words, it would be easy to write unit tests for the circular list, validate its operation, then write unit tests to verify that the averager is working correctly.
The new code looks like this:
using System.Collections.G
using System.T
using Clifton.Collections.G
namespace AveragerTest
public class BruteForce
CircularList&float&
public float Average
float total=0;
foreach(float v in samples)
return total/samples.C
public BruteForce(int numSamples)
if (numSamples &= 0)
throw new ArgumentOutOfRangeException(
"numSamples can't be negative or 0.");
samples = new CircularList&float&(numSamples);
public void AddSample(float val)
samples.Value =
samples.Next();
This is a much cleaner implementation already!
Performance
The next issue is performance. For a large sample size, we don't want to be iterating over the entire sample to get the total. It would make more sense to manage a running total. This requires a little bit of logic when adding a sample value, to determine whether a sample already exists so that it can be subtracted from the total:
public void AddSample(float val)
if (samples.Count == samples.Length)
total -= samples.V
samples.Value =
samples.Next();
and the Average getter no longer needs to sum up all the samples:
public float Average
get { return total/samples.C }
But Is It Really Better Performance?
That's always the question, and it depends on usage! Arguably, the first method might be better performing if ratio of samples being added to the number of times the average is taken is very high. For example, you might be adding a million samples between taking average, and the sample size might only be a hundred items. In this scenario, the overhead of keeping the total current probably far outweighs the overhead of calculating the total on the fly.
So, the right answer regarding performance is to select the algorithm that works well for your requirements. The algorithm I describe above is what I consider to be a typical case, where the average is taken after every new sample, and in this case, it does make sense to use a running total, especially if the sample size is large.
Improving Usability
Usability can be improved by providing an interface that the programmer can use rather than the class directly. By using an interface, the programmer can create the correct moving average algorithm without the code needing to know which (simple or weighted) was actually constructed. Some other part of the application can make this decision.
public interface IMovingAverage
float Average { get;}
void AddSample(float val);
void ClearSamples();
void InitializeSamples(float val);
Comments, Error Checking, And Bells And Whistles
The final implementation, with some additional error checking and the ability to clear the average or pre-load the samples with a particular value, looks like this:
using Clifton.Collections.G
namespace Clifton.Tools.Data
public class SimpleMovingAverage : IMovingAverage
CircularList&float&
protected float
public float Average
if (samples.Count == 0)
throw new ApplicationException("Number of samples is 0.");
return total / samples.C
public SimpleMovingAverage(int numSamples)
if (numSamples &= 0)
throw new ArgumentOutOfRangeException(
"numSamples can't be negative or 0.");
samples = new CircularList&float&(numSamples);
total = 0;
public void AddSample(float val)
if (samples.Count == samples.Length)
total -= samples.V
samples.Value =
samples.Next();
public void ClearSamples()
total = 0;
samples.Clear();
public void InitializeSamples(float v)
samples.SetAll(v);
total = v * samples.L
A simple program demonstrating the usage of the SimpleMovingAverage class is:
using Clifton.Collections.G
using Clifton.Tools.D
namespace AveragerTest
class Program
static void Main(string[] args)
IMovingAverage avg = new SimpleMovingAverage(10);
Test(avg);
static void Test(IMovingAverage avg)
for (int i = 0; i & 20; i++)
avg.AddSample(i);
Console.WriteLine(avg.Average);
Note how the sample size is 10 items but there are 20 samples added to the moving average. The old samples are replaced as new samples are added. As the code illustrates, a SimpleMovingAverage instance is constructed and used to initialize the variable avg, whose type is IMovingAverage. The Test method takes the interface for a parameter. If we implemented, say, a WeightedMovingAverage class, we could change the construction to:
IMovingAverage avg = new WeightedMovingAverage(10);
and call the exact same Test routine to see how the new moving average works. By using the interface, we promote the usability of the code.
All This Work!
Admittedly, this is a lot of work for a simple algorithm. Personally, I try to follow the principle of doing the job right, even if it's a small job, because small jobs quickly turn into large jobs. The old excuse "we don't have the time and the budget", or "it's simple, just bang it out and we'll refactor it later" has proven time and time again to be lies. It does not save time and money to do it wrong first! Refactoring is rarely put into the "new" budget. Bad practices continue on unless they are changed from the beginning. The only reason to hack out an algorithm is for a prototype, so you can learn from it and develop the production algorithm correctly.
This article, along with any associated source code and files, is licensed under
United States
Marc is the creator of two open source projects, , a declarative (XML) instantiation engine and , and , a commercial n-tier RAD application suite.
Visit his website, , where you will find many of his articles and his
Marc lives in Philmont, NY.
Comments and Discussions
General &&
Suggestion &&
Question &&
Last Updated 4 Mar 2007
Article Copyright 2007 by Marc CliftonEverything else
Copyright & ,C free(): invalid pointer - Stack Overflow
to customize your list.
Announcing Stack Overflow Documentation
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
I am teaching myself C. My goal is to make a C function that just walks a query string and splits on the ampersand and the equals sign. I am getting stuck on this error from Valgrind.
==5411== Invalid free() / delete / delete[] / realloc()
at 0x402AC38: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
by 0x804857C: main (leak.c:28)
Address 0x420a02a is 2 bytes inside a block of size 8 free'd
at 0x402AC38: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
by 0x804857C: main (leak.c:28)
==5411== HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 1 allocs, 2 frees, 8 bytes allocated
==5411== All heap blocks were freed -- no leaks are possible
==5411== For counts of detected and suppressed errors, rerun with: -v
==5411== ERROR SUMMARY: 20 errors from 9 contexts (suppressed: 0 from 0)
and the backtrace :
*** Error in `./leak': free(): invalid pointer: 0x08c1d00a ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767c2)[0xb75f17c2]
/lib/i386-linux-gnu/libc.so.6(+0x7f2510]
./leak[0x804857d]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb7594905]
./leak[0x8048421]
======= Memory map: ========
49000 r-xp :05 262764
/home/danny/dev/c-qs-parser/leak
4a000 r--p :05 262764
/home/danny/dev/c-qs-parser/leak
4b000 rw-p :05 262764
/home/danny/dev/c-qs-parser/leak
08c1d000-08c3e000 rw-p :00 0
b757a000-b757b000 rw-p :00 0
b757b000-b7729000 r-xp :05 1312132
/lib/i386-linux-gnu/libc-2.17.so
b2b000 r--p 001ae000 08:05 1312132
/lib/i386-linux-gnu/libc-2.17.so
b772b000-b772c000 rw-p 001b 1312132
/lib/i386-linux-gnu/libc-2.17.so
b772c000-b772f000 rw-p :00 0
b772f000-b774a000 r-xp :05 1312589
/lib/i386-linux-gnu/libgcc_s.so.1
b774a000-b774b000 r--p :05 1312589
/lib/i386-linux-gnu/libgcc_s.so.1
b774b000-b774c000 rw-p :05 1312589
/lib/i386-linux-gnu/libgcc_s.so.1
b774c000-b7750000 rw-p :00 0
b51000 r-xp :00 0
b71000 r-xp :05 1312116
/lib/i386-linux-gnu/ld-2.17.so
b72000 r--p :05 1312116
/lib/i386-linux-gnu/ld-2.17.so
b73000 rw-p :05 1312116
/lib/i386-linux-gnu/ld-2.17.so
bfe93000-bfeb4000 rw-p :00 0
Aborted (core dumped)
finally here is the code:
#include &stdio.h&
#include &string.h&
#include &stdlib.h&
int main() {
//char p[] = "t=quote&k=id&v=10";
char p[] = "t=quote";
s = strdup(p);
if (s != NULL) {
while ((token = strsep(&s, "&")) != NULL) {
found = 0;
printf("TOKEN: %s\n\n", token);
while ((tk = strsep(&token, "=")) != NULL) {
printf("TK: %s\n\n", tk);
free(token);
37.7k676137
You're attempting to free something that isn't a pointer to a "freeable" memory address. Just because something is an address doesn't mean that you need to or should free it.
There are two main types of memory you seem to be confusing - stack memory and heap memory.
Stack memory lives in the live span of the function. It's temporary space for things that shouldn't grow too big. When you call the function main, it sets aside some memory for your variables you've declared (p,token, and so on).
Heap memory lives from when you malloc it to when you free it. You can use much more heap memory than you can stack memory. You also need to keep track of it - it's not easy like stack memory!
You have a few errors:
You're trying to free memory that's not heap memory. Don't do that.
You're trying to free the inside of a block of memory. When you have in fact allocated a block of memory, you can only free it from the pointer returned by malloc. That is to say, only from the beginning of the block. You can't free a portion of the block from the inside.
For your bit of code here, you probably want to find a way to copy relevant portion of memory to somewhere else...say another block of memory you've set aside. Or you can modify the original string if you want (hint: char value 0 is the null terminator and tells functions like printf to stop reading the string).
EDIT: The malloc function does allocate heap memory*.
"9.9.1 The malloc and free Functions
The C standard library provides an explicit allocator known as the malloc package. Programs allocate blocks from the heap by calling the malloc function."
~Computer Systems : A Programmer's Perspective, 2nd Edition, Bryant & O'Hallaron, 2011
EDIT 2: * The C standard does not, in fact, specify anything about the heap or the stack. However, for anyone learning on a relevant desktop/laptop machine, the distinction is probably unnecessary and confusing if anything, especially if you're learning about how your program is stored and executed. When you find yourself working on something like an AVR microcontroller as H2CO3 has, it is definitely worthwhile to note all the differences, which from my own experience with embedded systems, extend well past memory allocation.
2,86011643
From where did you get the idea that you need to free(token) and free(tk)? You don't. strsep() doesn't allocate memory, it only returns pointers inside the original string. Of course, those are not pointers allocated by malloc() (or similar), so free()ing them is undefined behavior. You only need to free(s) when you are done with the entire string.
Also note that you don't need dynamic memory allocation at all in your example. You can avoid strdup() and free() altogether by simply writing char *s =.
user529758
You can't call free on the pointers returned from strsep. Those are not individually allocated strings, but just pointers into the string s
that you've already allocated. When you're done with s altogether, you should free it, but you do not have to do that with the return values of strsep.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled}

我要回帖

更多关于 invalid party list 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信