Windbg dump analysis: what causes error 0x80004002 while asking for handle information? - windbg

I'm debugging the memory dump of a process, of which I assume the number of handles becoming too large. When I open the dump in Windbg, I see following error/warning message (I don't know if this is relevant to my question) :
Dir entry 8, HandleDataStream stream has too many elements (0xfefffd > 0x400000)
While launching the Windbg !handle extension command, I see following error message:
0:000> !handle
ERROR: !handle: extension exception 0x80004002.
"Unable to read handle information"
I have already launched that same extension command on other memory dumps of the same process (maybe another version). Hence I don't understand the relevance of most Google results of that error code (something about a wrong interface).
Does anybody know what might cause the mentioned error code and what I can do in order to see the amount of handles in my application dump?
For your information, I'm not interested in every single handle, just the total amount of them.
Edit after first comments
The results of .dumpdebug are the following: (only handle related)
0:000> .dumpdebug
----- User Mini Dump Analysis
MINIDUMP_HEADER:
Version A793 (62F0)
NumberOfStreams 13
Flags 61826
0002 MiniDumpWithFullMemory
0004 MiniDumpWithHandleData
...
Stream 8: type HandleDataStream (12), size 27D7FF98, RVA 101DEF6C
Dir entry 8, HandleDataStream stream has too many elements (0xfefffd > 0x400000)
Stream 9: type CommentStreamW (11), size 000001A0, RVA 000102E0
'
*** "C:\Internal\Tools\Procdump\procdump.exe" -ma -accepteula 18732 C:\Dumps\Own_Application_PID_18732_2019_02_07_11_38_02_777_NOW.dmp
*** Manual dump'
(The results of .dumpdebug and Dumpchk.exe are very similar, I decided not to add them too)
Edited after chdump.py result
Hereby the result (partially) of chdump.py:
MINIDUMP_HEADER EXCLUDING SIGNATURE
version 0xa793
internal version 0x62f0
Number of Streams 0xd
Stream Directory RVA 0x20
CheckSum 0x0
u.TimeDateStamp 2019-02-07 11:45:24
Flags 0x61826
MINIDUMP_DIRECTORY
StreamType DataSize RVA
0x3 0x754 0x434
0x11 0x9cc 0xb88
0x4 0x1588 0x1554
0x13 0x290 0x2adc
0x9 0x12250 0x37fc9f84
0x10 0x6b080 0x37f5ef04
0x7 0x38 0xbc
0xf 0x340 0xf4
0xc 0x27d7ff98 0x101def6c
0xb 0x1a0 0x102e0
0x0 0x0 0x0
0x0 0x0 0x0
0x0 0x0 0x0
_MHDesc2
Handle TypeNameRva ObjectNameRva Attributes GrantedAccess HandleCount PointerCount ObjectInfoRva Reserved0
0x4 0x10490 0x104a8 0x10 0x3 0x7c 0x1ee0c0b 0x0 0x0
0x8 0x104c2 0x0 0x0 0x100020 0x2 0x80001 0x0 0x0
0xc 0x104d0 0x104dc 0x0 0x1 0x2 0x80001 0x0 0x0
0x10 0x1055e 0x1056a 0x0 0x20019 0x2 0x80000 0x0 0x0
0x14 0x105f6 0x0 0x0 0x1f0000 0x2 0x80002 0x0 0x0
0x18 0x1060e 0x0 0x0 0x1f0003 0x2 0x80001 0x0 0x0
0x28 0x1061e 0x1062a 0x0 0xf003f 0x2 0x7ffba 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x30 0x10652 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x34 0x10662 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x38 0x10672 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x3c 0x10682 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x40 0x10692 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x44 0x106a2 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x4c 0x106b2 0x106ca 0x10 0xf 0x44 0xfe9d9c 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x78 0x106f2 0x0 0x0 0x1f0003 0x2 0x7ffc7 0x0 0x0
0x7c 0x10702 0x1070e 0x0 0x20019 0x2 0x7fffe 0x0 0x0
0x80 0x1077a 0x0 0x0 0x100020 0x2 0x40002 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x88 0x10788 0x0 0x0 0x100003 0x2 0x40002 0x0 0x0
0x8c 0x107a0 0x0 0x0 0x100003 0x2 0x40002 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0xb0 0x107b8 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0xb4 0x107c8 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0xb8 0x107d8 0x0 0x0 0x1f0003 0x2 0x7fddf 0x0 0x0
0xbc 0x107f0 0x0 0x0 0x1f0003 0x2 0x7fea0 0x0 0x0
0xc0 0x10808 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0xc4 0x10820 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0xc8 0x10838 0x0 0x0 0x1f0003 0x2 0x7fff6 0x0 0x0
0xcc 0x10850 0x0 0x0 0x1f0003 0x2 0x7fd62 0x0 0x0
0xd0 0x10868 0x0 0x0 0x1f0003 0x2 0x6f1cc 0x0 0x0
0xd4 0x10878 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0xd8 0x10888 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0xdc 0x108a0 0x108ac 0x0 0xf003f 0x2 0x80000 0x0 0x0
0xe0 0x108f6 0x10902 0x0 0x20019 0x2 0x80000 0x0 0x0
0xe4 0x10958 0x10964 0x0 0x20019 0x2 0x80001 0x0 0x0
0xe8 0x10a08 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0xec 0x10a18 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0xf0 0x10a28 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x100 0x10a38 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x104 0x10a48 0x0 0x0 0x1fffff 0x4 0xff501 0x10a5a 0x0
0x108 0x10a96 0x0 0x0 0x1f0000 0x2 0x7fff8 0x0 0x0
0x10c 0x10aae 0x0 0x0 0x1f0003 0x2 0x7fffe 0x0 0x0
0x110 0x10abe 0x0 0x0 0x1f0003 0x2 0x5ebe2 0x0 0x0
0x114 0x10adc 0x0 0x0 0xf00ff 0x2 0x73b3f 0x0 0x0
0x118 0x10b00 0x0 0x0 0x100002 0x2 0x80002 0x0 0x0
0x11c 0x10b10 0x0 0x0 0x1 0x2 0x80002 0x0 0x0
0x120 0x10b3e 0x0 0x0 0x100002 0x2 0x7d72d 0x0 0x0
0x124 0x10b4e 0x0 0x0 0x1 0x2 0x5ebe2 0x0 0x0
0x128 0x10b7c 0x0 0x0 0x1f0003 0x2 0x80000 0x0 0x0
0x12c 0x10b8c 0x0 0x0 0x1f0003 0x2 0x80000 0x0 0x0
0x130 0x10b9c 0x0 0x0 0x1f0003 0x2 0x5671f 0x0 0x0
0x134 0x10bac 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x138 0x10bbc 0x0 0x0 0x1fffff 0x4 0xbf505 0x10bce 0x0
0x13c 0x10c0a 0x0 0x0 0x1f0003 0x2 0x40002 0x0 0x0
0x140 0x10c1a 0x0 0x0 0x1f0003 0x2 0x74432 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x148 0x10c2a 0x0 0x0 0x1f0003 0x2 0x80001 0x0 0x0
0x14c 0x10c3a 0x0 0x0 0x100001 0x2 0x7feb3 0x0 0x0
0x150 0x10c48 0x0 0x0 0x1f0003 0x2 0x80001 0x0 0x0
0x154 0x10c58 0x0 0x0 0x1f0000 0x2 0x4d899 0x0 0x0
0x158 0x10c70 0x0 0x0 0x1f0003 0x2 0x7ffdc 0x0 0x0
0x15c 0x10c80 0x0 0x0 0x1f0003 0x2 0x80000 0x0 0x0
0x160 0x10c90 0x10c9c 0x0 0xf003f 0x2 0x7ffd6 0x0 0x0
0x164 0x10ce6 0x0 0x0 0x1f0003 0x2 0x80000 0x0 0x0
0x168 0x10cf6 0x10d0a 0x0 0x4 0xa3 0x28c0002 0x0 0x0
0x16c 0x10d5a 0x10d66 0x0 0xf003f 0x2 0x7ffc4 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x174 0x10db0 0x10dc0 0x10 0x100001 0x53 0x18003f 0x0 0x0
0x178 0x10e10 0x10e1c 0x0 0x20019 0x2 0x7fff4 0x0 0x0
0x17c 0x10e94 0x10ea0 0x0 0x20019 0x2 0x7fff4 0x0 0x0
0x180 0x10f1c 0x10f30 0x0 0x4 0xa3 0x28c0002 0x0 0x0
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x188 0x10f80 0x0 0x0 0x120089 0x2 0x7fffc 0x0 0x0
0x18c 0x10f8e 0x0 0x0 0xf0005 0x2 0x80001 0x0 0x0
It does very far: the Python script even stopped, due to a memory error, after having generated ±13 million(!) lines of results.
Thanks in advance

