Invalid pointer dequeuedpop from empty listfree list 内存错误怎么解决

malloc: Invalid pointer dequeued from free list
Related questions
malloc: Invalid pointer dequeued from free list
imported from
I have some C++ code in my OS X project that allocates an array thusly:
C * p = new C[lengthHint + 2];
This is C is unsigned short. lengthHint is 1. This is all irrelevant. The error I get at runtime is:
malloc: *** error for object 0xf0: Invalid pointer dequeued from free list
*** set a breakpoint in malloc_error_break to debug
It appears malloc is failing because a previous call to free freed something that wasn't valid. But it seems like free would've complained about that at the time.
Obviously there are millions of malloc/free and new/delete calls being executed and this same code is running without issues in other programs running on iOS and OS X. I'm not sure how to approach debugging this and am looking for suggestions.
As I suspected, the problem didn't had anything to do with the malloc call. I had decided to ignore the problem while I worked on another issue. The project was one where I was moving some code previously written in C++ for Windows over to Mac. While changing some type names I inadvertently changed this:
TCHAR * p = new TCHAR[(length + 1)];
char * p = new char(length + 1);
So just a typo, but one with pretty significant implications.
I discovered this while reviewing recent changes to a file that had some other odd behavior. So the answer to my original question was pretty simple and applies in a lot of other situations: "What have you changed lately?" :-)
See also questions close to this topic
I would like to ensure that my C++ application is dynamically loading only DLLs which I have specifically defined in a whitelist. All other DLLs not on the whitelist, should be prevented to load.
Of particular concern are third-party DLLs loaded dynamically from system DLLs not under my control. For instance, when opening a FileOpen dialog for the first time, I see a large number of shell extensions (e.g. tortoise_svn.dll, DropboxExt64.18.0.dll, gitdll.dll) being loaded into my process.
Safety - To ensure the stability of my software, I want to ensure that only DLLs get loaded that have been tested to be compatible and secure.
Performance - Unnecessary DLLs are causing serious slow-downs and memory bloat. For example, the first time opening a FileOpen dialog takes 3-5 seconds during Debug builds just to load all shell extensions (mostly because they also follow-up with their own msvcp90.dll, etc., etc.).
I am aware that what I am asking has many downsides:
Robustness - A system update might update DLLs which require new dependencies, causing the application not to start. As I have to clear my application for any system update anyway, this is fine. In act, I am already using a DLL version check to prevent the application from starting if known system libraries are not matching a whitelist of cleared versions.
User Experience - Users install third party applications such as keyboard hooks and shell extensions for a reason, so there is a point not to mess with these. Ideally, the solution to my question should allow for configuration to add the user's DLLs to the whitelist.
What I have been exploring:
I have tried to figure out, whether manifests could be used to restrict what is loaded by my executable. It seems that I could create a set of manifests for offending DLLs and redirect them to empty shim DLLs I have created. It is unclear to me, whether a manifest could define a catch-all assembly to redirect any loadlibrary calls.
I saw suggestions to use hooking / detours to redirect loadlibrary(ex) and others to intercept any loading of libraries, but this crashes under 64-bit, because I believe other DLLs are messing with loadlibrary as well. Has anybody gotten this to work and can point to working code?
I have read up on Application Isolation, but am unsure whether that means anything with regards to my question.
Since many of the unwanted DLLs are loaded by reading the DLL names/paths from registry, I am wondering if I could blacklist access to certain registry subtrees (ShellExtensions being a terrible offender of bloat)?
Some links of other people having the same issue, in particular with WinAPI FileOpen dialogs causing unwanted DLLs to load due to ShellExtensions being loaded by the explorer process.
So I have a function main.cpp() that calls
Update(x1,y1)
.This Update has 2 arguments x1(scalar double) and y1(scalar double). main.cpp continuously reads incoming values from a datafile and after some computations, passes them to Update.Update does several computations and finally calculates a scalar metric value_ . I want to write the metric value_ in a txt file on a newline every time Update calculates that metric value_ inside Update function
So for example if Update calculates 0.45 as value_ for 1st iteration,I want 0.45 on first line of my txt file.If second value_ is 0.1, 0.1 should be on next line of my txt file.
Say my txt is "example.txt",here is what i should get,
but for some reason I am getting this-
0.450.10.55
Here is what I have so far-
void Update (x1,y1){
/* Bunch of calculations to find update my x1 and y1
//calculate value
double value_ = x1_mod * y1_
//store value measurements
value_vector_.push_back(vector_);
ofstream myF
myFile.open("C:\\Users\\Nexus\\Documents\\example.txt",ios::app);
myFile && value_ && '\n';
myFile.close();
I am on windows and want to write each value_ to txt file on a new line.Any suggestions?
I have a basic server written in c++ using winsock (code below). I am able to connect to it using a client written in python on the same machine but when i try to connect to it from a remote machine it just doesn't do anything. i know my remote client is working because it is able to connect to a python server on the same machine as the c++ server.
#pragma comment(lib, "Ws2_32.lib")
//STD console header files
#include &sdkddkver.h&
#include &conio.h&
#include &stdio.h&
//SOCKET header files
#include &WinSock2.h&
#include &Windows.h&
#include &iostream&
#define SCKVERSION2 0x0202
void main()
//main locals
long SUCCESSFUL;
WSAData WinSockD
WORD DLLVERSION;
//WORDS = objects of a data size that a processor naturally handles (such as 16 or 32-bit)
DLLVERSION = MAKEWORD(2, 1); //macro to create WORD value by concatenating its arguments
//Start Winsock DLL
SUCCESSFUL = WSAStartup(DLLVERSION, &WinSockData);
//Create Socket Structure
SOCKADDR_IN ADDRESS;
int AddressSize = sizeof(ADDRESS);
//Create Sockets
SOCKET sock_LISTEN; //listen for incoming connection
SOCKET sock_CONNECTION; //active if connection found
//socket Arguments: AF_INET = internet domain,
//SOCK_STREAM = connection oriented with TCP
sock_CONNECTION = socket(AF_INET, SOCK_STREAM, NULL);
ADDRESS.sin_addr.s_addr = htons(INADDR_ANY);
ADDRESS.sin_family = AF_INET;
ADDRESS.sin_port = htons(2002); //port
sock_LISTEN = socket(AF_INET, SOCK_STREAM, NULL);
bind(sock_LISTEN, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS));
listen(sock_LISTEN, SOMAXCONN); //listen without limits
//If connection found:
cout && "\n\tSERVER: WAITING for incoming connection...";
if (sock_CONNECTION = accept(sock_LISTEN, (SOCKADDR*)&ADDRESS, &AddressSize))
cout && "\n\tA connection found!" &&
SUCCESSFUL = send(sock_CONNECTION, "Welcome! You have connected to Banana SERVER!", 46, NULL);
any help would be appreciated
if it is helpful i am using visual studio and write and compile the c++ code
I am trying to install react-native-maps with Google Maps support.
I am following the guide at
I have installed GoogleMaps with Pod and added the lines in AppDelegate.m (without any errors).
I am also dragging the two folders AirGoogleMaps and AirMaps into my project.
However, every time I run my app, I receive the error AirGoogleMaps dir must be added to your xCode project to support GoogleMaps on iOS.
I don't understand why React Native doesn't recognise the folders when I add them to the project.
I am currently working on a project, where i have created some pins to the map from some locations i my database.
In the app, i want the user to have the option for clicking on an annotation, and see how far the user are from the annotation.
Are there any way to get the clicked annotations coordinate in swift?
I've been looking for a long time around, i found some hints but i cannot figure out how to apply this to my problem. Would appreciate any help.
What trying to achieve is pretty straight forward :
My app includes a WKWebview playing a youtube video (with some
overlaying text). I added a link to take a screenshot of the screen
and to save it to the camera roll.
It works perfectly in Simulator but on device I can only see as a result overlaying text and background (supposed to be the video) is black.
Here is my current code (SWIFT 3):
// Save screenshot
UIImageWriteToSavedPhotosAlbum(captureScreen(), nil, nil, nil)
func captureScreen() -& UIImage {
UIGraphicsBeginImageContextWithOptions(webView!.bounds.size, webView!.isOpaque, 0)
webView!.drawHierarchy(in: webView!.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
I also tried :
UIImageWriteToSavedPhotosAlbum(captureScreen(), nil, nil, nil)
func captureScreen() -& UIImage {
let screenShot = UIApplication.shared.screenShot!
return screenShot
I've read online that we can't use renderInContext: because the GL layer containing the video player is not captured. So they suggest to actually merge content of UIImageView and EAGLview (, ).
This sounds great but most of solutions i found are written in objective-c or relatively complex ().
I need help to find a solution in SWIFT
Thank you in advance for your time.
Here's my code:
#include &stdlib.h& //malloc
#define lineSize 16
#define columnSize 16
#define cellSize 16
int main()
unsigned char***
tab = malloc(sizeof(unsigned char**) * lineSize);
for(unsigned int i = 0; i & lineS i++)
tab[i] = malloc(sizeof(unsigned char*) * columnSize);
for(unsigned int i = 0; i & lineS i++)
for(unsigned int j = 0; j & columnS j++)
tab[i][j] = malloc(sizeof(unsigned char) * cellSize);
unsigned int line = 0;
unsigned int column = 0;
unsigned int cell = 0;
unsigned char* ptr = &tab[line][column][cell];
for(line = 0; line & lineS line++)
for(column = 0; column & columnS column++)
for(cell = 0; cell & cellS cell++)
*ptr = ...;
This code fills tab with values that are only known at execution time
In this case there's no much problems because lineSize, columnSize and cellSize are small, The problem is that when cellSize become 100000+ then dereferencing the pointer gets expensive in terms of time,
that's why I thought to use a pointer to avoid dereferencing.
The problem is that I don't know how to do in order for the pointer to be updated as line, column or cell change.
Appreciates your help, Thanks.
EDIT: More explanations:
The bigger lineSize, columnSize and cellSize, the longer is the execution. It's expected but what takes a "lot of time" is what's inside the loops and inside the loops is a pointer dereferencing 16*16*100000 times (When cellSize = 100000).
If I'm right, Dereferencing is a multiplication like:
tab[2][5][3] = tab + 2*16*100000 + 5*100000 + 3;
And do math like that 16*16*100000 times is long.
So to avoid maths, I thought about a pointer that permanently points totab[line][column][cell] but I don't know how to do it without having to recalculate the pointer each time cell is incremented.
I would like to use the
library for a hash table with a pair of int and const char * as a compound key:
typedef struct entry_s {
// This field is needed by the uthash library
// Compound key
const char *
Specifically, I want the string pointed by const char * to be a part of the key. To clarify: the different values of a pointer may correspond to the same string (in the sense of strcmp()).
shows how to implement a key that is similar to what I want with an int and char[] as a compound key:
typedef struct another_entry_s {
// This field is needed by the uthash library
// Compound key
char str[];
} another_
However, the second approach (i.e., (int, char[])) assumes the string is copied to char[], but I would like to avoid the copying.
Also, I'm not looking for concatenating int and string pointed by const char * in order to leverage HASH_ADD_KEYPTR() and HASH_FIND_STR() convenience macros.
I cannot figure out how to use HASH_ADD(), HASH_FIND(), and other general macros, with the first approach (i.e., (int, const char *)). It looks like it is impossible to avoid copying, as by the uthash library design. Do I understand that right? Or is there a non-copy approach that I overlooked?
Beginner here!
I'm attempting to put together a data structure with an array of structs and the array's length inside.
In reality I'm doing this with structs (not longs) but for the sake of simplicity, why can't I free this dynamically allocated memory?
typedef struct temptemp{
t_temptemp* temptemp_New();
t_temptemp* temptemp_newFill();
t_temptemp* temptemp_New(){
//assuming this allocates for a pointer to long.
t_temptemp* newMe = malloc(sizeof(t_temptemp));
return newMe;
t_temptemp* temptemp_newFill(){
t_temptemp *newMe = temptemp_New();
//assuming this allocates for ten longs in a row.
long *someLongs = malloc(10*sizeof(long));
//initialize longs
for(long i = 0 ; i & 10 ; i++)
someLongs[i]=i+100;
newMe-&arr=someL
return newMe;
The Code That Crashes:
t_temptemp* difficultCase = temptemp_newFill();
free(difficultCase-&arr);
//this crashes
I want to write a C program, where I have an array of 10 strings, where each string denotes the license plate number of the car parked at spot i. A spot is picked at random and if it is vacant, a random license plate number is generated and assigned to that spot, and if it is occupied, then the spot is vacated and the license plate number is deleted. However, the program is going into an infinite loop, which is what I want, but it is not printing any statements I have written to debug the program. The code is as follows:
#include &stdlib.h&
#include &stdio.h&
#include &string.h&
#include &unistd.h&
#include &time.h&
#include &stdint.h&
char * generateLicense()
srand((unsigned)time(NULL));
char const *code[] = {"AN","AP","AR","AS","BR","CG","CH","DD","DL","DN","GA","GJ","HR","HP","JH","JK","KA","KL","LD","MH","ML","MP","MN","MZ","NL","OD","PB","PY","RJ","SK","TN","TR","TS","UK","UP","WB"};
char const *alphabets[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
char const *numbers[] = {"0","1","2","3","4","5","6","7","8","9"};
char *licensePlate = (char *)malloc(100*sizeof(char));
strcpy(licensePlate,code[rand()%36]);
strcat(licensePlate,"-");
strcat(licensePlate,numbers[rand()%10]);
strcat(licensePlate,numbers[rand()%10]);
strcat(licensePlate,"-");
strcat(licensePlate,alphabets[rand()%26]);
strcat(licensePlate,alphabets[rand()%26]);
strcat(licensePlate,"-");
strcat(licensePlate,numbers[rand()%10]);
strcat(licensePlate,numbers[rand()%10]);
strcat(licensePlate,numbers[rand()%10]);
strcat(licensePlate,numbers[rand()%10]);
return licenseP
int main()
char *messagebody = (char *)malloc(100*sizeof(char));
char *licensePlate = (char *)malloc(100*sizeof(char));
char *currentSpot = (char *)malloc(10*sizeof(char));
char *by = ", by: ";
char *client = "From client 1, ";
char *spots[] = {"00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000"};
printf("variables declared\n");
srand((unsigned)time(NULL));
printf("in while loop\n");
//messagebody = "";
//licensePlate = "";
spot = rand()%10;
//currentSpot = "";
sprintf(currentSpot, "%d", spot);
printf("%s",currentSpot);
strcpy(messagebody,client);
printf("%s",messagebody);
if(spots[spot] == "00-00-00-0000")
printf("%s",messagebody);
strcpy(licensePlate, generateLicense());
printf("%s",licensePlate);
strcpy(spots[spot], licensePlate);
strcat(messagebody,"spot occupied: ");
printf("%s",messagebody);
strcat(messagebody,currentSpot);
printf("%s",messagebody);
strcat(messagebody,by);
printf("%s",messagebody);
strcat(messagebody,licensePlate);
printf("%s",messagebody);
printf("%s",messagebody);
strcpy(licensePlate, spots[spot]);
strcpy(spots[spot],"00-00-00-0000");
strcat(messagebody,"spot vacated: ");
printf("%s",messagebody);
strcat(messagebody,currentSpot);
printf("%s",messagebody);
strcat(messagebody,by);
printf("%s",messagebody);
strcat(messagebody,licensePlate);
printf("%s",messagebody);
printf("%s",messagebody);
I have included the statements I wrote to debug the program as well. What am I doing wrong here?
This code fails compilation on both the MSVC compiler and gcc (MinGW).
#include &stdlib.h&
int main() {
int *a = malloc(sizeof(int));
The compile error is
Error C2440 'initializing': cannot convert from 'void *' to 'int *'
Of course, changing the allocation line to
int *a = (int*) malloc(sizeof(int));
makes compilation possible. Why is an explicit type cast required? When I allocate other kinds of data (like a struct or ), there's no problem with this approach.
I was reading source code of WebKit yesterday when I found some lines of
code that I can't understand how it works.
There is a C++ method declared in WebPageProxy.h as this:
RefPtr&API::Navigation& loadRequest(const WebCore::ResourceRequest&, WebCore::ShouldOpenExternalURLsPolicy = WebCore::ShouldOpenExternalURLsPolicy::ShouldAllowExternalSchemes, API::Object* userData = nullptr);
It is called in WKWebView.mm like this:
- (WKNavigation *)loadRequest:(NSURLRequest *)request
auto navigation = _page-&loadRequest(request);
if (!navigation)
return [wrapper(*navigation.leakRef()) autorelease];
The argument request is of type NSURLRequest *, but the type in the declaration of the method is const WebCore::ResourceRequest&. I can't fully understand it.
Also, after setting some breakpoints and experimenting, I found that this constructor of the ResourceRequest class is being called:
ResourceRequest(NSURLRequest *nsRequest)
: ResourceRequestBase()
, m_nsRequest(nsRequest)
I'm not familiar with C++. Can someone help me understand how does this work?
Where is a clear, concise example of how to use Objective-C++ to access and use a native macOS framework in a Node.js native module?
For example: touch bar functionality
I would prefer for the example to be as short and simple as possible while still being clear. Thank you!
I want to convert an objective c code to objective cpp and use it in my application. Given the following code:
CFTypeRef dynamicValue = (__bridge CFTypeRef)([[NSBundle bundleWithPath:aStr] objectForInfoDictionaryKey: @"CFPlugInDynamicRegistration"]);
BOOL removeFlag = NO;
if(dynamicValue == nil)
removeFlag = NO;
else if( CFGetTypeID(dynamicValue) == CFBooleanGetTypeID()
removeFlag = CFBooleanGetValue(dynamicValue);
else if( CFGetTypeID(dynamicValue) == CFStringGetTypeID()
removeFlag = ( [[(__bridge NSString*)dynamicValue lowercaseString] isEqualToString:@"yes"] ? YES:NO);
if( removeFlag )
[unloadedArray addObject:aStr];
[sortedArray removeObjectAtIndex:hoge];
I get an error when changing the file name from file.m to file.mm and
building: "no matching function for call to 'CFBooleanGetValue'"
I tried to #include &CoreFoundation/CFNumber.h& but I get the same error (I guess it's an ifdef issue).
Do you know any way I can cast without the missing function? Should I try to use the same function anyway?

我要回帖

更多关于 cvalistpointer 的文章

 

随机推荐