Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Threading function
i have created a thread in which i am allocating memory to a pointer,freeing it inside the same. The function is being called in more than one thread.but some times it is getting crashed because of EXC_BAD_INSTRUCTION and at log it is giving incorrect checksum for frees object - object was probably modified after being freed.
 

this is the code
Posted 18-May-12 1:13am
Edited 18-May-12 11:09am
v3
Comments
Richard MacCutchan at 18-May-12 6:28am
   
Very interesting, but I do not think that anyone can help you without a lot more information.
subrata kumar Nayak at 18-May-12 7:14am
   
sry....i don't know what i need to give as more information.......
subrata kumar Nayak at 18-May-12 8:22am
   
above i have updated the question with code ........
Richard MacCutchan at 18-May-12 12:44pm
   
Post the code here, not on some other site. Note, use the "Improve Question" link and put <pre> tags around it. Tell us exactly which line gets an error and what the error is.
CPallini at 18-May-12 8:39am
   
Is the pointer variable shared among the threads?
subrata kumar Nayak at 18-May-12 8:44am
   
no,the variable is not shared,as there are two threads only(main,thread created by me)and i am using the variable in thread created by me only...not in main thread.....
(u can check the code also,posted by me.)
CPallini at 18-May-12 16:57pm
   
I see no code.
subrata kumar Nayak at 19-May-12 0:09am
   
in my question only, there is a link "this is the code",click on code ,it will redirect to my code.....
CPallini at 19-May-12 4:22am
   
OK, I missed it.
However, you are a liar :-) since p.Var IS a shared variable and it is the root of all your problems (if you are not married).
subrata kumar Nayak at 19-May-12 6:35am
   
pVar is an global object .....it is not being shared among threads......
CPallini at 19-May-12 14:07pm
   
If it is global then it is shared.
Pablo Aliskevicius at 18-May-12 12:49pm
   
I'd suggest checking the return values of the allocation functions (GlobalAlloc, GlobalRealloc, ...) for NULL. Lately, memory allocation functions are returning NULL more than they used to, particularly on virtual machines. Something to do with timeouts.
subrata kumar Nayak at 19-May-12 0:12am
   
by checking null also it is not working.....
i am checking whether the return type of malloc is null or not.....it coming not null,still it is crashing.......
SAKryukov at 18-May-12 13:18pm
   
What you really need it to make a code sample (better be complete but as short as possible) just to reproduce this exact problem. If you can do it, use "Improve question" above.
First of all, check up if you can reproduce the problem is a single-thread application.
--SA
subrata kumar Nayak at 19-May-12 0:11am
   
problem is not there with single thread application.....when ever i am creating multiple thread then only it happens.....
SAKryukov at 22-May-12 0:51am
   
OK, that's easier. Now, you need to create a code sample and provided it, with steps to reproduce. How about it?
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you need to balance the memory allocations per thread. So beware of accessing memory in different threads, ie per global objects.
 
Take care in exiting every thread that cleanup is fine.
 
Dont use functions or API-Calls in destructors!!!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 400
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 245
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 23 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100