The Format of Minidump file is almost Documented You can parse the file yourself without having to Rely on windbg
say with python
the Error seems to be Explicit there is some corruption in the _MINIDUMP_DIRECTORY->DataSize
The Max Number of Handles iirc is limited to 10000 handles per process
(browse for Raymond Chens blog old new thing)
so there is must be some hardcoded limit for the stream size which when violated
results in that error
below is a quickly churned python script that takes a dump and dumps the raw data
open the dump in hexeditor and peer around or maybe patch to recover part handle information
%%writefile chkdump.py
import sys
import os
import struct
import datetime
scriptname = os.path.split(sys.argv[0])[1]
if (len(sys.argv) != 2 ):
sys.exit("usage python %s path_to_dump" % scriptname)
fin = open(sys.argv[1],'rb')
if( fin.read(4) != 'MDMP' ):
fin.close()
sys.exit("not a windbg dump file no MDMP signature")
print ( "MINIDUMP_HEADER EXCLUDING SIGNATURE")
dmphdr = struct.unpack("<HHiiiiQ",fin.read(28))
print ( "%-20s\t0x%x") % ( "version", dmphdr[0] )
print ( "%-20s\t0x%x") % ( "internal version", dmphdr[1] )
print ( "%-20s\t0x%x") % ( "Number of Streams", dmphdr[2] )
print ( "%-20s\t0x%x") % ( "Stream Directory RVA", dmphdr[3] )
print ( "%-20s\t0x%x") % ( "CheckSum", dmphdr[4] )
print ( "%-20s\t") % ( "u.TimeDateStamp" ),
print ( datetime.datetime.fromtimestamp(dmphdr[5]))
print ( "%-20s\t0x%x") % ( "Flags", dmphdr[6] )
print ("\nMINIDUMP_DIRECTORY ")
print ("%-24s%-24s%-24s") % ("StreamType" , "DataSize","RVA")
streamdata = []
for i in range(0,dmphdr[2],1):
streamdata.insert(i,struct.unpack("<iii",fin.read(12)))
print ("%-24s%-24s%-24s") % ( hex(streamdata[i][0]),
hex(streamdata[i][1]),hex(streamdata[i][2]))
HStreamLoc, = [z for (x,y,z) in streamdata if x == 0xc]
HStreamDSize, = [y for (x,y,z) in streamdata if x == 0xc]
fin.seek(HStreamLoc)
sizeof_HDStream = 16
HDStream = struct.unpack("<iiii",fin.read(sizeof_HDStream))
assert (HDStream[1] * HDStream[2] + sizeof_HDStream ) == HStreamDSize
print ("_MHDesc2")
sizeof_MHDesc2 = 40
HDesc = []
print ("%-14s%-14s%-14s%-14s%-14s%-14s%-14s%-14s%-14s") % ("Handle" ,"TypeNameRva",
"ObjectNameRva","Attributes","GrantedAccess","HandleCount","PointerCount",
"ObjectInfoRva","Reserved0")
for i in range(0,HDStream[2],1):
HDesc.insert(i,struct.unpack("<Qiiiiiiii",fin.read(sizeof_MHDesc2)))
print ("%-14s%-14s%-14s%-14s%-14s%-14s%-14s%-14s%-14s") % ( hex(HDesc[i][0]),
hex(HDesc[i][1]), hex(HDesc[i][2]), hex(HDesc[i][3]),hex(HDesc[i][4]),
hex(HDesc[i][5]), hex(HDesc[i][6]),hex(HDesc[i][7]), hex(HDesc[i][8]))
when executed it would return data like this for handle stream
MINIDUMP_HEADER EXCLUDING SIGNATURE
version 0xa793
internal version 0x61b1
Number of Streams 0xd
Stream Directory RVA 0x20
CheckSum 0x0
u.TimeDateStamp 2019-02-14 02:38:24
Flags 0x61826
MINIDUMP_DIRECTORY
StreamType DataSize RVA
0x3 0x94 0x1dc
0x11 0xcc 0x270
0x4 0xc40 0x33c
0x13 0x388 0xf7c
0x9 0x1100 0x91f0
0x10 0x4f30 0x42c0
0x7 0x38 0xbc
0xf 0xe8 0xf4
0xc 0xb28 0x3798
0xb 0x58 0x294c
0x0 0x0 0x0
0x0 0x0 0x0
0x0 0x0 0x0
_MHDesc2
Handle TypeNameRva ObjectNameRva Attributes GrantedAccess HandleCount PointerCount ObjectInfoRva Reserved0
0x4 0x29b4 0x29cc 0x10 0x3 0x2d 0x54 0x0 0x0
0x8 0x29e6 0x0 0x0 0x100020 0x2 0x3 0x0 0x0
0xc 0x29f4 0x0 0x0 0x100020 0x2 0x3 0x0 0x0
0x10 0x2a02 0x0 0x0 0x100020 0x2 0x3 0x0 0x0
0x14 0x2a10 0x0 0x0 0x1f0000 0x2 0x5 0x0 0x0
EDIT
i have edited the code to print handle , type name , object name
and put it here
the results will be like
Handle TypeName ObjectName
0x4 Directory \KnownDlls
0x8 File No ObjName
0xc File No ObjName
0x10 File No ObjName
0x14 ALPC Port No ObjName
0x18 Key \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\Nls\Sorting\Versions
it should print all the 16.7 million handles in your dump if they exist

Related

Pymodbus Asynchronous Server with RTU framer not working

I'm trying to implement a modbus asynchronous serial server with the library Pymodbus. I'm using a ModbusRtuFramer as a frame template. Unfortunately, when I send a command, it looks like the bytes are splitted and the frame is not recognized. I post both the code and the log.
StartSerialServer(context, identity=identity,
framer=ModbusRtuFramer,
port='/dev/ttyS2',
timeout=0.1,
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1)
I send the command 03102c0002 (read 2 holding registers starting from address 102c. The log says:
DEBUG:pymodbus.server.asynchronous:Data Received: 0x1
DEBUG:pymodbus.framer.rtu_framer:Frame - [b'\x01'] not ready
DEBUG:pymodbus.server.asynchronous:Data Received: 0x3 0x10 0x2c
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x1 0x3 0x10 0x2c
DEBUG:pymodbus.server.asynchronous:Data Received: 0x0 0x2 0x1 0x2
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x0 0x2 0x1 0x2
DEBUG:pymodbus.server.asynchronous:Data Received: 0x1
DEBUG:pymodbus.framer.rtu_framer:Frame - [b'\x01'] not ready
DEBUG:pymodbus.server.asynchronous:Data Received: 0x3 0x10 0x2c
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x1 0x3 0x10 0x2c
DEBUG:pymodbus.server.asynchronous:Data Received: 0x0 0x2 0x1
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x0 0x2 0x1
DEBUG:pymodbus.server.asynchronous:Data Received: 0x2
DEBUG:pymodbus.framer.rtu_framer:Frame - [b'\x02'] not ready
DEBUG:pymodbus.server.asynchronous:Data Received: 0x1
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x2 0x1
DEBUG:pymodbus.server.asynchronous:Data Received: 0x3 0x10 0x2c 0x0
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x3 0x10 0x2c 0x0
DEBUG:pymodbus.server.asynchronous:Data Received: 0x2 0x1 0x2
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x2 0x1 0x2
DEBUG:pymodbus.server.asynchronous:Data Received: 0x1
DEBUG:pymodbus.framer.rtu_framer:Frame - [b'\x01'] not ready
DEBUG:pymodbus.server.asynchronous:Data Received: 0x3 0x10 0x2c
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x1 0x3 0x10 0x2c
DEBUG:pymodbus.server.asynchronous:Data Received: 0x0 0x2 0x1 0x2
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x0 0x2 0x1 0x2

UDS SID2E & SID22

In SID2E and SID22 is there a condition where the length of the entire frame will exceed 7 bytes?
If yes then how it will send or write tha data bytes?
Yes, it is a common use-case in UDS that the response to SID 0x22 (ReadDataByIdentifier) or the request to SID 0x2E (WriteDataByIdentifier) exceeds 7 bytes in length. For this purpose a message consisting of multple CAN frames is
sent, utilizing the Transport-Layer (ISO-TP, ISO 15765-2).
Consider a normal single frame message, where the high-nibble of the first byte is 0x0 i.e.
0x7E0 0x03 0x22 0xF1 0x90
0x7E8 0x04 0x62 0xF1 0x90 0x01
Here the payload is within 7 bytes (in both request and response), so the low-nibble of the first byte tells us the exact length (0x03 in the request, 0x04 in the response). As the complete message fits within a single CAN frame, nothing else is required. But to send a longer diagnostic message, it needs to be splitted across multple CAN frames (segmentation). For this 3 different types of messages are required:
A first frame is sent to the receiver to start the transmission.
This contains the length (up to 4095 bytes) of the complete payload
data and the first 6 bytes of the message. The high-nibble 0x1 of
the first byte indicates that the message is a first frame.
A flow control frame confirming receiving of the first frame is
replied to the sender of the first frame. It contains additional
information like expected STmin timing and block-size. The
high-nibble 0x3 of the first byte indicates that the message is a
flow control frame.
One or more consecutive frames are sent to the receiver, which
contain up to 7 bytes of the remaining payload - together with a
counter to ensure the data can be desegmented in the correct order.
The high-nibble 0x2 of the first byte indicates that the message is
a consecutive frame.
Now consider the following scenario: The tester application sends the single frame 0x7E0 0x03 0x22 0xF1 0x90 as request. The ECU may want to send the response 0x62 0xF1 0x90 0x01 0x02 0x03 0x04 0x05 (8 bytes payload) to the tester application.
So the ECU will first send the first frame:
0x7E8 0x10 0x08 0x62 0xF1 0x90 0x01 0x02 0x03
And wait for a flow control frame from the tester application:
0x7E0 0x30 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Then the ECU will continue to send consecutive frames until the
complete message is transmitted:
0x7E8 0x21 0x04 0x05 0x00 0x00 0x00 0x00 0x00
For the SID 0x2E (WriteDataByIdentifier) it will be very similar, just the roles are reversed as typically the tester application wants to send the long message in the request and the ECU will reply with the flow control message. i.e.
0x7E0 0x10 0x08 0x2E 0xF1 0x90 0x01 0x02 0x03
0x7E8 0x30 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x7E0 0x21 0x04 0x05 0x00 0x00 0x00 0x00 0x00
0x7E8 0x03 0x6E 0xF1 0x90 0x00 0x00 0x00 0x00
If more than one consecutive frame is required, the first byte will simply be increased from 0x21 up to 0x2F and then start again from 0x20 to count up.
0x7E0 0x10 0x76 0x2E 0xF1 0x90 0x01 0x02 0x03
0x7E8 0x30 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x7E0 0x21 0x04 0x05 0x06 0x07 0x08 0x09 0x0A
0x7E0 0x22 0x0B 0x0C 0x0D 0x0E 0x0F 0x10 0x11
...
0x7E0 0x2F 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0x7E0 0x20 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF

merge two images into one image in iPhone

I need to merge two images into one image, here is my code:
-(UIImage*)mergeImage:(UIImage*)mask overImage:(UIImage*)source inSize:(CGSize)size
{
//Capture image context ref
UIImageView *totalimage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UIImageView *firstImage=[[UIImageView alloc] initWithImage:mask];
UIImageView *secondImage=[[UIImageView alloc] initWithImage:source];
[totalimage addSubview:firstImage];
[totalimage addSubview:secondImage];
UIGraphicsBeginImageContext(totalimage.bounds.size);
[totalimage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Draw images onto the context
[source drawInRect:CGRectMake(0, 0, source.size.width, source.size.height)];
[mask drawInRect:CGRectMake(0, 0, mask.size.width, mask.size.height)];
return viewImage;
}
I call this method as follows:
UIImage *totalImage = [self mergeImage:self.Apicimage overImage:questionImage inSize:CGSizeMake(100, 100)];
but on execution I get this output:
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSaveGState: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetBlendMode: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetAlpha: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextTranslateCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextScaleCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextConcatCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextDrawImage: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextRestoreGState: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSaveGState: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetBlendMode: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetAlpha: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextTranslateCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextScaleCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextConcatCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextDrawImage: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextRestoreGState: invalid context 0x0
can anyone guide me please. How can I merge two images?
your code
[source drawInRect:CGRectMake(0, 0, source.size.width, source.size.height)];
[mask drawInRect:CGRectMake(0, 0, mask.size.width, mask.size.height)];
should be written before
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Random img saving error on some iPhone 4s and iPod Touches

i encountered a weird error. i will like the app to save an image into camera roll.
but a small amount of device are giving me error saving msg.
EDIT: ERROR Log
"Failed to encode image for saved photos." UserInfo=0x3e7170 {NSUnderlyingError=0x389310 "Failed to encode image for saved photos.", NSLocalizedDescription=Failed to encode image for saved photos.
May 22 14:13:34 unknown assetsd[281] <Error>: ImageIO: CGImageDestinationAddImage image parameter is nil
May 22 14:13:34 unknown assetsd[281] <Error>: ImageIO: CGImageDestinationFinalize image destination does not have enough images
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextSetBaseCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextSaveGState: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextSetInterpolationQuality: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextFillRects: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextSetBaseCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextSaveGState: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextSetInterpolationQuality: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextFillRects: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextConcatCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextDrawImage: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextConcatCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextDrawImage: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextRestoreGState: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGBitmapContextCreateImage: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextRestoreGState: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGBitmapContextCreateImage: invalid context 0x0
>
below are my code used to save the images.
- (IBAction)saveImage:(id)sender {
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.labelText = #"Its saving!";
self.imageOverlay.alpha = 1;
self.savedImage = [self maskImage:self.imgView withMask:self.baseImgView];
UIImageWriteToSavedPhotosAlbum(self.savedImage, self, #selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSString *message;
NSString *title;
if (!error) {
title = NSLocalizedString(#"Save Success", #"");
message = NSLocalizedString(#"Save Success Message", #"");
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = #"We’re saved - WooHoo!!";
[HUD hide:YES afterDelay:1.5];
saved =1;
self.imageOverlay.alpha =0.85;
[self performSelector:#selector(saveSuccess) withObject:nil afterDelay:1.5];
} else
{
title = NSLocalizedString(#"Save Failed", #"");
message = [error description];
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sad_face.png"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = #"Error! Try saving photo again :p";
[HUD hide:YES afterDelay:3];
}
}
- (UIImage*) maskImage:(UIImageView *)maskImage withMask:(UIImageView *)cropImage
{
UIImage *image = nil;
UIImage *imagePNG = nil;
CGSize newImageSize = CGSizeMake(cropImage.frame.size.width, cropImage.frame.size.height);
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
[self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imgData = UIImagePNGRepresentation ( image ); // get PNG representation
imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation
return imagePNG;
}
- (UIImage*) maskImage:(UIImageView *)maskImage withMask:(UIImageView *)cropImage
{
UIImage *image = nil;
UIImage *imagePNG = nil;
CGSize newImageSize = CGSizeMake(cropImage.frame.size.width, cropImage.frame.size.height);
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
[self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
NSData *imgData = UIImagePNGRepresentation ( image ); // get PNG representation
imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation
UIGraphicsEndImageContext();
return imagePNG;
}

iPhone Choking on Certain Image Formats

My app allows upload of an image from the camera roll. Some images are causing the app to crash when I attempt to save them to my documents directory prior to upload. I'm converting the image to a PNG prior to saving, but that hasn't helped. Here's the error...
Tue Aug 31 20:39:13 localhost XXX[76409] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 1600 bytes/row.
Tue Aug 31 20:39:13 localhost XXX[76409] <Error>: CGContextConcatCTM: invalid context 0x0
Tue Aug 31 20:39:13 localhost XXX[76409] <Error>: CGContextSetInterpolationQuality: invalid context 0x0
Tue Aug 31 20:39:13 localhost XXX[76409] <Error>: CGContextDrawImage: invalid context 0x0
Tue Aug 31 20:39:13 localhost XXX[76409] <Error>: CGBitmapContextCreateImage: invalid context 0x0
and here's a sample of my code...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *pngData = UIImagePNGRepresentation(img);
UIImage *pngImg = [UIImage imageWithData:pngData];
... code to save image ...
}
Note also that what you are doing is redundant and a waste of memory. img and pngImg are the same. Or is there a specific reason that you convert an image to png and then back to an image?